Loading...
Searching...
No Matches
booleanExpression.h
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7
8#ifndef PXR_USD_SDF_BOOLEAN_EXPRESSION_H
9#define PXR_USD_SDF_BOOLEAN_EXPRESSION_H
10
11#include "pxr/usd/sdf/api.h"
12
14#include "pxr/base/tf/functionRef.h"
15#include "pxr/base/tf/token.h"
16#include "pxr/base/vt/value.h"
17
18#include <iosfwd>
19#include <string>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
28
30public:
33
37 SDF_API explicit SdfBooleanExpression(std::string const& text);
38
41 SDF_API bool IsEmpty() const;
42
47 SDF_API std::string GetText() const;
48
51 SDF_API std::string const& GetParseError() const;
52
54 SDF_API std::set<TfToken> GetVariableNames() const;
55
59 SDF_API static SdfBooleanExpression
60 MakeVariable(TfToken const& variableName);
61
65 SDF_API static SdfBooleanExpression
66 MakeConstant(VtValue const& value);
67
70 enum class BinaryOperator {
72 EqualTo,
73
76
79
82
85
88
90 And,
91
93 Or,
94 };
95
106 SDF_API static SdfBooleanExpression
110
113 enum class UnaryOperator {
115 Not,
116 };
117
128 SDF_API static SdfBooleanExpression
130
144 SdfBooleanExpression const&)>;
145
158
163 using VariableVisitor = TfFunctionRef<void(TfToken const&)>;
164
169 using ConstantVisitor = TfFunctionRef<void(VtValue const&)>;
170
172 SDF_API void Visit(VariableVisitor variable,
173 ConstantVisitor constant,
174 BinaryVisitor binary,
175 UnaryVisitor unary) const;
176
180
183 SDF_API bool Evaluate(VariableCallback const& variableCallback) const;
184
188
192 RenameVariables(NameTransform const& transform) const;
193
198 SDF_API static bool
199 Validate(std::string const& expression, std::string* errorMessage = nullptr);
200
201 // Note that the internal _Node class is public to simplify details of the
202 // implementation.
203 class _Node;
204 TF_DECLARE_REF_PTRS(_Node);
205
206private:
207 SdfBooleanExpression(_NodeRefPtr const& node);
208
209 SDF_API friend std::ostream&
210 operator<<(std::ostream&, SdfBooleanExpression const&);
211
212 std::string _text;
213 std::string _parseError;
214 _NodeRefPtr _rootNode;
215};
216
217SDF_API std::ostream&
218operator<<(std::ostream& os, SdfBooleanExpression::BinaryOperator const& rhs);
219
220SDF_API std::ostream&
221operator<<(std::ostream& os, SdfBooleanExpression::UnaryOperator const& rhs);
222
223PXR_NAMESPACE_CLOSE_SCOPE
224
225#endif // PXR_USD_SDF_BOOLEAN_EXPRESSION_H
Objects of this class represent expressions that can be evaluated to produce a boolean value.
static SDF_API SdfBooleanExpression MakeVariable(TfToken const &variableName)
Constructs an expression representing a variable.
static SDF_API SdfBooleanExpression MakeUnaryOp(SdfBooleanExpression expression, UnaryOperator op)
Constructs an expression that applies the provided operator to the result of the provided subexpressi...
static SDF_API SdfBooleanExpression MakeBinaryOp(SdfBooleanExpression lhs, BinaryOperator op, SdfBooleanExpression rhs)
Constructs an expression that applies the provided operator to the result of the two provided subexpr...
SdfBooleanExpression()=default
Constructs an empty expression.
SDF_API bool IsEmpty() const
An expression is empty if it was default constructed or if there was a problem parsing its string rep...
UnaryOperator
Operators applied to a single subexpression.
SDF_API void Visit(VariableVisitor variable, ConstantVisitor constant, BinaryVisitor binary, UnaryVisitor unary) const
Invokes one of the given callbacks based on the type of the expression.
SDF_API SdfBooleanExpression(std::string const &text)
Constructs an expression by parsing a string representation.
SDF_API std::string GetText() const
Provides a string representation that can be parsed by SdfBooleanExpression(std::string const&).
static SDF_API SdfBooleanExpression MakeConstant(VtValue const &value)
Constructs an expression wrapping a constant value.
static SDF_API bool Validate(std::string const &expression, std::string *errorMessage=nullptr)
Determines if the provided string can be parsed as an expression.
SDF_API bool Evaluate(VariableCallback const &variableCallback) const
Evaluates the expression.
SDF_API SdfBooleanExpression RenameVariables(NameTransform const &transform) const
Applies the provided transform to each variable name and returns the resulting expression.
BinaryOperator
Operators for combining two subexpressions.
SDF_API std::set< TfToken > GetVariableNames() const
Provides the collection of variable names referenced by the expression.
SDF_API std::string const & GetParseError() const
Return parsing errors as a string if this expression was constructed from a string and parse errors w...
This class provides a non-owning reference to a type-erased callable object with a specified signatur...
Definition: functionRef.h:19
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:152
Standard pointer typedefs.
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:58
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...