This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pathPattern.h
1//
2// Copyright 2023 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_USD_SDF_PATH_PATTERN_H
8#define PXR_USD_SDF_PATH_PATTERN_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
12#include "pxr/usd/sdf/path.h"
13#include "pxr/usd/sdf/predicateExpression.h"
14#include "pxr/base/tf/hash.h"
15
16#include <iosfwd>
17#include <string>
18#include <tuple>
19#include <utility>
20#include <vector>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
31{
32public:
35 SDF_API
37
39 SDF_API
40 explicit SdfPathPattern(SdfPath const &prefix);
41
43 SDF_API
44 explicit SdfPathPattern(SdfPath &&prefix);
45
47 SDF_API
48 static SdfPathPattern const &Everything();
49
52 SDF_API
54
57 return {};
58 }
59
66 struct Component {
67 bool IsStretch() const {
68 return predicateIndex == -1 && text.empty();
69 }
70
71 std::string text;
72 int predicateIndex = -1;
73 bool isLiteral = false;
74
75 friend bool operator==(Component const &l, Component const &r) {
76 return std::tie(l.text, l.predicateIndex, l.isLiteral) ==
77 std::tie(r.text, r.predicateIndex, r.isLiteral);
78 }
79
80 friend bool operator!=(Component const &l, Component const &r) {
81 return !(l == r);
82 }
83
84 template <class HashState>
85 friend void TfHashAppend(HashState &h, Component const &c) {
86 h.Append(c.text, c.predicateIndex, c.isLiteral);
87 }
88
89 friend void swap(Component &l, Component &r) {
90 auto lt = std::tie(l.text, l.predicateIndex, l.isLiteral);
91 auto rt = std::tie(r.text, r.predicateIndex, r.isLiteral);
92 swap(lt, rt);
93 }
94 };
95
99 bool CanAppendChild(std::string const &text,
100 std::string *reason = nullptr) const {
101 return CanAppendChild(text, {}, reason);
102 }
103
107 SDF_API
108 bool CanAppendChild(std::string const &text,
109 SdfPredicateExpression const &predExpr,
110 std::string *reason = nullptr) const;
111
119 SDF_API
120 SdfPathPattern &AppendChild(std::string const &text,
121 SdfPredicateExpression &&predExpr);
123 SDF_API
124 SdfPathPattern &AppendChild(std::string const &text,
125 SdfPredicateExpression const &predExpr);
127 SDF_API
128 SdfPathPattern &AppendChild(std::string const &text);
129
133 bool CanAppendProperty(std::string const &text,
134 std::string *reason = nullptr) const {
135 return CanAppendProperty(text, {}, reason);
136 }
137
141 SDF_API
142 bool CanAppendProperty(std::string const &text,
143 SdfPredicateExpression const &predExpr,
144 std::string *reason = nullptr) const;
145
153 SDF_API
154 SdfPathPattern &AppendProperty(std::string const &text,
155 SdfPredicateExpression &&predExpr);
157 SDF_API
158 SdfPathPattern &AppendProperty(std::string const &text,
159 SdfPredicateExpression const &predExpr);
161 SDF_API
162 SdfPathPattern &AppendProperty(std::string const &text);
163
166 SdfPath const &GetPrefix() const & {
167 return _prefix;
168 }
169
172 return std::move(_prefix);
173 }
174
177 SDF_API
179
182 return SetPrefix(SdfPath(p));
183 }
184
188 SDF_API
189 bool HasLeadingStretch() const;
190
193 SDF_API
194 bool HasTrailingStretch() const;
195
200 SDF_API
202
205 SDF_API
207
211 SDF_API
213
215 SDF_API
216 std::string GetText() const;
217
220 std::vector<Component> const &GetComponents() const & {
221 return _components;
222 }
223
225 std::vector<Component> GetComponents() && {
226 return std::move(_components);
227 }
228
231 std::vector<SdfPredicateExpression> const &
233 return _predExprs;
234 }
235
237 std::vector<SdfPredicateExpression>
239 return std::move(_predExprs);
240 }
241
244 bool IsProperty() const {
245 return _isProperty;
246 }
247
249 explicit operator bool() const {
250 return !_prefix.IsEmpty();
251 }
252
253private:
254 SdfPathPattern(SdfPath prefix,
255 std::vector<Component> &&components,
256 std::vector<SdfPredicateExpression> &&predExprs,
257 bool isProperty);
258
259 template <class HashState>
260 friend void TfHashAppend(HashState &h, SdfPathPattern const &pat) {
261 h.Append(pat._prefix, pat._components,
262 pat._predExprs, pat._isProperty);
263 }
264
265 friend bool
266 operator==(SdfPathPattern const &l, SdfPathPattern const &r) {
267 return std::tie(l._prefix, l._components,
268 l._predExprs, l._isProperty) ==
269 std::tie(r._prefix, r._components,
270 r._predExprs, r._isProperty);
271 }
272
273 friend bool
274 operator!=(SdfPathPattern const &l, SdfPathPattern const &r) {
275 return !(l == r);
276 }
277
278 friend void swap(SdfPathPattern &l, SdfPathPattern &r) {
279 auto lt = std::tie(
280 l._prefix, l._components, l._predExprs, l._isProperty);
281 auto rt = std::tie(
282 r._prefix, r._components, r._predExprs, r._isProperty);
283 swap(lt, rt);
284 }
285
286 SdfPath _prefix;
287 std::vector<Component> _components;
288 std::vector<SdfPredicateExpression> _predExprs;
289 bool _isProperty;
290};
291
292
293PXR_NAMESPACE_CLOSE_SCOPE
294
295#endif // PXR_USD_SDF_PATH_PATTERN_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
Objects of this class represent SdfPath matching patterns, consisting of an SdfPath prefix followed b...
Definition: pathPattern.h:31
bool IsProperty() const
Return true if this pattern identifies properties exclusively, false otherwise.
Definition: pathPattern.h:244
SDF_API SdfPathPattern & AppendChild(std::string const &text)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API SdfPathPattern()
Construct the empty pattern whose bool-conversion operator returns false.
bool CanAppendProperty(std::string const &text, std::string *reason=nullptr) const
Return true if it is valid to append the property element text to this pattern.
Definition: pathPattern.h:133
SDF_API SdfPathPattern(SdfPath &&prefix)
Construct a SdfPathPattern with the prefix path.
SDF_API std::string GetText() const
Return the string representation of this pattern.
bool CanAppendChild(std::string const &text, std::string *reason=nullptr) const
Return true if it is valid to append the child element text to this pattern.
Definition: pathPattern.h:99
SdfPathPattern & SetPrefix(SdfPath const &p)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: pathPattern.h:181
SDF_API SdfPathPattern & RemoveTrailingComponent()
If this pattern has components, remove the final component.
std::vector< SdfPredicateExpression > GetPredicateExprs() &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: pathPattern.h:238
SDF_API bool CanAppendChild(std::string const &text, SdfPredicateExpression const &predExpr, std::string *reason=nullptr) const
Return true if it is valid to append the child element text and predExpr to this pattern.
SDF_API SdfPathPattern & AppendProperty(std::string const &text, SdfPredicateExpression &&predExpr)
Append a prim property component to this pattern, with optional predicate expression predExpr.
static SDF_API SdfPathPattern const & Everything()
Return the pattern "//" which matches all paths.
std::vector< Component > GetComponents() &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: pathPattern.h:225
SDF_API SdfPathPattern & AppendChild(std::string const &text, SdfPredicateExpression const &predExpr)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API SdfPathPattern & SetPrefix(SdfPath &&p)
Set this pattern's non-speculative prefix (leading path components with no wildcards and no predicate...
SDF_API SdfPathPattern & AppendChild(std::string const &text, SdfPredicateExpression &&predExpr)
Append a prim child component to this pattern, with optional predicate expression predExpr.
SDF_API SdfPathPattern & AppendProperty(std::string const &text, SdfPredicateExpression const &predExpr)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< Component > const & GetComponents() const &
Return this pattern's components that follow its non-speculative prefix path.
Definition: pathPattern.h:220
SDF_API SdfPathPattern & RemoveTrailingStretch()
Remove trailing stretch from this pattern if it has trailing stretch.
SdfPath const & GetPrefix() const &
Return this pattern's non-speculative prefix (leading path components with no wildcards and no predic...
Definition: pathPattern.h:166
std::vector< SdfPredicateExpression > const & GetPredicateExprs() const &
Return the predicate expressions used by this pattern.
Definition: pathPattern.h:232
SDF_API bool HasTrailingStretch() const
Return true if this pattern ends with a stretch component: //, false otherwise.
static SdfPathPattern Nothing()
Return a default constructed SdfPathPattern that matches nothing.
Definition: pathPattern.h:56
SDF_API SdfPathPattern & AppendStretchIfPossible()
Append a stretch component (i.e.
SDF_API SdfPathPattern(SdfPath const &prefix)
Construct a SdfPathPattern with the prefix path.
static SDF_API SdfPathPattern const & EveryDescendant()
Return the pattern ".//" which matches all paths descendant to an anchor path.
SDF_API bool HasLeadingStretch() const
Return true if this pattern's prefix is the absolute root path and and its first component is a stret...
SDF_API SdfPathPattern & AppendProperty(std::string const &text)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API bool CanAppendProperty(std::string const &text, SdfPredicateExpression const &predExpr, std::string *reason=nullptr) const
Return true if it is valid to append the property element text and predExpr to this pattern.
SdfPath GetPrefix() &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: pathPattern.h:171
Represents a logical expression syntax tree consisting of predicate function calls joined by the logi...
A component represents a pattern matching component past the initial SdfPath prefix.
Definition: pathPattern.h:66