7#ifndef PXR_USD_SDF_VARIABLE_EXPRESSION
8#define PXR_USD_SDF_VARIABLE_EXPRESSION
13#include "pxr/usd/sdf/api.h"
17#include "pxr/base/vt/value.h"
21#include <unordered_set>
24PXR_NAMESPACE_OPEN_SCOPE
26namespace Sdf_VariableExpressionImpl {
90 static bool IsExpression(
const std::string& s);
100 static bool IsValidVariableType(
const VtValue& value);
110 explicit operator bool()
const;
145 std::vector<std::string> errors;
155 std::unordered_set<std::string> usedVariables;
200 template <
class ResultType>
206 r.value =
VtValue(ResultType());
210 _FormatUnexpectedTypeError(r.value,
VtValue(ResultType())));
254 Builder(std::string&& expr) : _expr(std::move(expr)) { }
266 template <
class Argument>
270 SDF_API
operator Builder() const &;
287 template <
class Element>
296 SDF_API
operator Builder() const &;
311 template <
class... Arguments>
313 MakeFunction(
const std::string& fnName, Arguments&&... fnArgs)
316 (b.AddArgument(std::forward<Arguments>(fnArgs)), ...);
325 template <
class... Elements>
327 MakeList(Elements&&... elems)
330 (b.
AddElement(std::forward<Elements>(elems)), ...);
341 MakeListOfLiterals(
const std::vector<T>& values)
347 SDF_API
static Builder MakeLiteral(int64_t value);
348 SDF_API
static Builder MakeLiteral(
bool value);
349 SDF_API
static Builder MakeLiteral(
const std::string& value);
350 SDF_API
static Builder MakeLiteral(
const char* value);
353 SDF_API
static Builder MakeNone();
357 SDF_API
static Builder MakeVariable(
const std::string& name);
364 _FormatUnexpectedTypeError(
const VtValue& got,
const VtValue& expected);
373 _AppendBuilder(std::string* expr,
const Builder& b,
bool first);
375 template <
class Argument>
377 _Append(std::string* expr, Argument&& arg,
bool first)
381 if constexpr (std::is_same_v<
383 _AppendExpression(expr, std::forward<Argument>(arg), first);
386 _AppendBuilder(expr, std::forward<Argument>(arg), first);
390 std::vector<std::string> _errors;
391 std::shared_ptr<Sdf_VariableExpressionImpl::Node> _expression;
392 std::string _expressionStr;
397 const SdfVariableExpression::EmptyList&,
398 const SdfVariableExpression::EmptyList&)
405 const SdfVariableExpression::EmptyList&,
406 const SdfVariableExpression::EmptyList&)
411template <
class Argument>
412inline SdfVariableExpression::FunctionBuilder&
413SdfVariableExpression::FunctionBuilder::AddArgument(Argument&& arg)
415 SdfVariableExpression::_Append(
416 &_expr, std::forward<Argument>(arg),
417 *_expr.rbegin() ==
'(');
421template <
class Element>
422inline SdfVariableExpression::ListBuilder&
423SdfVariableExpression::ListBuilder::AddElement(Element&& arg)
425 SdfVariableExpression::_Append(
426 &_expr, std::forward<Element>(arg),
427 *_expr.rbegin() ==
'[');
432inline SdfVariableExpression::ListBuilder&
433SdfVariableExpression::ListBuilder::AddLiteralValues(
434 const std::vector<T>& values)
436 for (
const T& v : values) {
437 AddElement(SdfVariableExpression::MakeLiteral(v));
442PXR_NAMESPACE_CLOSE_SCOPE
Helper class for storing intermediate results when building a variable expression.
Helper class for storing intermediate results when building a function variable expression.
FunctionBuilder & AddArgument(Argument &&arg)
Add an expression as an argument to the function call.
Helper class for storing intermediate results when building a list variable expression.
ListBuilder & AddElement(Element &&elem)
Add an expression as an element to the list.
ListBuilder & AddLiteralValues(const std::vector< T > &values)
Add values in values as literal expressions to the list.
Class responsible for parsing and evaluating variable expressions.
A map with string keys and VtValue values.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
bool IsEmpty() const
Returns true iff this value is empty.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
A trait to detect instantiations of VtArray, specialized in array.h.
SDF_API const std::string & GetString() const
Returns the expression string used to construct this object.
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...
A result value representing an empty list.