Loading...
Searching...
No Matches
pathExpression.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_EXPRESSION_H
8#define PXR_USD_SDF_PATH_EXPRESSION_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/pathPattern.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
55{
56public:
58
65 public:
69 SDF_API
70 static ExpressionReference const &Weaker();
71
72 // Optional path reference, can be empty for "weaker" references (name
73 // is "_") or for references to local or otherwise "named" collections.
74 SdfPath path;
75
76 // Name is either a property name, or "_" (meaning the weaker
77 // collection). If the name is "_", the path must be empty.
78 std::string name;
79
80 template <class HashState>
81 friend void TfHashAppend(HashState &h, ExpressionReference const &er) {
82 h.Append(er.path, er.name);
83 }
84
85 friend bool
86 operator==(ExpressionReference const &l, ExpressionReference const &r) {
87 return std::tie(l.path, l.name) == std::tie(r.path, r.name);
88 }
89
90 friend bool
91 operator!=(ExpressionReference const &l, ExpressionReference const &r) {
92 return !(l == r);
93 }
94
95 friend void swap(ExpressionReference &l, ExpressionReference &r) {
96 auto lt = std::tie(l.path, l.name);
97 auto rt = std::tie(r.path, r.name);
98 swap(lt, rt);
99 }
100 };
101
103 enum Op {
104 // Operations on atoms.
105 Complement,
106 ImpliedUnion,
107 Union,
108 Intersection,
109 Difference,
110
111 // Atoms.
112 ExpressionRef,
113 Pattern
114 };
115
118 SdfPathExpression() = default;
119
124 SDF_API
125 explicit SdfPathExpression(std::string const &expr,
126 std::string const &parseContext = {});
127
129 SDF_API
131
134 SDF_API
136
139 SDF_API
140 static SdfPathExpression const &Nothing();
141
145 SDF_API
147
149 SDF_API
150 static SdfPathExpression
152
154 static SdfPathExpression
156 return MakeComplement(SdfPathExpression(right));
157 }
158
162 SDF_API
163 static SdfPathExpression
165
167 static SdfPathExpression
169 SdfPathExpression const &left,
170 SdfPathExpression const &right) {
171 return MakeOp(op, SdfPathExpression(left), SdfPathExpression(right));
172 }
173
175 SDF_API
176 static SdfPathExpression
178
180 static SdfPathExpression
182 return MakeAtom(ExpressionReference(ref));
183 }
184
186 SDF_API
187 static SdfPathExpression
189
191 static SdfPathExpression
192 MakeAtom(PathPattern const &pattern) {
193 return MakeAtom(PathPattern(pattern));
194 }
195
197 static SdfPathExpression
198 MakeAtom(SdfPath const &path) {
199 return MakeAtom(PathPattern(path));
200 }
201
203 static SdfPathExpression
205 return MakeAtom(PathPattern(path));
206 }
207
241 SDF_API
242 void Walk(TfFunctionRef<void (Op, int)> logic,
243 TfFunctionRef<void (ExpressionReference const &)> ref,
244 TfFunctionRef<void (PathPattern const &)> pattern) const;
245
251 SDF_API
253 TfFunctionRef<void (std::vector<std::pair<Op, int>> const &)> logic,
254 TfFunctionRef<void (ExpressionReference const &)> ref,
255 TfFunctionRef<void (PathPattern const &)> pattern) const;
256
260 ReplacePrefix(SdfPath const &oldPrefix,
261 SdfPath const &newPrefix) const & {
262 return SdfPathExpression(*this).ReplacePrefix(oldPrefix, newPrefix);
263 }
264
267 SDF_API
269 ReplacePrefix(SdfPath const &oldPrefix,
270 SdfPath const &newPrefix) &&;
271
275 SDF_API
276 bool IsAbsolute() const;
277
281 MakeAbsolute(SdfPath const &anchor) const & {
282 return SdfPathExpression(*this).MakeAbsolute(anchor);
283 }
284
287 SDF_API
289 MakeAbsolute(SdfPath const &anchor) &&;
290
294 return !_refs.empty();
295 }
296
300 SDF_API
302
311 ExpressionReference const &)> resolve) const & {
312 return SdfPathExpression(*this).ResolveReferences(resolve);
313 }
314
316 SDF_API
320 ExpressionReference const &)> resolve) &&;
321
329 ComposeOver(SdfPathExpression const &weaker) const & {
330 return SdfPathExpression(*this).ComposeOver(weaker);
331 }
332
334 SDF_API
337
348 bool IsComplete() const {
350 }
351
354 SDF_API
355 std::string GetText() const;
356
359 bool IsEmpty() const {
360 return _ops.empty();
361 }
362
364 explicit operator bool() const {
365 return !IsEmpty();
366 }
367
370 std::string const &GetParseError() const & {
371 return _parseError;
372 }
373
374private:
375 template <class HashState>
376 friend void TfHashAppend(HashState &h, SdfPathExpression const &expr) {
377 h.Append(expr._ops, expr._refs, expr._patterns, expr._parseError);
378 }
379
380 SDF_API
381 friend std::ostream &
382 operator<<(std::ostream &, SdfPathExpression const &);
383
384 friend bool
385 operator==(SdfPathExpression const &l, SdfPathExpression const &r) {
386 return std::tie(l._ops, l._refs, l._patterns, l._parseError) ==
387 std::tie(r._ops, r._refs, r._patterns, r._parseError);
388 }
389
390 friend bool
391 operator!=(SdfPathExpression const &l, SdfPathExpression const &r) {
392 return !(l == r);
393 }
394
395 friend void swap(SdfPathExpression &l, SdfPathExpression &r) {
396 auto lt = std::tie(l._ops, l._refs, l._patterns, l._parseError);
397 auto rt = std::tie(r._ops, r._refs, r._patterns, r._parseError);
398 swap(lt, rt);
399 }
400
401 std::vector<Op> _ops;
402 std::vector<ExpressionReference> _refs;
403 std::vector<PathPattern> _patterns;
404
405 // This member holds a parsing error string if this expression was
406 // constructed by the parser and errors were encountered during the parsing.
407 std::string _parseError;
408};
409
410
411PXR_NAMESPACE_CLOSE_SCOPE
412
413#endif // PXR_USD_SDF_PATH_EXPRESSION_H
Objects of this class represent references to other path expressions, which will be resolved later by...
static SDF_API ExpressionReference const & Weaker()
Return the special "weaker" reference, whose syntax in an SdfPathExpression is "%_".
Objects of this class represent a logical expression syntax tree consisting of SdfPathPattern s,...
SDF_API SdfPathExpression ReplacePrefix(SdfPath const &oldPrefix, SdfPath const &newPrefix) &&
Return a new expression created by replacing literal path prefixes that start with oldPrefix with new...
static SDF_API SdfPathExpression MakeAtom(ExpressionReference &&ref)
Produce a new expression containing only the reference ref.
SDF_API bool IsAbsolute() const
Return true if all contained pattern prefixes are absolute, false otherwise.
static SDF_API SdfPathExpression const & EveryDescendant()
Return the relative expression ".//" which matches all paths descendant to an anchor path.
static SdfPathExpression MakeAtom(ExpressionReference const &ref)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API SdfPathExpression MakeAbsolute(SdfPath const &anchor) &&
Return a new expression created by making any relative path prefixes in this expression absolute by S...
bool IsComplete() const
Return true if this expression is considered "complete".
static SDF_API SdfPathExpression MakeAtom(PathPattern &&pattern)
Produce a new expression containing only the pattern pattern.
SDF_API std::string GetText() const
Return a text representation of this expression that parses to the same expression.
SDF_API SdfPathExpression(std::string const &expr, std::string const &parseContext={})
Construct an expression by parsing expr.
static SdfPathExpression MakeAtom(PathPattern const &pattern)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Op
Enumerant describing a subexpression operation.
static SDF_API SdfPathExpression const & Nothing()
Return the empty expression which matches no paths.
SdfPathExpression ComposeOver(SdfPathExpression const &weaker) const &
Return a new expression created by replacing references to the "weaker expression" (i....
SdfPathExpression ReplacePrefix(SdfPath const &oldPrefix, SdfPath const &newPrefix) const &
Return a new expression created by replacing literal path prefixes that start with oldPrefix with new...
static SDF_API SdfPathExpression MakeComplement(SdfPathExpression &&right)
Produce a new expression representing the set-complement of right.
SdfPathExpression()=default
Default construction produces the "empty" expression.
static SDF_API SdfPathExpression const & Everything()
Return the expression "//" which matches all paths.
bool IsEmpty() const
Return true if this is the empty expression; i.e.
SdfPathExpression MakeAbsolute(SdfPath const &anchor) const &
Return a new expression created by making any relative path prefixes in this expression absolute by S...
static SdfPathExpression MakeOp(Op op, SdfPathExpression const &left, SdfPathExpression const &right)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool ContainsExpressionReferences() const
Return true if this expression contains any references to other collections.
SDF_API void Walk(TfFunctionRef< void(Op, int)> logic, TfFunctionRef< void(ExpressionReference const &)> ref, TfFunctionRef< void(PathPattern const &)> pattern) const
Walk this expression's syntax tree in depth-first order, calling pattern with the current PathPattern...
SDF_API SdfPathExpression ComposeOver(SdfPathExpression const &weaker) &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
static SdfPathExpression MakeAtom(SdfPath const &path)
Produce a new expression that matches path exactly.
std::string const & GetParseError() const &
Return parsing errors as a string if this function was constructed from a string and parse errors wer...
static SdfPathExpression MakeAtom(SdfPath &&path)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static SDF_API SdfPathExpression MakeOp(Op op, SdfPathExpression &&left, SdfPathExpression &&right)
Produce a new expression representing the set-algebraic operation op with operands left and right.
static SDF_API SdfPathExpression const & WeakerRef()
Return the expression "%_", consisting solely of a reference to the "weaker" path expression,...
SDF_API void WalkWithOpStack(TfFunctionRef< void(std::vector< std::pair< Op, int > > const &)> logic, TfFunctionRef< void(ExpressionReference const &)> ref, TfFunctionRef< void(PathPattern const &)> pattern) const
Equivalent to Walk(), except that the logic function is called with a const reference to the current ...
SDF_API SdfPathExpression ResolveReferences(TfFunctionRef< SdfPathExpression(ExpressionReference const &)> resolve) &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
static SdfPathExpression MakeComplement(SdfPathExpression const &right)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SdfPathExpression ResolveReferences(TfFunctionRef< SdfPathExpression(ExpressionReference const &)> resolve) const &
Return a new expression created by resolving collection references in this expression.
SDF_API bool ContainsWeakerExpressionReference() const
Return true if this expression contains one or more "weaker" expression references,...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Objects of this class represent SdfPath matching patterns, consisting of an SdfPath prefix followed b...
Definition: pathPattern.h:31
This class provides a non-owning reference to a type-erased callable object with a specified signatur...
Definition: functionRef.h:19