HDK
 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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_TEMPLATE_STRING_H
8 #define PXR_BASE_TF_TEMPLATE_STRING_H
9 
10 /// \file tf/templateString.h
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/base/tf/api.h"
15 
16 #include <tbb/spin_mutex.h>
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
24 
25 /// \class TfTemplateString
26 /// \ingroup group_tf_String
27 ///
28 /// TfTemplateString provides simple string substitutions based on named
29 /// placeholders. Instead of the '%'-based substitutions used by printf,
30 /// template strings use '$'-based substitutions, using the following rules:
31 ///
32 /// \li "$$" is replaced with a single "$"
33 /// \li "$identifier" names a substitution placeholder matching a mapping key
34 /// of "identifier". The first non-identifier character after the "$"
35 /// character terminates the placeholder specification.
36 /// \li "${identifier}" is equivalent to "$identifier". It is required when
37 /// valid identifier characters follow the placeholder but are not part of the
38 /// placeholder, such as "${noun}ification".
39 /// \li An identifier is a sequence of characters "[A-Z][a-z][0-9]_".
40 ///
41 /// \a TfTemplateString is immutable: once one is created it may not be
42 /// modified. \a TfTemplateString is fast to copy, since it shares state
43 /// internally between copies. \a TfTemplateString is thread-safe. It may be
44 /// read freely by multiple threads concurrently.
45 ///
47 public:
48  typedef std::map<std::string, std::string> Mapping;
49 
50  /// Constructs a new template string.
51  TF_API
53 
54  /// Constructs a new template string.
55  TF_API
56  TfTemplateString(const std::string& template_);
57 
58  /// Returns the template source string supplied to the constructor.
59  const std::string& GetTemplate() const { return _data->template_; }
60 
61  /// Performs the template substitution, returning a new string. The mapping
62  /// contains keys which match the placeholders in the template. If a
63  /// placeholder is found for which no mapping is present, a coding error is
64  /// raised.
65  TF_API
66  std::string Substitute(const Mapping&) const;
67 
68  /// Like Substitute(), except that if placeholders are missing from the
69  /// mapping, instead of raising a coding error, the original placeholder
70  /// will appear in the resulting string intact.
71  TF_API
72  std::string SafeSubstitute(const Mapping&) const;
73 
74  /// Returns an empty mapping for the current template. This method first
75  /// calls IsValid to ensure that the template is valid.
76  TF_API
77  Mapping GetEmptyMapping() const;
78 
79  /// Returns true if the current template is well formed. Empty templates are
80  /// valid.
81  TF_API
82  bool IsValid() const;
83 
84  /// Returns any error messages generated during template parsing.
85  TF_API
86  std::vector<std::string> GetParseErrors() const;
87 
88 private:
89  struct _PlaceHolder {
90  _PlaceHolder(const std::string& n, size_t p, size_t l)
91  : name(n), pos(p), len(l) {}
92  std::string name;
93  size_t pos;
94  size_t len;
95  };
96 
97  // Returns a new string constructed by performing token replacement.
98  std::string _Evaluate(const Mapping&, std::vector<std::string>* = 0) const;
99 
100  // Finds the next token-like substring in the template and returns the
101  // token name, position and length. For token ${foo}, the name is 'foo',
102  // position is the position of '$' and length is the length of the entire
103  // token including optional enclosing braces.
104  bool _FindNextPlaceHolder(size_t*, std::vector<std::string>*) const;
105 
106  // Find all tokens in the template string and store away naming and
107  // position information.
108  void _ParseTemplate() const;
109 
110  // Emit any parse errors as TF_CODING_ERRORs.
111  void _EmitParseErrors() const;
112 
113  // Structure side-allocated and shared between copies.
114  struct _Data
115  {
116  _Data(_Data const &) = delete;
117  _Data &operator=(_Data const &) = delete;
118 
119  _Data() : parsed(false) {}
120 
121  std::string template_;
122  mutable std::vector<_PlaceHolder> placeholders;
123  mutable bool parsed;
124  mutable std::vector<std::string> parseErrors;
125  mutable tbb::spin_mutex mutex;
126  };
127 
128  std::shared_ptr<_Data> _data;
129 };
130 
132 
133 #endif // PXR_BASE_TF_TEMPLATE_STRING_H
#define TF_API
Definition: api.h:23
TF_API std::string SafeSubstitute(const Mapping &) const
TF_API TfTemplateString()
Constructs a new template string.
const std::string & GetTemplate() const
Returns the template source string supplied to the constructor.
std::map< std::string, std::string > Mapping
GLdouble n
Definition: glcorearb.h:2008
TF_API bool IsValid() const
GLuint const GLchar * name
Definition: glcorearb.h:786
TF_API std::vector< std::string > GetParseErrors() const
Returns any error messages generated during template parsing.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
LeafData & operator=(const LeafData &)=delete
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TF_API Mapping GetEmptyMapping() const
TF_API std::string Substitute(const Mapping &) const