|
| 1 | +// 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: [email protected] (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 <map> |
| 49 | +#include <ctemplate/template_string.h> // for StringHash |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +namespace ctemplate { |
| 54 | + |
| 55 | +class TemplateModifier; |
| 56 | +class TemplateAnnotator; |
| 57 | + |
| 58 | +class PerExpandData { |
| 59 | + public: |
| 60 | + PerExpandData() |
| 61 | + : annotate_path_(NULL), |
| 62 | + annotator_(NULL), |
| 63 | + expand_modifier_(NULL), |
| 64 | + map_(NULL) { } |
| 65 | + |
| 66 | + ~PerExpandData(); |
| 67 | + |
| 68 | + // Indicate that annotations should be inserted during template expansion. |
| 69 | + // template_path_start - the start of a template path. When |
| 70 | + // printing the filename for template-includes, anything before and |
| 71 | + // including template_path_start is elided. This can make the |
| 72 | + // output less dependent on filesystem location for template files. |
| 73 | + void SetAnnotateOutput(const char* template_path_start) { |
| 74 | + annotate_path_ = template_path_start; |
| 75 | + } |
| 76 | + |
| 77 | + // Whether to annotate the expanded output. |
| 78 | + bool annotate() const { return annotate_path_ != NULL; } |
| 79 | + |
| 80 | + // The annotate-path; undefined if annotate() != true |
| 81 | + const char* annotate_path() const { return annotate_path_; } |
| 82 | + |
| 83 | + // This sets the TemplateAnnotator to be used when annotating is on. |
| 84 | + // This allows you to override the default text-based annotator |
| 85 | + // that will be used if you do not call this. The passed annotator |
| 86 | + // will be aliased by this object and returned by annotator(). |
| 87 | + // Passing NULL has the special behavior of causing annotator() to |
| 88 | + // revert to returning its built-in instance. |
| 89 | + void SetAnnotator(TemplateAnnotator* annotator) { |
| 90 | + annotator_ = annotator; |
| 91 | + } |
| 92 | + |
| 93 | + // This returns the TemplateAnnotator to be used when annotating is on. |
| 94 | + // The value returned will be either an instance previously provided |
| 95 | + // to SetAnnotator() or the callable built-in text-based annotator. |
| 96 | + TemplateAnnotator* annotator() const; |
| 97 | + |
| 98 | + // This is a TemplateModifier to be applied to all templates |
| 99 | + // expanded via this call to Expand(). That is, this modifier is |
| 100 | + // applies to the template (.tpl) file we expand, as well as |
| 101 | + // sub-templates that are expanded due to {{>INCLUDE}} directives. |
| 102 | + // Caller is responsible for ensuring that modifier exists for the |
| 103 | + // lifetime of this object. |
| 104 | + void SetTemplateExpansionModifier(const TemplateModifier* modifier) { |
| 105 | + expand_modifier_ = modifier; |
| 106 | + } |
| 107 | + |
| 108 | + const TemplateModifier* template_expansion_modifier() const { |
| 109 | + return expand_modifier_; |
| 110 | + } |
| 111 | + |
| 112 | + // Store data in this structure, to be used by template modifiers |
| 113 | + // (see template_modifiers.h). Call with value set to NULL to clear |
| 114 | + // any value previously set. Caller is responsible for ensuring key |
| 115 | + // and value point to valid data for the lifetime of this object. |
| 116 | + void InsertForModifiers(const char* key, const void* value); |
| 117 | + |
| 118 | + // Retrieve data specific to this Expand call. Returns NULL if key |
| 119 | + // is not found. This should only be used by template modifiers. |
| 120 | + const void* LookupForModifiers(const char* key) const; |
| 121 | + |
| 122 | + // Same as Lookup, but casts the result to a c string. |
| 123 | + const char* LookupForModifiersAsString(const char* key) const { |
| 124 | + return static_cast<const char*>(LookupForModifiers(key)); |
| 125 | + } |
| 126 | + |
| 127 | + private: |
| 128 | +#ifdef _MSC_VER |
| 129 | + typedef std::map<const char*, const void*, StringHash> DataMap; |
| 130 | +#else |
| 131 | + struct DataEq { |
| 132 | + bool operator()(const char* s1, const char* s2) const; |
| 133 | + }; |
| 134 | + typedef std::map<const char*, const void*, StringHash> |
| 135 | + DataMap; |
| 136 | +#endif |
| 137 | + |
| 138 | + const char* annotate_path_; |
| 139 | + TemplateAnnotator* annotator_; |
| 140 | + const TemplateModifier* expand_modifier_; |
| 141 | + DataMap* map_; |
| 142 | + |
| 143 | + PerExpandData(const PerExpandData&); // disallow evil copy constructor |
| 144 | + void operator=(const PerExpandData&); // disallow evil operator= |
| 145 | +}; |
| 146 | + |
| 147 | +} |
| 148 | + |
| 149 | +#endif // TEMPLATE_PER_EXPAND_DATA_H_ |
0 commit comments