blob: 0cc9171012c1331d546888aae0ac32bba00ea7d3 [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@ac_windows_dllexport_defines@
50
51namespace ctemplate {
52
53const int kIndent = 2; // num spaces to indent each level -- used with dump
54
55// TemplateDictionaryInterface
56// The template data contains the associated values for
57// variables, the hidden/visible state for sections and included
58// templates, the associated set of dictionaries for sections and
59// included templates, and the template filenames to be expanded in
60// place of template-include nodes.
61class @ac_windows_dllexport@ TemplateDictionaryInterface {
62 public:
63 // TemplateDictionaryInterface destructor
64 virtual ~TemplateDictionaryInterface() {}
65
66 protected:
67 // The interface as follows is used at expand-time by Expand.
68 friend class VariableTemplateNode;
69 friend class SectionTemplateNode;
70 friend class TemplateTemplateNode;
71 // This class reaches into our internals for testing.
72 friend class TemplateDictionaryPeer;
73 friend class TemplateDictionaryPeerIterator;
74
75 // GetSectionValue
76 // Returns the value of a variable.
77 virtual TemplateString GetValue(const TemplateString& variable) const = 0;
78
79 // IsHiddenSection
80 // A predicate to indicate the current hidden/visible state of a section
81 // whose name is passed to it.
82 virtual bool IsHiddenSection(const TemplateString& name) const = 0;
83
84 // Dump a string representation of this dictionary to the supplied string.
85 virtual void DumpToString(std::string* out, int level) const = 0;
86
87 // TemplateDictionaryInterface is an abstract class, so its constructor is
88 // only visible to its subclasses.
89 TemplateDictionaryInterface() {}
90
91 class Iterator {
92 protected:
93 Iterator() { }
94 public:
95 virtual ~Iterator() { }
96
97 // Returns false if the iterator is exhausted.
98 virtual bool HasNext() const = 0;
99
100 // Returns the current referent and increments the iterator to the next.
101 virtual const TemplateDictionaryInterface& Next() = 0;
102 };
103
104 // IsHiddenTemplate
105 // Returns true if the template include is hidden. This is analogous to
106 // IsHiddenSection, but for template nodes.
107 virtual bool IsHiddenTemplate(const TemplateString& name) const = 0;
108
109 // GetIncludeTemplateName
110 // Returns the name of the template associated with the given template
111 // include variable. If more than one dictionary is attached to the include
112 // symbol, dictnum can be used to disambiguate which include name you mean.
113 virtual const char* GetIncludeTemplateName(
114 const TemplateString& variable, int dictnum) const = 0;
115
116 // CreateTemplateIterator
117 // A factory method for constructing an iterator representing the
118 // subdictionaries of the given include node. The caller is
119 // responsible for deleting the return value when it's done with it.
120 virtual Iterator* CreateTemplateIterator(
121 const TemplateString& section) const = 0;
122
123 // CreateSectionIterator
124 // A factory method for constructing an iterator representing the
125 // subdictionaries of the given section node. The caller is
126 // responsible for deleting the return value when it's done with it.
127 virtual Iterator* CreateSectionIterator(
128 const TemplateString& section) const = 0;
129
130 // IsUnhiddenSection
131 // Returns true if the section has been marked visible and false otherwise.
132 virtual bool IsUnhiddenSection(
133 const TemplateString& name) const = 0;
134
135 private:
136 // Disallow copy and assign.
137 TemplateDictionaryInterface(const TemplateDictionaryInterface&);
138 void operator=(const TemplateDictionaryInterface&);
139};
140
141}
142
143#endif // TEMPLATE_TEMPLATE_DICTIONARY_INTERFACE_H_