7#ifndef PXR_USD_SDF_PATH_EXPRESSION_EVAL_H
8#define PXR_USD_SDF_PATH_EXPRESSION_EVAL_H
11#include "pxr/usd/sdf/api.h"
12#include "pxr/usd/sdf/path.h"
13#include "pxr/usd/sdf/pathExpression.h"
14#include "pxr/usd/sdf/predicateExpression.h"
15#include "pxr/usd/sdf/predicateLibrary.h"
16#include "pxr/usd/sdf/predicateProgram.h"
18#include "pxr/base/arch/regex.h"
19#include "pxr/base/tf/functionRef.h"
25PXR_NAMESPACE_OPEN_SCOPE
28template <
class DomainType>
32template <
class DomainType>
38class Sdf_PathExpressionEvalBase;
43class Sdf_PathExpressionEvalBase
47 Sdf_MakePathExpressionEvalImpl(
48 Sdf_PathExpressionEvalBase &eval,
55 bool IsEmpty()
const {
60 explicit operator bool()
const {
65 class _PatternImplBase;
67 class _PatternIncrSearchState {
68 friend class _PatternImplBase;
70 void Pop(
int newDepth) {
71 while (!_segmentMatchDepths.empty() &&
72 _segmentMatchDepths.back() >= newDepth) {
73 _segmentMatchDepths.pop_back();
75 if (newDepth <= _constantDepth) {
80 std::vector<int> _segmentMatchDepths;
81 int _constantDepth = -1;
82 bool _constantValue =
false;
85 class _PatternImplBase {
100 runNthPredicate)
const;
104 _Next(_PatternIncrSearchState &searchState,
107 runNthPredicate)
const;
109 enum _ComponentType {
122 bool IsEmpty()
const {
return begin == end; }
123 bool StartsAt(
size_t idx)
const {
return begin == idx; }
124 bool EndsAt(
size_t idx)
const {
return end == idx; }
125 size_t GetSize()
const {
return end - begin; }
130 std::vector<_Component> _components;
131 std::vector<_Segment> _segments;
132 std::vector<std::string> _explicitNames;
133 std::vector<ArchRegex> _regexes;
139 _MatchPrimOrProp, _MatchPrimOnly, _MatchPropOnly
153 enum _Op { EvalPattern, Not, Open, Close, Or, And };
155 std::vector<_Op> _ops;
166template <
class DomainType>
170 class _PatternImpl :
public _PatternImplBase {
172 _PatternImpl() =
default;
178 _predicates.push_back(
179 SdfLinkPredicateExpression(predExpr, predLib));
180 return _predicates.size()-1;
182 _Init(pattern, linkPredicate);
186 template <
class PathToObject>
189 PathToObject
const &pathToObj)
const {
190 auto runNthPredicate =
191 [
this, &pathToObj](
int i,
SdfPath const &path) {
192 return _predicates[i](pathToObj(path));
194 return _Match(objPath, runNthPredicate);
198 template <
class PathToObject>
201 _PatternIncrSearchState &search,
202 PathToObject
const &pathToObj)
const {
203 auto runNthPredicate =
204 [
this, &pathToObj](
int i,
SdfPath const &path) {
205 return _predicates[i](pathToObj(path));
207 return _Next(search, objPath, runNthPredicate);
211 std::vector<SdfPredicateProgram<DomainType>> _predicates;
218 SdfMakePathExpressionEval<DomainType>(
222 bool IsEmpty()
const {
223 return _patternImpls.empty();
227 template <
class PathToObject>
230 PathToObject
const &pathToObj)
const {
234 auto patternImplIter = _patternImpls.cbegin();
235 auto evalPattern = [&](
bool skip) {
237 (*patternImplIter++).Match(objPath, pathToObj);
239 return _EvalExpr(evalPattern);
249 template <
class PathToObject>
255 PathToObject
const &p2o)
257 , _incrSearchStates(_eval->_patternImpls.size())
259 , _lastPathDepth(0) {}
264 , _incrSearchStates(_eval->_patternImpls.size())
265 , _pathToObj(std::move(p2o))
266 , _lastPathDepth(0) {}
280 auto patternImplIter = _eval->_patternImpls.begin();
281 auto stateIter = _incrSearchStates.begin();
283 const int popLevel = (newDepth <= _lastPathDepth) ? newDepth : 0;
284 auto patternStateNext = [&](
bool skip) {
286 stateIter->Pop(popLevel);
290 : (*patternImplIter++).Next(objPath, *stateIter++,
293 _lastPathDepth = newDepth;
294 return _eval->_EvalExpr(patternStateNext);
305 std::vector<_PatternIncrSearchState> _incrSearchStates;
307 PathToObject _pathToObj;
314 template <
class PathToObject>
315 IncrementalSearcher<std::decay_t<PathToObject>>
318 this, std::forward<PathToObject>(pathToObj));
322 std::vector<_PatternImpl> _patternImpls;
336template <
class DomainType>
346 auto translatePattern = [&](Expr::PathPattern
const &pattern) {
349 eval._patternImpls.emplace_back(pattern, lib);
350 eval._ops.push_back(Eval::EvalPattern);
353 if (!Sdf_MakePathExpressionEvalImpl(eval, expr, translatePattern)) {
360PXR_NAMESPACE_CLOSE_SCOPE
This class implements stateful incremental search over DomainType objects in depth-first order.
void Reset()
Reset this object's incremental search state so that a new round of searching may begin.
SdfPredicateFunctionResult Next(SdfPath const &objPath)
Advance the search to the next objPath, and return the result of evaluating the expression on it.
Objects of this class evaluate complete SdfPathExpressions.
friend SdfPathExpressionEval SdfMakePathExpressionEval(SdfPathExpression const &expr, SdfPredicateLibrary< DomainType > const &lib)
Make an SdfPathExpressionEval object to evaluate expr using lib to link any embedded predicate expres...
IncrementalSearcher< std::decay_t< PathToObject > > MakeIncrementalSearcher(PathToObject &&pathToObj) const
Create an IncrementalSearcher object, using pathToObject to map DomainType instances to their paths.
SdfPredicateFunctionResult Match(SdfPath const &objPath, PathToObject const &pathToObj) const
Test objPath for a match with this expression.
Objects of this class represent a logical expression syntax tree consisting of SdfPathPattern s,...
A path value used to locate objects in layers or scenegraphs.
SDF_API size_t GetPathElementCount() const
Returns the number of path elements in this path.
Objects of this class represent SdfPath matching patterns, consisting of an SdfPath prefix followed b...
Represents a logical expression syntax tree consisting of predicate function calls joined by the logi...
Represents the result of a predicate function: a pair of the boolean result and a Constancy token ind...
static SdfPredicateFunctionResult MakeConstant(bool value)
Create with value and 'ConstantOverDescendants'.
Represents a library of predicate functions for use with SdfPredicateExpression.
This class provides a non-owning reference to a type-erased callable object with a specified signatur...