Loading...
Searching...
No Matches
variableExpression.h
Go to the documentation of this file.
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_VARIABLE_EXPRESSION
8#define PXR_USD_SDF_VARIABLE_EXPRESSION
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/sdf/api.h"
14
15#include "pxr/base/vt/array.h"
17#include "pxr/base/vt/value.h"
18
19#include <memory>
20#include <string>
21#include <unordered_set>
22#include <vector>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
26namespace Sdf_VariableExpressionImpl {
27 class Node;
28}
29
64{
65public:
69 SDF_API
70 explicit SdfVariableExpression(const std::string& expr);
71
73 SDF_API
75
76 SDF_API
78
85 SDF_API
86 static bool IsExpression(const std::string& s);
87
95 SDF_API
96 static bool IsValidVariableType(const VtValue& value);
97
105 SDF_API
106 explicit operator bool() const;
107
109 SDF_API
110 const std::string& GetString() const;
111
117 SDF_API
118 const std::vector<std::string>& GetErrors() const;
119
122 class EmptyList { };
123
125 class Result
126 {
127 public:
135 VtValue value;
136
138 std::vector<std::string> errors;
139
148 std::unordered_set<std::string> usedVariables;
149 };
150
172 SDF_API
173 Result Evaluate(const VtDictionary& variables) const;
174
193 template <class ResultType>
194 Result EvaluateTyped(const VtDictionary& variables) const
195 {
196 Result r = Evaluate(variables);
197
198 if (VtIsArray<ResultType>::value && r.value.IsHolding<EmptyList>()) {
199 r.value = VtValue(ResultType());
200 }
201 else if (!r.value.IsEmpty() && !r.value.IsHolding<ResultType>()) {
202 r.errors.push_back(
203 _FormatUnexpectedTypeError(r.value, VtValue(ResultType())));
204 r.value = VtValue();
205 }
206 return r;
207 }
208
209private:
210 SDF_API
211 static std::string
212 _FormatUnexpectedTypeError(const VtValue& got, const VtValue& expected);
213
214 std::vector<std::string> _errors;
215 std::shared_ptr<Sdf_VariableExpressionImpl::Node> _expression;
216 std::string _expressionStr;
217};
218
219inline bool
220operator==(
223{
224 return true;
225}
226
227inline bool
228operator!=(
231{
232 return false;
233}
234
235PXR_NAMESPACE_CLOSE_SCOPE
236
237#endif
Class responsible for parsing and evaluating variable expressions.
SDF_API const std::string & GetString() const
Returns the expression string used to construct this object.
static SDF_API bool IsExpression(const std::string &s)
Returns true if s is a variable expression, false otherwise.
SDF_API SdfVariableExpression()
Construct an object representing an invalid expression.
static SDF_API bool IsValidVariableType(const VtValue &value)
Returns true if value holds a type that is supported by variable expressions, false otherwise.
SDF_API Result Evaluate(const VtDictionary &variables) const
Evaluates this expression using the variables in variables and returns a Result object with the final...
SDF_API const std::vector< std::string > & GetErrors() const
Returns a list of errors encountered when parsing this expression.
Result EvaluateTyped(const VtDictionary &variables) const
Evaluates this expression using the variables in variables and returns a Result object with the final...
SDF_API SdfVariableExpression(const std::string &expr)
Construct using the expression expr.
A result value representing an empty list.
A map with string keys and VtValue values.
Definition: dictionary.h:43
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147
Array concept. By default, types are not arrays.
Definition: traits.h:22