Loading...
Searching...
No Matches
variableExpression.h
Go to the documentation of this file.
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_VARIABLE_EXPRESSION
25#define PXR_USD_SDF_VARIABLE_EXPRESSION
26
28
29#include "pxr/pxr.h"
30#include "pxr/usd/sdf/api.h"
31
32#include "pxr/base/vt/array.h"
34#include "pxr/base/vt/value.h"
35
36#include <memory>
37#include <string>
38#include <unordered_set>
39#include <vector>
40
41PXR_NAMESPACE_OPEN_SCOPE
42
43namespace Sdf_VariableExpressionImpl {
44 class Node;
45}
46
81{
82public:
86 SDF_API
87 explicit SdfVariableExpression(const std::string& expr);
88
90 SDF_API
92
93 SDF_API
95
102 SDF_API
103 static bool IsExpression(const std::string& s);
104
112 SDF_API
113 static bool IsValidVariableType(const VtValue& value);
114
122 SDF_API
123 explicit operator bool() const;
124
126 SDF_API
127 const std::string& GetString() const;
128
134 SDF_API
135 const std::vector<std::string>& GetErrors() const;
136
139 class EmptyList { };
140
142 class Result
143 {
144 public:
152 VtValue value;
153
155 std::vector<std::string> errors;
156
165 std::unordered_set<std::string> usedVariables;
166 };
167
189 SDF_API
190 Result Evaluate(const VtDictionary& variables) const;
191
210 template <class ResultType>
211 Result EvaluateTyped(const VtDictionary& variables) const
212 {
213 Result r = Evaluate(variables);
214
215 if (VtIsArray<ResultType>::value && r.value.IsHolding<EmptyList>()) {
216 r.value = VtValue(ResultType());
217 }
218 else if (!r.value.IsEmpty() && !r.value.IsHolding<ResultType>()) {
219 r.errors.push_back(
220 _FormatUnexpectedTypeError(r.value, VtValue(ResultType())));
221 r.value = VtValue();
222 }
223 return r;
224 }
225
226private:
227 SDF_API
228 static std::string
229 _FormatUnexpectedTypeError(const VtValue& got, const VtValue& expected);
230
231 std::vector<std::string> _errors;
232 std::shared_ptr<Sdf_VariableExpressionImpl::Node> _expression;
233 std::string _expressionStr;
234};
235
236inline bool
237operator==(
240{
241 return true;
242}
243
244inline bool
245operator!=(
248{
249 return false;
250}
251
252PXR_NAMESPACE_CLOSE_SCOPE
253
254#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:60
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
Array concept. By default, types are not arrays.
Definition: traits.h:39