All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
templateString.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef TF_TEMPLATE_STRING_H
25 #define TF_TEMPLATE_STRING_H
26 
28 
29 #include "pxr/pxr.h"
30 
31 #include "pxr/base/tf/api.h"
32 
33 #include <tbb/spin_mutex.h>
34 
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
64 public:
65  typedef std::map<std::string, std::string> Mapping;
66 
68  TF_API
70 
72  TF_API
73  TfTemplateString(const std::string& template_);
74 
76  const std::string& GetTemplate() const { return _data->template_; }
77 
82  TF_API
83  std::string Substitute(const Mapping&) const;
84 
88  TF_API
89  std::string SafeSubstitute(const Mapping&) const;
90 
93  TF_API
94  Mapping GetEmptyMapping() const;
95 
98  TF_API
99  bool IsValid() const;
100 
102  TF_API
103  std::vector<std::string> GetParseErrors() const;
104 
105 private:
106  struct _PlaceHolder {
107  _PlaceHolder(const std::string& n, size_t p, size_t l)
108  : name(n), pos(p), len(l) {}
109  std::string name;
110  size_t pos;
111  size_t len;
112  };
113 
114  // Returns a new string constructed by performing token replacement.
115  std::string _Evaluate(const Mapping&, std::vector<std::string>* = 0) const;
116 
117  // Finds the next token-like substring in the template and returns the
118  // token name, position and length. For token ${foo}, the name is 'foo',
119  // position is the position of '$' and length is the length of the entire
120  // token including optional enclosing braces.
121  bool _FindNextPlaceHolder(size_t*, std::vector<std::string>*) const;
122 
123  // Find all tokens in the template string and store away naming and
124  // position information.
125  void _ParseTemplate() const;
126 
127  // Emit any parse errors as TF_CODING_ERRORs.
128  void _EmitParseErrors() const;
129 
130  // Structure side-allocated and shared between copies.
131  struct _Data
132  {
133  _Data(_Data const &) = delete;
134  _Data &operator=(_Data const &) = delete;
135 
136  _Data() : parsed(false) {}
137 
138  std::string template_;
139  mutable std::vector<_PlaceHolder> placeholders;
140  mutable bool parsed;
141  mutable std::vector<std::string> parseErrors;
142  mutable tbb::spin_mutex mutex;
143  };
144 
145  std::shared_ptr<_Data> _data;
146 };
147 
148 PXR_NAMESPACE_CLOSE_SCOPE
149 
150 #endif // TF_TEMPLATE_STRING_H
TF_API std::vector< std::string > GetParseErrors() const
Returns any error messages generated during template parsing.
const std::string & GetTemplate() const
Returns the template source string supplied to the constructor.
TF_API std::string SafeSubstitute(const Mapping &) const
Like Substitute(), except that if placeholders are missing from the mapping, instead of raising a cod...
TF_API TfTemplateString()
Constructs a new template string.
TF_API std::string Substitute(const Mapping &) const
Performs the template substitution, returning a new string.
TF_API Mapping GetEmptyMapping() const
Returns an empty mapping for the current template.
TF_API bool IsValid() const
Returns true if the current template is well formed.
TfTemplateString provides simple string substitutions based on named placeholders.