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
94 struct Component {
95 bool IsStretch() const {
96 return predicateIndex == -1 && text.empty();
97 }
98
99 std::string text;
100 int predicateIndex = -1;
101 bool isLiteral = false;
102
103 friend bool operator==(Component const &l, Component const &r) {
104 return std::tie(l.text, l.predicateIndex, l.isLiteral) ==
105 std::tie(r.text, r.predicateIndex, r.isLiteral);
106 }
107
108 friend bool operator!=(Component const &l, Component const &r) {
109 return !(l == r);
110 }
111
112 template <class HashState>
113 friend void TfHashAppend(HashState &h, Component const &c) {
114 h.Append(c.text, c.predicateIndex, c.isLiteral);
115 }
116
117 friend void swap(Component &l, Component &r) {
118 auto lt = std::tie(l.text, l.predicateIndex, l.isLiteral);
119 auto rt = std::tie(r.text, r.predicateIndex, r.isLiteral);
120 swap(lt, rt);
121 }
122 };
123
131 SDF_API
132 void AppendChild(std::string const &text,
133 SdfPredicateExpression &&predExpr);
135 SDF_API
136 void AppendChild(std::string const &text,
137 SdfPredicateExpression const &predExpr);
139 SDF_API
140 void AppendChild(std::string const &text);
141
149 SDF_API
150 void AppendProperty(std::string const &text,
151 SdfPredicateExpression &&predExpr);
153 SDF_API
154 void AppendProperty(std::string const &text,
155 SdfPredicateExpression const &predExpr);
157 SDF_API
158 void AppendProperty(std::string const &text);
159
162 SdfPath const &GetPrefix() const & {
163 return _prefix;
164 }
165
168 return std::move(_prefix);
169 }
170
173 SDF_API
175
177 void SetPrefix(SdfPath const &p) {
178 SetPrefix(SdfPath(p));
179 }
180
182 SDF_API
183 std::string GetText() const;
184
185 std::vector<Component> const &GetComponents() const & {
186 return _components;
187 }
188
189 std::vector<Component> GetComponents() && {
190 return _components;
191 }
192
193 std::vector<SdfPredicateExpression> const &
194 GetPredicateExprs() const & {
195 return _predExprs;
196 }
197
198 std::vector<SdfPredicateExpression>
199 GetPredicateExprs() && {
200 return _predExprs;
201 }
202
203 bool IsProperty() const {
204 return _isProperty;
205 }
206
208 explicit operator bool() const {
209 return !_prefix.IsEmpty();
210 }
211
212 private:
213 template <class HashState>
214 friend void TfHashAppend(HashState &h, PathPattern const &pat) {
215 h.Append(pat._prefix, pat._components,
216 pat._predExprs, pat._isProperty);
217 }
218
219 friend bool
220 operator==(PathPattern const &l, PathPattern const &r) {
221 return std::tie(l._prefix, l._components,
222 l._predExprs, l._isProperty) ==
223 std::tie(r._prefix, r._components,
224 r._predExprs, r._isProperty);
225 }
226
227 friend bool
228 operator!=(PathPattern const &l, PathPattern const &r) {
229 return !(l == r);
230 }
231
232 friend void swap(PathPattern &l, PathPattern &r) {
233 auto lt = std::tie(
234 l._prefix, l._components, l._predExprs, l._isProperty);
235 auto rt = std::tie(
236 r._prefix, r._components, r._predExprs, r._isProperty);
237 swap(lt, rt);
238 }
239
240 SdfPath _prefix;
241 std::vector<Component> _components;
242 std::vector<SdfPredicateExpression> _predExprs;
243 bool _isProperty;
244 };
245
252 public:
256 SDF_API
258
259 // Optional path reference, can be empty for "weaker" references (name
260 // is "_") or for references to local or otherwise "named" collections.
261 SdfPath path;
262
263 // Name is either a property name, or "_" (meaning the weaker
264 // collection). If the name is "_", the path must be empty.
265 std::string name;
266
267 template <class HashState>
268 friend void TfHashAppend(HashState &h, ExpressionReference const &er) {
269 h.Append(er.path, er.name);
270 }
271
272 friend bool
273 operator==(ExpressionReference const &l, ExpressionReference const &r) {
274 return std::tie(l.path, l.name) == std::tie(r.path, r.name);
275 }
276
277 friend bool
278 operator!=(ExpressionReference const &l, ExpressionReference const &r) {
279 return !(l == r);
280 }
281
282 friend void swap(ExpressionReference &l, ExpressionReference &r) {
283 auto lt = std::tie(l.path, l.name);
284 auto rt = std::tie(r.path, r.name);
285 swap(lt, rt);
286 }
287 };
288
290 enum Op {
291 // Operations on atoms.
292 Complement,
293 ImpliedUnion,
294 Union,
295 Intersection,
296 Difference,
297
298 // Atoms.
299 ExpressionRef,
300 Pattern
301 };
302
305 SdfPathExpression() = default;
306
311 SDF_API
312 explicit SdfPathExpression(std::string const &expr,
313 std::string const &parseContext = {});
314
316 SDF_API
318
321 SDF_API
323
326 SDF_API
327 static SdfPathExpression const &Nothing();
328
332 SDF_API
334
336 SDF_API
337 static SdfPathExpression
339
341 static SdfPathExpression
343 return MakeComplement(SdfPathExpression(right));
344 }
345
349 SDF_API
350 static SdfPathExpression
352
354 static SdfPathExpression
356 SdfPathExpression const &left,
357 SdfPathExpression const &right) {
358 return MakeOp(op, SdfPathExpression(left), SdfPathExpression(right));
359 }
360
362 SDF_API
363 static SdfPathExpression
365
367 static SdfPathExpression
369 return MakeAtom(ExpressionReference(ref));
370 }
371
373 SDF_API
374 static SdfPathExpression
376
378 static SdfPathExpression
379 MakeAtom(PathPattern const &pattern) {
380 return MakeAtom(PathPattern(pattern));
381 }
382
416 SDF_API
417 void Walk(TfFunctionRef<void (Op, int)> logic,
418 TfFunctionRef<void (ExpressionReference const &)> ref,
419 TfFunctionRef<void (PathPattern const &)> pattern) const;
420
426 SDF_API
428 TfFunctionRef<void (std::vector<std::pair<Op, int>> const &)> logic,
429 TfFunctionRef<void (ExpressionReference const &)> ref,
430 TfFunctionRef<void (PathPattern const &)> pattern) const;
431
435 ReplacePrefix(SdfPath const &oldPrefix,
436 SdfPath const &newPrefix) const & {
437 return SdfPathExpression(*this).ReplacePrefix(oldPrefix, newPrefix);
438 }
439
442 SDF_API
444 ReplacePrefix(SdfPath const &oldPrefix,
445 SdfPath const &newPrefix) &&;
446
450 SDF_API
451 bool IsAbsolute() const;
452
456 MakeAbsolute(SdfPath const &anchor) const & {
457 return SdfPathExpression(*this).MakeAbsolute(anchor);
458 }
459
462 SDF_API
464 MakeAbsolute(SdfPath const &anchor) &&;
465
469 return !_refs.empty();
470 }
471
475 SDF_API
477
486 ExpressionReference const &)> resolve) const & {
487 return SdfPathExpression(*this).ResolveReferences(resolve);
488 }
489
491 SDF_API
495 ExpressionReference const &)> resolve) &&;
496
504 ComposeOver(SdfPathExpression const &weaker) const & {
505 return SdfPathExpression(*this).ComposeOver(weaker);
506 }
507
509 SDF_API
512
523 bool IsComplete() const {
525 }
526
529 SDF_API
530 std::string GetText() const;
531
534 bool IsEmpty() const {
535 return _ops.empty();
536 }
537
539 explicit operator bool() const {
540 return !IsEmpty();
541 }
542
545 std::string const &GetParseError() const & {
546 return _parseError;
547 }
548
549private:
550 template <class HashState>
551 friend void TfHashAppend(HashState &h, SdfPathExpression const &expr) {
552 h.Append(expr._ops, expr._refs, expr._patterns, expr._parseError);
553 }
554
555 SDF_API
556 friend std::ostream &
557 operator<<(std::ostream &, SdfPathExpression const &);
558
559 friend bool
560 operator==(SdfPathExpression const &l, SdfPathExpression const &r) {
561 return std::tie(l._ops, l._refs, l._patterns, l._parseError) ==
562 std::tie(r._ops, r._refs, r._patterns, r._parseError);
563 }
564
565 friend bool
566 operator!=(SdfPathExpression const &l, SdfPathExpression const &r) {
567 return !(l == r);
568 }
569
570 friend void swap(SdfPathExpression &l, SdfPathExpression &r) {
571 auto lt = std::tie(l._ops, l._refs, l._patterns, l._parseError);
572 auto rt = std::tie(r._ops, r._refs, r._patterns, r._parseError);
573 swap(lt, rt);
574 }
575
576 std::vector<Op> _ops;
577 std::vector<ExpressionReference> _refs;
578 std::vector<PathPattern> _patterns;
579
580 // This member holds a parsing error string if this expression was
581 // constructed by the parser and errors were encountered during the parsing.
582 std::string _parseError;
583};
584
585
586PXR_NAMESPACE_CLOSE_SCOPE
587
588#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 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.
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...
std::string const & GetParseError() const &
Return parsing errors as a string if this function was constructed from a string and parse errors wer...
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.