blob: 6abce971882ed5d0bd07b40a132e69f5f5e43cf6 [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: csilvers@google.com (Craig Silverstein)
32//
33// In addition to a TemplateDictionary, there is also a PerExpandData
34// dictionary. This dictionary holds information that applies to one
35// call to Expand, such as whether to annotate the template expansion
36// output. A template dictionary is associated with a template (.tpl)
37// file; a per-expand dictionary is associated to a particular call to
38// Expand() in a .cc file.
39//
40// For (many) more details, see the doc/ directory.
41
42#ifndef TEMPLATE_PER_EXPAND_DATA_H_
43#define TEMPLATE_PER_EXPAND_DATA_H_
44
45#include <stdlib.h> // for NULL
46#include <string.h> // for strcmp
47#include <sys/types.h>
48#include <hash_map>
49#include <ctemplate/template_string.h> // for StringHash
50
51// NOTE: if you are statically linking the template library into your binary
52// (rather than using the template .dll), set '/D CTEMPLATE_DLL_DECL='
53// as a compiler flag in your project file to turn off the dllimports.
54#ifndef CTEMPLATE_DLL_DECL
55# define CTEMPLATE_DLL_DECL __declspec(dllimport)
56#endif
57
58namespace ctemplate {
59
60class TemplateModifier;
61class TemplateAnnotator;
62
63class CTEMPLATE_DLL_DECL PerExpandData {
64 public:
65 PerExpandData()
66 : annotate_path_(NULL),
67 annotator_(NULL),
68 expand_modifier_(NULL),
69 map_(NULL) { }
70
71 ~PerExpandData();
72
73 // Indicate that annotations should be inserted during template expansion.
74 // template_path_start - the start of a template path. When
75 // printing the filename for template-includes, anything before and
76 // including template_path_start is elided. This can make the
77 // output less dependent on filesystem location for template files.
78 void SetAnnotateOutput(const char* template_path_start) {
79 annotate_path_ = template_path_start;
80 }
81
82 // Whether to annotate the expanded output.
83 bool annotate() const { return annotate_path_ != NULL; }
84
85 // The annotate-path; undefined if annotate() != true
86 const char* annotate_path() const { return annotate_path_; }
87
88 // This sets the TemplateAnnotator to be used when annotating is on.
89 // This allows you to override the default text-based annotator
90 // that will be used if you do not call this. The passed annotator
91 // will be aliased by this object and returned by annotator().
92 // Passing NULL has the special behavior of causing annotator() to
93 // revert to returning its built-in instance.
94 void SetAnnotator(TemplateAnnotator* annotator) {
95 annotator_ = annotator;
96 }
97
98 // This returns the TemplateAnnotator to be used when annotating is on.
99 // The value returned will be either an instance previously provided
100 // to SetAnnotator() or the callable built-in text-based annotator.
101 TemplateAnnotator* annotator() const;
102
103 // This is a TemplateModifier to be applied to all templates
104 // expanded via this call to Expand(). That is, this modifier is
105 // applies to the template (.tpl) file we expand, as well as
106 // sub-templates that are expanded due to {{>INCLUDE}} directives.
107 // Caller is responsible for ensuring that modifier exists for the
108 // lifetime of this object.
109 void SetTemplateExpansionModifier(const TemplateModifier* modifier) {
110 expand_modifier_ = modifier;
111 }
112
113 const TemplateModifier* template_expansion_modifier() const {
114 return expand_modifier_;
115 }
116
117 // Store data in this structure, to be used by template modifiers
118 // (see template_modifiers.h). Call with value set to NULL to clear
119 // any value previously set. Caller is responsible for ensuring key
120 // and value point to valid data for the lifetime of this object.
121 void InsertForModifiers(const char* key, const void* value);
122
123 // Retrieve data specific to this Expand call. Returns NULL if key
124 // is not found. This should only be used by template modifiers.
125 const void* LookupForModifiers(const char* key) const;
126
127 // Same as Lookup, but casts the result to a c string.
128 const char* LookupForModifiersAsString(const char* key) const {
129 return static_cast<const char*>(LookupForModifiers(key));
130 }
131
132 private:
133#ifdef _MSC_VER
134 typedef stdext::hash_map<const char*, const void*, StringHash> DataMap;
135#else
136 struct DataEq {
137 bool operator()(const char* s1, const char* s2) const;
138 };
139 typedef stdext::hash_map<const char*, const void*, StringHash, DataEq>
140 DataMap;
141#endif
142
143 const char* annotate_path_;
144 TemplateAnnotator* annotator_;
145 const TemplateModifier* expand_modifier_;
146 DataMap* map_;
147
148 PerExpandData(const PerExpandData&); // disallow evil copy constructor
149 void operator=(const PerExpandData&); // disallow evil operator=
150};
151
152}
153
154#endif // TEMPLATE_PER_EXPAND_DATA_H_