All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
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
23PXR_NAMESPACE_OPEN_SCOPE
24
47public:
48 typedef std::map<std::string, std::string> Mapping;
49
51 TF_API
53
55 TF_API
56 TfTemplateString(const std::string& template_);
57
59 const std::string& GetTemplate() const { return _data->template_; }
60
65 TF_API
66 std::string Substitute(const Mapping&) const;
67
71 TF_API
72 std::string SafeSubstitute(const Mapping&) const;
73
76 TF_API
77 Mapping GetEmptyMapping() const;
78
81 TF_API
82 bool IsValid() const;
83
85 TF_API
86 std::vector<std::string> GetParseErrors() const;
87
88private:
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
131PXR_NAMESPACE_CLOSE_SCOPE
132
133#endif // PXR_BASE_TF_TEMPLATE_STRING_H
TfTemplateString provides simple string substitutions based on named placeholders.
TF_API TfTemplateString()
Constructs a new template string.
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 std::string Substitute(const Mapping &) const
Performs the template substitution, returning a new string.
TF_API bool IsValid() const
Returns true if the current template is well formed.
TF_API Mapping GetEmptyMapping() const
Returns an empty mapping for the current template.
const std::string & GetTemplate() const
Returns the template source string supplied to the constructor.
TF_API TfTemplateString(const std::string &template_)
Constructs a new template string.
TF_API std::vector< std::string > GetParseErrors() const
Returns any error messages generated during template parsing.