24#ifndef PXR_USD_SDF_PATH_EXPRESSION_EVAL_H
25#define PXR_USD_SDF_PATH_EXPRESSION_EVAL_H
28#include "pxr/usd/sdf/api.h"
29#include "pxr/usd/sdf/path.h"
30#include "pxr/usd/sdf/pathExpression.h"
31#include "pxr/usd/sdf/predicateExpression.h"
32#include "pxr/usd/sdf/predicateLibrary.h"
33#include "pxr/usd/sdf/predicateProgram.h"
35#include "pxr/base/arch/regex.h"
36#include "pxr/base/tf/functionRef.h"
42PXR_NAMESPACE_OPEN_SCOPE
45template <
class DomainType>
49template <
class DomainType>
55class Sdf_PathExpressionEvalBase;
60class Sdf_PathExpressionEvalBase
64 Sdf_MakePathExpressionEvalImpl(
65 Sdf_PathExpressionEvalBase &eval,
72 bool IsEmpty()
const {
77 explicit operator bool()
const {
82 class _PatternImplBase;
84 class _PatternIncrSearchState {
85 friend class _PatternImplBase;
87 void Pop(
int newDepth) {
88 while (!_segmentMatchDepths.empty() &&
89 _segmentMatchDepths.back() >= newDepth) {
90 _segmentMatchDepths.pop_back();
92 if (newDepth <= _constantDepth) {
97 std::vector<int> _segmentMatchDepths;
98 int _constantDepth = -1;
99 bool _constantValue =
false;
102 class _PatternImplBase {
117 runNthPredicate)
const;
121 _Next(_PatternIncrSearchState &searchState,
124 runNthPredicate)
const;
126 enum _ComponentType {
139 bool IsEmpty()
const {
return begin == end; }
140 bool StartsAt(
size_t idx)
const {
return begin == idx; }
141 bool EndsAt(
size_t idx)
const {
return end == idx; }
142 size_t GetSize()
const {
return end - begin; }
147 std::vector<_Component> _components;
148 std::vector<_Segment> _segments;
149 std::vector<std::string> _explicitNames;
150 std::vector<ArchRegex> _regexes;
156 _MatchPrimOrProp, _MatchPrimOnly, _MatchPropOnly
170 enum _Op { EvalPattern, Not, Open, Close, Or, And };
172 std::vector<_Op> _ops;
183template <
class DomainType>
187 class _PatternImpl :
public _PatternImplBase {
189 _PatternImpl() =
default;
195 _predicates.push_back(
196 SdfLinkPredicateExpression(predExpr, predLib));
197 return _predicates.size()-1;
199 _Init(pattern, linkPredicate);
203 template <
class PathToObject>
206 PathToObject
const &pathToObj)
const {
207 auto runNthPredicate =
208 [
this, &pathToObj](
int i,
SdfPath const &path) {
209 return _predicates[i](pathToObj(path));
211 return _Match(objPath, runNthPredicate);
215 template <
class PathToObject>
218 _PatternIncrSearchState &search,
219 PathToObject
const &pathToObj)
const {
220 auto runNthPredicate =
221 [
this, &pathToObj](
int i,
SdfPath const &path) {
222 return _predicates[i](pathToObj(path));
224 return _Next(search, objPath, runNthPredicate);
228 std::vector<SdfPredicateProgram<DomainType>> _predicates;
235 SdfMakePathExpressionEval<DomainType>(
239 bool IsEmpty()
const {
240 return _patternImpls.empty();
244 template <
class PathToObject>
247 PathToObject
const &pathToObj)
const {
251 auto patternImplIter = _patternImpls.cbegin();
252 auto evalPattern = [&](
bool skip) {
254 (*patternImplIter++).Match(objPath, pathToObj);
256 return _EvalExpr(evalPattern);
266 template <
class PathToObject>
272 PathToObject
const &p2o)
274 , _incrSearchStates(_eval->_patternImpls.size())
276 , _lastPathDepth(0) {}
281 , _incrSearchStates(_eval->_patternImpls.size())
282 , _pathToObj(std::move(p2o))
283 , _lastPathDepth(0) {}
297 auto patternImplIter = _eval->_patternImpls.begin();
298 auto stateIter = _incrSearchStates.begin();
300 const int popLevel = (newDepth <= _lastPathDepth) ? newDepth : 0;
301 auto patternStateNext = [&](
bool skip) {
303 stateIter->Pop(popLevel);
307 : (*patternImplIter++).Next(objPath, *stateIter++,
310 _lastPathDepth = newDepth;
311 return _eval->_EvalExpr(patternStateNext);
322 std::vector<_PatternIncrSearchState> _incrSearchStates;
324 PathToObject _pathToObj;
331 template <
class PathToObject>
332 IncrementalSearcher<std::decay_t<PathToObject>>
335 this, std::forward<PathToObject>(pathToObj));
339 std::vector<_PatternImpl> _patternImpls;
353template <
class DomainType>
363 auto translatePattern = [&](Expr::PathPattern
const &pattern) {
366 eval._patternImpls.emplace_back(pattern, lib);
367 eval._ops.push_back(Eval::EvalPattern);
370 if (!Sdf_MakePathExpressionEvalImpl(eval, expr, translatePattern)) {
377PXR_NAMESPACE_CLOSE_SCOPE
Objects of this class represent SdfPath matching patterns, consisting of an SdfPath prefix followed b...
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 SdfPath matching patte...
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.
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...