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
262 ReplacePrefix(SdfPath const &oldPrefix,
263 SdfPath const &newPrefix) const & {
264 return SdfPathExpression(*this).ReplacePrefix(oldPrefix, newPrefix);
265 }
266
269 SDF_API
271 ReplacePrefix(SdfPath const &oldPrefix,
272 SdfPath const &newPrefix) &&;
273
277 SDF_API
278 bool IsAbsolute() const;
279
283 MakeAbsolute(SdfPath const &anchor) const & {
284 return SdfPathExpression(*this).MakeAbsolute(anchor);
285 }
286
289 SDF_API
291 MakeAbsolute(SdfPath const &anchor) &&;
292
296 return !_refs.empty();
297 }
298
302 SDF_API
304
313 ExpressionReference const &)> resolve) const & {
314 return SdfPathExpression(*this).ResolveReferences(resolve);
315 }
316
318 SDF_API
322 ExpressionReference const &)> resolve) &&;
323
330 ComposeOver(SdfPathExpression const &weaker) const & {
331 return SdfPathExpression(*this).ComposeOver(weaker);
332 }
333
335 SDF_API
338
349 bool IsComplete() const {
351 }
352
355 SDF_API
356 std::string GetText() const;
357
360 bool IsEmpty() const {
361 return _ops.empty();
362 }
363
365 explicit operator bool() const {
366 return !IsEmpty();
367 }
368
371 std::string const &GetParseError() const & {
372 return _parseError;
373 }
374
375private:
376 template <class HashState>
377 friend void TfHashAppend(HashState &h, SdfPathExpression const &expr) {
378 h.Append(expr._ops, expr._refs, expr._patterns, expr._parseError);
379 }
380
381 SDF_API
382 friend std::ostream &
383 operator<<(std::ostream &, SdfPathExpression const &);
384
385 friend bool
386 operator==(SdfPathExpression const &l, SdfPathExpression const &r) {
387 return std::tie(l._ops, l._refs, l._patterns, l._parseError) ==
388 std::tie(r._ops, r._refs, r._patterns, r._parseError);
389 }
390
391 friend bool
392 operator!=(SdfPathExpression const &l, SdfPathExpression const &r) {
393 return !(l == r);
394 }
395
396 friend void swap(SdfPathExpression &l, SdfPathExpression &r) {
397 auto lt = std::tie(l._ops, l._refs, l._patterns, l._parseError);
398 auto rt = std::tie(r._ops, r._refs, r._patterns, r._parseError);
399 swap(lt, rt);
400 }
401
402 std::vector<Op> _ops;
403 std::vector<ExpressionReference> _refs;
404 std::vector<PathPattern> _patterns;
405
406 // This member holds a parsing error string if this expression was
407 // constructed by the parser and errors were encountered during the parsing.
408 std::string _parseError;
409};
410
411// Path expressions support composing-over and value transforms.
414
415PXR_NAMESPACE_CLOSE_SCOPE
416
417#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 "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:280
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