blob: 03bae41a62aaa337cf1c44fc7e00a652d8656417 [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// Copyright (c) 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31// Author: williasr@google.com (Scott Williams)
32//
33// This file implements the TemplateDictionaryInterface class. This interface
34// forms the root of the TemplateDictionary class tree, but the interface is
35// minimal enough to allow other sources of template data. Note that the
36// TemplateDictionaryInterface class enumerates the properties expected by
37// Template: it doesn't constrain how data gets into the
38// TemplateDictionaryInterface class to begin with. For these methods, see
39// TemplateDictionary.
40//
41
42#ifndef TEMPLATE_TEMPLATE_DICTIONARY_INTERFACE_H_
43#define TEMPLATE_TEMPLATE_DICTIONARY_INTERFACE_H_
44
45#include <stdlib.h>
46#include <string>
47#include <ctemplate/template_string.h>
48
49// NOTE: if you are statically linking the template library into your binary
50// (rather than using the template .dll), set '/D CTEMPLATE_DLL_DECL='
51// as a compiler flag in your project file to turn off the dllimports.
52#ifndef CTEMPLATE_DLL_DECL
53# define CTEMPLATE_DLL_DECL __declspec(dllimport)
54#endif
55
56namespace ctemplate {
57
58const int kIndent = 2; // num spaces to indent each level -- used with dump
59
60// TemplateDictionaryInterface
61// The template data contains the associated values for
62// variables, the hidden/visible state for sections and included
63// templates, the associated set of dictionaries for sections and
64// included templates, and the template filenames to be expanded in
65// place of template-include nodes.
66class CTEMPLATE_DLL_DECL TemplateDictionaryInterface {
67 public:
68 // TemplateDictionaryInterface destructor
69 virtual ~TemplateDictionaryInterface() {}
70
71 protected:
72 // The interface as follows is used at expand-time by Expand.
73 friend class VariableTemplateNode;
74 friend class SectionTemplateNode;
75 friend class TemplateTemplateNode;
76 // This class reaches into our internals for testing.
77 friend class TemplateDictionaryPeer;
78 friend class TemplateDictionaryPeerIterator;
79
80 // GetSectionValue
81 // Returns the value of a variable.
82 virtual TemplateString GetValue(const TemplateString& variable) const = 0;
83
84 // IsHiddenSection
85 // A predicate to indicate the current hidden/visible state of a section
86 // whose name is passed to it.
87 virtual bool IsHiddenSection(const TemplateString& name) const = 0;
88
89 // Dump a string representation of this dictionary to the supplied string.
90 virtual void DumpToString(std::string* out, int level) const = 0;
91
92 // TemplateDictionaryInterface is an abstract class, so its constructor is
93 // only visible to its subclasses.
94 TemplateDictionaryInterface() {}
95
96 class Iterator {
97 protected:
98 Iterator() { }
99 public:
100 virtual ~Iterator() { }
101
102 // Returns false if the iterator is exhausted.
103 virtual bool HasNext() const = 0;
104
105 // Returns the current referent and increments the iterator to the next.
106 virtual const TemplateDictionaryInterface& Next() = 0;
107 };
108
109 // IsHiddenTemplate
110 // Returns true if the template include is hidden. This is analogous to
111 // IsHiddenSection, but for template nodes.
112 virtual bool IsHiddenTemplate(const TemplateString& name) const = 0;
113
114 // GetIncludeTemplateName
115 // Returns the name of the template associated with the given template
116 // include variable. If more than one dictionary is attached to the include
117 // symbol, dictnum can be used to disambiguate which include name you mean.
118 virtual const char* GetIncludeTemplateName(
119 const TemplateString& variable, int dictnum) const = 0;
120
121 // CreateTemplateIterator
122 // A factory method for constructing an iterator representing the
123 // subdictionaries of the given include node. The caller is
124 // responsible for deleting the return value when it's done with it.
125 virtual Iterator* CreateTemplateIterator(
126 const TemplateString& section) const = 0;
127
128 // CreateSectionIterator
129 // A factory method for constructing an iterator representing the
130 // subdictionaries of the given section node. The caller is
131 // responsible for deleting the return value when it's done with it.
132 virtual Iterator* CreateSectionIterator(
133 const TemplateString& section) const = 0;
134
135 // IsUnhiddenSection
136 // Returns true if the section has been marked visible and false otherwise.
137 virtual bool IsUnhiddenSection(
138 const TemplateString& name) const = 0;
139
140 private:
141 // Disallow copy and assign.
142 TemplateDictionaryInterface(const TemplateDictionaryInterface&);
143 void operator=(const TemplateDictionaryInterface&);
144};
145
146}
147
148
149#endif // TEMPLATE_TEMPLATE_DICTIONARY_INTERFACE_H_