Loading...
Searching...
No Matches
pathExpression.h
1//
2// Copyright 2023 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_USD_SDF_PATH_EXPRESSION_H
25#define PXR_USD_SDF_PATH_EXPRESSION_H
26
27#include "pxr/pxr.h"
28#include "pxr/usd/sdf/api.h"
29#include "pxr/usd/sdf/path.h"
30#include "pxr/usd/sdf/predicateExpression.h"
31#include "pxr/base/tf/hash.h"
32
33#include <iosfwd>
34#include <string>
35#include <tuple>
36#include <utility>
37#include <vector>
38
39PXR_NAMESPACE_OPEN_SCOPE
40
72{
73public:
81 {
82 public:
85 SDF_API
87
89 SDF_API
90 explicit PathPattern(SdfPath const &prefix);
91
93 SDF_API
94 explicit PathPattern(SdfPath &&prefix);
95
102 struct Component {
103 bool IsStretch() const {
104 return predicateIndex == -1 && text.empty();
105 }
106
107 std::string text;
108 int predicateIndex = -1;
109 bool isLiteral = false;
110
111 friend bool operator==(Component const &l, Component const &r) {
112 return std::tie(l.text, l.predicateIndex, l.isLiteral) ==
113 std::tie(r.text, r.predicateIndex, r.isLiteral);
114 }
115
116 friend bool operator!=(Component const &l, Component const &r) {
117 return !(l == r);
118 }
119
120 template <class HashState>
121 friend void TfHashAppend(HashState &h, Component const &c) {
122 h.Append(c.text, c.predicateIndex, c.isLiteral);
123 }
124
125 friend void swap(Component &l, Component &r) {
126 auto lt = std::tie(l.text, l.predicateIndex, l.isLiteral);
127 auto rt = std::tie(r.text, r.predicateIndex, r.isLiteral);
128 swap(lt, rt);
129 }
130 };
131
139 SDF_API
140 void AppendChild(std::string const &text,
141 SdfPredicateExpression &&predExpr);
143 SDF_API
144 void AppendChild(std::string const &text,
145 SdfPredicateExpression const &predExpr);
147 SDF_API
148 void AppendChild(std::string const &text);
149
157 SDF_API
158 void AppendProperty(std::string const &text,
159 SdfPredicateExpression &&predExpr);
161 SDF_API
162 void AppendProperty(std::string const &text,
163 SdfPredicateExpression const &predExpr);
165 SDF_API
166 void AppendProperty(std::string const &text);
167
170 SdfPath const &GetPrefix() const & {
171 return _prefix;
172 }
173
176 return std::move(_prefix);
177 }
178
181 SDF_API
183
185 void SetPrefix(SdfPath const &p) {
186 SetPrefix(SdfPath(p));
187 }
188
190 SDF_API
191 std::string GetText() const;
192
193 std::vector<Component> const &GetComponents() const & {
194 return _components;
195 }
196
197 std::vector<Component> GetComponents() && {
198 return _components;
199 }
200
201 std::vector<SdfPredicateExpression> const &
202 GetPredicateExprs() const & {
203 return _predExprs;
204 }
205
206 std::vector<SdfPredicateExpression>
207 GetPredicateExprs() && {
208 return _predExprs;
209 }
210
211 bool IsProperty() const {
212 return _isProperty;
213 }
214
216 explicit operator bool() const {
217 return !_prefix.IsEmpty();
218 }
219
220 private:
221 template <class HashState>
222 friend void TfHashAppend(HashState &h, PathPattern const &pat) {
223 h.Append(pat._prefix, pat._components,
224 pat._predExprs, pat._isProperty);
225 }
226
227 friend bool
228 operator==(PathPattern const &l, PathPattern const &r) {
229 return std::tie(l._prefix, l._components,
230 l._predExprs, l._isProperty) ==
231 std::tie(r._prefix, r._components,
232 r._predExprs, r._isProperty);
233 }
234
235 friend bool
236 operator!=(PathPattern const &l, PathPattern const &r) {
237 return !(l == r);
238 }
239
240 friend void swap(PathPattern &l, PathPattern &r) {
241 auto lt = std::tie(
242 l._prefix, l._components, l._predExprs, l._isProperty);
243 auto rt = std::tie(
244 r._prefix, r._components, r._predExprs, r._isProperty);
245 swap(lt, rt);
246 }
247
248 SdfPath _prefix;
249 std::vector<Component> _components;
250 std::vector<SdfPredicateExpression> _predExprs;
251 bool _isProperty;
252 };
253
260 public:
264 SDF_API
266
267 // Optional path reference, can be empty for "weaker" references (name
268 // is "_") or for references to local or otherwise "named" collections.
269 SdfPath path;
270
271 // Name is either a property name, or "_" (meaning the weaker
272 // collection). If the name is "_", the path must be empty.
273 std::string name;
274
275 template <class HashState>
276 friend void TfHashAppend(HashState &h, ExpressionReference const &er) {
277 h.Append(er.path, er.name);
278 }
279
280 friend bool
281 operator==(ExpressionReference const &l, ExpressionReference const &r) {
282 return std::tie(l.path, l.name) == std::tie(r.path, r.name);
283 }
284
285 friend bool
286 operator!=(ExpressionReference const &l, ExpressionReference const &r) {
287 return !(l == r);
288 }
289
290 friend void swap(ExpressionReference &l, ExpressionReference &r) {
291 auto lt = std::tie(l.path, l.name);
292 auto rt = std::tie(r.path, r.name);
293 swap(lt, rt);
294 }
295 };
296
298 enum Op {
299 // Operations on atoms.
300 Complement,
301 ImpliedUnion,
302 Union,
303 Intersection,
304 Difference,
305
306 // Atoms.
307 ExpressionRef,
308 Pattern
309 };
310
313 SdfPathExpression() = default;
314
319 SDF_API
320 explicit SdfPathExpression(std::string const &expr,
321 std::string const &parseContext = {});
322
324 SDF_API
326
329 SDF_API
331
334 SDF_API
335 static SdfPathExpression const &Nothing();
336
340 SDF_API
342
344 SDF_API
345 static SdfPathExpression
347
349 static SdfPathExpression
351 return MakeComplement(SdfPathExpression(right));
352 }
353
357 SDF_API
358 static SdfPathExpression
360
362 static SdfPathExpression
364 SdfPathExpression const &left,
365 SdfPathExpression const &right) {
366 return MakeOp(op, SdfPathExpression(left), SdfPathExpression(right));
367 }
368
370 SDF_API
371 static SdfPathExpression
373
375 static SdfPathExpression
377 return MakeAtom(ExpressionReference(ref));
378 }
379
381 SDF_API
382 static SdfPathExpression
384
386 static SdfPathExpression
387 MakeAtom(PathPattern const &pattern) {
388 return MakeAtom(PathPattern(pattern));
389 }
390
392 static SdfPathExpression
393 MakeAtom(SdfPath const &path) {
394 return MakeAtom(PathPattern(path));
395 }
396
398 static SdfPathExpression
400 return MakeAtom(PathPattern(path));
401 }
402
436 SDF_API
437 void Walk(TfFunctionRef<void (Op, int)> logic,
438 TfFunctionRef<void (ExpressionReference const &)> ref,
439 TfFunctionRef<void (PathPattern const &)> pattern) const;
440
446 SDF_API
448 TfFunctionRef<void (std::vector<std::pair<Op, int>> const &)> logic,
449 TfFunctionRef<void (ExpressionReference const &)> ref,
450 TfFunctionRef<void (PathPattern const &)> pattern) const;
451
455 ReplacePrefix(SdfPath const &oldPrefix,
456 SdfPath const &newPrefix) const & {
457 return SdfPathExpression(*this).ReplacePrefix(oldPrefix, newPrefix);
458 }
459
462 SDF_API
464 ReplacePrefix(SdfPath const &oldPrefix,
465 SdfPath const &newPrefix) &&;
466
470 SDF_API
471 bool IsAbsolute() const;
472
476 MakeAbsolute(SdfPath const &anchor) const & {
477 return SdfPathExpression(*this).MakeAbsolute(anchor);
478 }
479
482 SDF_API
484 MakeAbsolute(SdfPath const &anchor) &&;
485
489 return !_refs.empty();
490 }
491
495 SDF_API
497
506 ExpressionReference const &)> resolve) const & {
507 return SdfPathExpression(*this).ResolveReferences(resolve);
508 }
509
511 SDF_API
515 ExpressionReference const &)> resolve) &&;
516
524 ComposeOver(SdfPathExpression const &weaker) const & {
525 return SdfPathExpression(*this).ComposeOver(weaker);
526 }
527
529 SDF_API
532
543 bool IsComplete() const {
545 }
546
549 SDF_API
550 std::string GetText() const;
551
554 bool IsEmpty() const {
555 return _ops.empty();
556 }
557
559 explicit operator bool() const {
560 return !IsEmpty();
561 }
562
565 std::string const &GetParseError() const & {
566 return _parseError;
567 }
568
569private:
570 template <class HashState>
571 friend void TfHashAppend(HashState &h, SdfPathExpression const &expr) {
572 h.Append(expr._ops, expr._refs, expr._patterns, expr._parseError);
573 }
574
575 SDF_API
576 friend std::ostream &
577 operator<<(std::ostream &, SdfPathExpression const &);
578
579 friend bool
580 operator==(SdfPathExpression const &l, SdfPathExpression const &r) {
581 return std::tie(l._ops, l._refs, l._patterns, l._parseError) ==
582 std::tie(r._ops, r._refs, r._patterns, r._parseError);
583 }
584
585 friend bool
586 operator!=(SdfPathExpression const &l, SdfPathExpression const &r) {
587 return !(l == r);
588 }
589
590 friend void swap(SdfPathExpression &l, SdfPathExpression &r) {
591 auto lt = std::tie(l._ops, l._refs, l._patterns, l._parseError);
592 auto rt = std::tie(r._ops, r._refs, r._patterns, r._parseError);
593 swap(lt, rt);
594 }
595
596 std::vector<Op> _ops;
597 std::vector<ExpressionReference> _refs;
598 std::vector<PathPattern> _patterns;
599
600 // This member holds a parsing error string if this expression was
601 // constructed by the parser and errors were encountered during the parsing.
602 std::string _parseError;
603};
604
605
606PXR_NAMESPACE_CLOSE_SCOPE
607
608#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 SdfPath matching patterns, consisting of an SdfPath prefix followed b...
SDF_API void AppendChild(std::string const &text, SdfPredicateExpression &&predExpr)
Append a prim child component to this pattern, with optional predicate expression predExpr.
SDF_API void SetPrefix(SdfPath &&p)
Set this pattern's non-speculative prefix (leading path components with no wildcards and no predicate...
SDF_API void AppendProperty(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 std::string GetText() const
Return the string representation of this pattern.
SDF_API void AppendProperty(std::string const &text)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API PathPattern(SdfPath const &prefix)
Construct a PathPattern with the prefix path.
SDF_API void 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 void AppendChild(std::string const &text)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SdfPath const & GetPrefix() const &
Return this pattern's non-speculative prefix (leading path components with no wildcards and no predic...
SDF_API void AppendProperty(std::string const &text, SdfPredicateExpression &&predExpr)
Append a prim property component to this pattern, with optional predicate expression predExpr.
SDF_API PathPattern(SdfPath &&prefix)
Construct a PathPattern with the prefix path.
void SetPrefix(SdfPath const &p)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SdfPath GetPrefix() &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDF_API PathPattern()
Construct the empty pattern whose bool-conversion operator returns false.
Objects of this class represent a logical expression syntax tree consisting of SdfPath matching patte...
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:290
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:414
Represents a logical expression syntax tree consisting of predicate function calls joined by the logi...
This class provides a non-owning reference to a type-erased callable object with a specified signatur...
Definition: functionRef.h:36
A component represents a pattern matching component past the initial SdfPath prefix.