blob: e7daf6d01397618e0f80054b360e2acb08633af7 [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// Copyright (c) 2009, 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//
32// The template expansion system provides a set of hooks that allow for
33// the insertion of diagnostic content into the output stream for the use
34// by content developers and testers. For instance, the default text
35// annotation mode inserts strings bracketed by {{ }} to describe every
36// occurrence of a dynamic substitution feature. That mode turns the
37// rendering into a logical text description of the construction of
38// template-based output. It is useful for regression testing of output
39// in conjunction with text-based diffing tools.
40//
41// An annotation mode is implemented with the TemplateAnnotator interface.
42// When template annotation is turned on, then during template expansion
43// whenever a dynamic substitution feature is encountered, a call is made
44// to one of the TemplateAnnotator functions. In response to a call
45// an implementation can render any additional content into the passed
46// emitter, which is the same emitter that the rendering output is going
47// to.
48//
49// Template annotation is turned on and the template annotator subclass
50// set by methods in ctemplate::PerExpandData.
51
52#ifndef TEMPLATE_TEMPLATE_ANNOTATOR_H_
53#define TEMPLATE_TEMPLATE_ANNOTATOR_H_
54
55#include <string>
56
57// NOTE: if you are statically linking the template library into your binary
58// (rather than using the template .dll), set '/D CTEMPLATE_DLL_DECL='
59// as a compiler flag in your project file to turn off the dllimports.
60#ifndef CTEMPLATE_DLL_DECL
61# define CTEMPLATE_DLL_DECL __declspec(dllimport)
62#endif
63
64namespace ctemplate {
65
66class ExpandEmitter;
67
68// This is the abstract interface for an annotation mode. A new annotation
69// mode is introduced by subclassing and implementing each function
70// to add annotation content. There is one function for each internal
71// template expansion event type. The emitter argument passed to the
72// function is the same stream that the expanding content is being output to;
73// so the action of an implementation will be to add additional inline
74// content. The emitter argument is never to be remembered beyond each
75// function call.
76class CTEMPLATE_DLL_DECL TemplateAnnotator {
77 public:
78 TemplateAnnotator() { }
79 virtual ~TemplateAnnotator() { }
80
81 // Called before processing a subtemplate include marker.
82 // Passed value is the include marker name.
83 virtual void EmitOpenInclude(ExpandEmitter* emitter, const std::string& value) = 0;
84 // Called after processing a subtemplate include marker.
85 virtual void EmitCloseInclude(ExpandEmitter* emitter) = 0;
86
87 // Called before opening a template or subtemplate file for processing.
88 // Passed value is the filename.
89 virtual void EmitOpenFile(ExpandEmitter* emitter, const std::string& value) = 0;
90 // Called after processing a template or subtemplate file.
91 virtual void EmitCloseFile(ExpandEmitter* emitter) = 0;
92
93 // Called before processing a section.
94 // Passed value is the section name.
95 virtual void EmitOpenSection(ExpandEmitter* emitter, const std::string& value) = 0;
96 // Called after processing a section.
97 virtual void EmitCloseSection(ExpandEmitter* emitter) = 0;
98
99 // Called before processing a variable marker.
100 // Passed value is the variable name.
101 virtual void EmitOpenVariable(ExpandEmitter* emitter,
102 const std::string& value) = 0;
103 // Called after processing a variable marker.
104 virtual void EmitCloseVariable(ExpandEmitter* emitter) = 0;
105
106 virtual void EmitFileIsMissing(ExpandEmitter* emitter,
107 const std::string& value) = 0;
108
109 private:
110 // Can't invoke copy constructor or assignment operator
111 TemplateAnnotator(const TemplateAnnotator&);
112 void operator=(const TemplateAnnotator&);
113};
114
115// This is a concrete template annotator class that inserts annotations
116// that have a standard text form bracketed by {{ }}. It is used as
117// the default annotation implementation when annotation is turned on
118// by PerExpandData and no annotator type is specified.
119class CTEMPLATE_DLL_DECL TextTemplateAnnotator : public TemplateAnnotator {
120 public:
121 TextTemplateAnnotator() { }
122 virtual void EmitOpenInclude(ExpandEmitter* emitter, const std::string& value);
123 virtual void EmitCloseInclude(ExpandEmitter* emitter);
124 virtual void EmitOpenFile(ExpandEmitter* emitter, const std::string& value);
125 virtual void EmitCloseFile(ExpandEmitter* emitter);
126 virtual void EmitOpenSection(ExpandEmitter* emitter, const std::string& value);
127 virtual void EmitCloseSection(ExpandEmitter* emitter);
128 virtual void EmitOpenVariable(ExpandEmitter* emitter, const std::string& value);
129 virtual void EmitCloseVariable(ExpandEmitter* emitter);
130 virtual void EmitFileIsMissing(ExpandEmitter* emitter,
131 const std::string& value);
132
133 private:
134 // Can't invoke copy constructor or assignment operator
135 TextTemplateAnnotator(const TextTemplateAnnotator&);
136 void operator=(const TextTemplateAnnotator&);
137};
138
139}
140
141
142#endif // TEMPLATE_TEMPLATE_ANNOTATOR_H_