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#include "pxr/base/vt/value.h"
16#include "pxr/base/vt/traits.h"
17
18#include <iosfwd>
19#include <string>
20#include <tuple>
21#include <utility>
22#include <vector>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
57{
58public:
60
67 public:
71 SDF_API
72 static ExpressionReference const &Weaker();
73
74 // Optional path reference, can be empty for "weaker" references (name
75 // is "_") or for references to local or otherwise "named" collections.
76 SdfPath path;
77
78 // Name is either a property name, or "_" (meaning the weaker
79 // collection). If the name is "_", the path must be empty.
80 std::string name;
81
82 template <class HashState>
83 friend void TfHashAppend(HashState &h, ExpressionReference const &er) {
84 h.Append(er.path, er.name);
85 }
86
87 friend bool
88 operator==(ExpressionReference const &l, ExpressionReference const &r) {
89 return std::tie(l.path, l.name) == std::tie(r.path, r.name);
90 }
91
92 friend bool
93 operator!=(ExpressionReference const &l, ExpressionReference const &r) {
94 return !(l == r);
95 }
96
97 friend void swap(ExpressionReference &l, ExpressionReference &r) {
98 auto lt = std::tie(l.path, l.name);
99 auto rt = std::tie(r.path, r.name);
100 swap(lt, rt);
101 }
102 };
103
105 enum Op {
106 // Operations on atoms.
107 Complement,
108 ImpliedUnion,
109 Union,
110 Intersection,
111 Difference,
112
113 // Atoms.
114 ExpressionRef,
115 Pattern
116 };
117
120 SdfPathExpression() = default;
121
126 SDF_API
127 explicit SdfPathExpression(std::string const &expr,
128 std::string const &parseContext = {});
129
131 SDF_API
133
136 SDF_API
138
141 SDF_API
142 static SdfPathExpression const &Nothing();
143
147 SDF_API
149
151 SDF_API
152 static SdfPathExpression
154
156 static SdfPathExpression
158 return MakeComplement(SdfPathExpression(right));
159 }
160
164 SDF_API
165 static SdfPathExpression
167
169 static SdfPathExpression
171 SdfPathExpression const &left,
172 SdfPathExpression const &right) {
173 return MakeOp(op, SdfPathExpression(left), SdfPathExpression(right));
174 }
175
177 SDF_API
178 static SdfPathExpression
180
182 static SdfPathExpression
184 return MakeAtom(ExpressionReference(ref));
185 }
186
188 SDF_API
189 static SdfPathExpression
191
193 static SdfPathExpression
194 MakeAtom(PathPattern const &pattern) {
195 return MakeAtom(PathPattern(pattern));
196 }
197
199 static SdfPathExpression
200 MakeAtom(SdfPath const &path) {
201 return MakeAtom(PathPattern(path));
202 }
203
205 static SdfPathExpression
207 return MakeAtom(PathPattern(path));
208 }
209
243 SDF_API
244 void Walk(TfFunctionRef<void (Op, int)> logic,
245 TfFunctionRef<void (ExpressionReference const &)> ref,
246 TfFunctionRef<void (PathPattern const &)> pattern) const;
247
253 SDF_API
255 TfFunctionRef<void (std::vector<std::pair<Op, int>> const &)> logic,
256 TfFunctionRef<void (ExpressionReference const &)> ref,
257 TfFunctionRef<void (PathPattern const &)> pattern) const;
258
264 ReplacePrefix(SdfPath const &oldPrefix,
265 SdfPath const &newPrefix) const & {
266 return SdfPathExpression(*this).ReplacePrefix(oldPrefix, newPrefix);
267 }
268
273 SDF_API
275 ReplacePrefix(SdfPath const &oldPrefix,
276 SdfPath const &newPrefix) &&;
277
281 SDF_API
282 bool IsAbsolute() const;
283
287 MakeAbsolute(SdfPath const &anchor) const & {
288 return SdfPathExpression(*this).MakeAbsolute(anchor);
289 }
290
293 SDF_API
295 MakeAbsolute(SdfPath const &anchor) &&;
296
300 return !_refs.empty();
301 }
302
306 SDF_API
308
317 ExpressionReference const &)> resolve) const & {
318 return SdfPathExpression(*this).ResolveReferences(resolve);
319 }
320
322 SDF_API
326 ExpressionReference const &)> resolve) &&;
327
334 ComposeOver(SdfPathExpression const &weaker) const & {
335 return SdfPathExpression(*this).ComposeOver(weaker);
336 }
337
339 SDF_API
342
353 bool IsComplete() const {
355 }
356
359 SDF_API
360 std::string GetText() const;
361
364 bool IsEmpty() const {
365 return _ops.empty();
366 }
367
369 explicit operator bool() const {
370 return !IsEmpty();
371 }
372
375 std::string const &GetParseError() const & {
376 return _parseError;
377 }
378
379private:
380 template <class HashState>
381 friend void TfHashAppend(HashState &h, SdfPathExpression const &expr) {
382 h.Append(expr._ops, expr._refs, expr._patterns, expr._parseError);
383 }
384
385 SDF_API
386 friend std::ostream &
387 operator<<(std::ostream &, SdfPathExpression const &);
388
389 friend bool
390 operator==(SdfPathExpression const &l, SdfPathExpression const &r) {
391 return std::tie(l._ops, l._refs, l._patterns, l._parseError) ==
392 std::tie(r._ops, r._refs, r._patterns, r._parseError);
393 }
394
395 friend bool
396 operator!=(SdfPathExpression const &l, SdfPathExpression const &r) {
397 return !(l == r);
398 }
399
400 friend void swap(SdfPathExpression &l, SdfPathExpression &r) {
401 auto lt = std::tie(l._ops, l._refs, l._patterns, l._parseError);
402 auto rt = std::tie(r._ops, r._refs, r._patterns, r._parseError);
403 swap(lt, rt);
404 }
405
406 std::vector<Op> _ops;
407 std::vector<ExpressionReference> _refs;
408 std::vector<PathPattern> _patterns;
409
410 // This member holds a parsing error string if this expression was
411 // constructed by the parser and errors were encountered during the parsing.
412 std::string _parseError;
413};
414
415// Path expressions support composing-over and value transforms.
418
419PXR_NAMESPACE_CLOSE_SCOPE
420
421#endif // PXR_USD_SDF_PATH_EXPRESSION_H
#define VT_VALUE_TYPE_CAN_TRANSFORM(T)
A helper for specializing the above trait.
Definition traits.h:150
#define VT_VALUE_TYPE_CAN_COMPOSE(T)
A helper for specializing the above trait.
Definition traits.h:140
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 "weakerexpression" (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:281
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