HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
booleanExpression.h
Go to the documentation of this file.
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 
15 #include "pxr/base/tf/token.h"
16 #include "pxr/base/vt/value.h"
17 
18 #include <iosfwd>
19 #include <string>
20 
22 
23 /// \class SdfBooleanExpression
24 ///
25 /// Objects of this class represent expressions that can be evaluated to produce
26 /// a boolean value.
27 /// See \ref Sdf_Page_BooleanExpressions for more details.
28 
30 public:
31  /// Constructs an empty expression.
32  SdfBooleanExpression() = default;
33 
34  /// Constructs an expression by parsing a string representation. If an error
35  /// occurs while parsing the string, the result will be an empty expression.
36  /// See also GetParseError().
37  SDF_API explicit SdfBooleanExpression(std::string const& text);
38 
39  /// An expression is empty if it was default constructed or if there
40  /// was a problem parsing its string representation.
41  SDF_API bool IsEmpty() const;
42 
43  /// Provides a string representation that can be parsed by
44  /// SdfBooleanExpression(std::string const&).
45  /// If the expression was constructed from a string, the existing formatting
46  /// will be preserved.
47  SDF_API std::string GetText() const;
48 
49  /// Return parsing errors as a string if this expression was constructed
50  /// from a string and parse errors were encountered.
51  SDF_API std::string const& GetParseError() const;
52 
53  /// Provides the collection of variable names referenced by the expression.
54  SDF_API std::set<TfToken> GetVariableNames() const;
55 
56  /// Constructs an expression representing a variable.
57  ///
58  /// \see VariableVisitor
59  SDF_API static SdfBooleanExpression
60  MakeVariable(TfToken const& variableName);
61 
62  /// Constructs an expression wrapping a constant value.
63  ///
64  /// \see ConstantVisitor
65  SDF_API static SdfBooleanExpression
66  MakeConstant(VtValue const& value);
67 
68  /// Operators for combining two subexpressions.
69  /// \see MakeBinaryOp
70  enum class BinaryOperator {
71  /// The `==` operator.
72  EqualTo,
73 
74  /// The `!=` operator.
75  NotEqualTo,
76 
77  /// The `<` operator.
78  LessThan,
79 
80  /// The `<=` operator.
82 
83  /// The `>` operator.
85 
86  /// The `>=` operator.
88 
89  /// The `&&` operator.
90  And,
91 
92  /// The `||` operator.
93  Or,
94  };
95 
96  /// Constructs an expression that applies the provided operator to the
97  /// result of the two provided subexpressions.
98  ///
99  /// The expression `width > 10.0` would be constructed with the arguments:
100  /// |argument|value |
101  /// |--------|-------------------------------------------------------|
102  /// |lhs |`SdfBooleanExpression::MakeVariable(TfToken("width"));`|
103  /// |op |BinaryOperator::GreaterThan |
104  /// |rhs |`SdfBooleanExpression::MakeConstant(VtValue(10.0));` |
105  /// \see BinaryVisitor
106  SDF_API static SdfBooleanExpression
107  MakeBinaryOp(SdfBooleanExpression lhs,
109  SdfBooleanExpression rhs);
110 
111  /// Operators applied to a single subexpression.
112  /// \see MakeUnaryOp
113  enum class UnaryOperator {
114  /// The `!` operator.
115  Not,
116  };
117 
118  /// Constructs an expression that applies the provided operator to the
119  /// result of the provided subexpression.
120  ///
121  /// The expression `!(width > 10.0)` would be constructed with the
122  /// arguments:
123  /// |argument |value |
124  /// |----------|------------------------------------------------|
125  /// |expression|SdfBooleanExpression representing `width > 10.0`|
126  /// |op | UnaryOp::Not |
127  /// \see UnaryVisitor
128  SDF_API static SdfBooleanExpression
129  MakeUnaryOp(SdfBooleanExpression expression, UnaryOperator op);
130 
131  /// The Visit() method will invoke this callback if the receiver represents
132  /// a binary operator applied to two subexpressions.
133  ///
134  /// Given the expression `width > 10.0`, the callback would be invoked with
135  /// the arguments:
136  /// |argument|value |
137  /// |--------|-----------------------------------------|
138  /// |lhs |SdfBooleanExpression representing `width`|
139  /// |op |BinaryOperator::GreaterThan |
140  /// |rhs |SdfBooleanExpression representing `10.0` |
141  /// \see MakeBinaryOp()
142  using BinaryVisitor = TfFunctionRef<void(SdfBooleanExpression const&,
144  SdfBooleanExpression const&)>;
145 
146  /// The Visit() method will invoke this callback if the receiver represents
147  /// an operator applied to a single subexpression
148  ///
149  /// Given the expression `!(width > 10.0)`, the callback would be invoked
150  /// with the arguments:
151  /// |argument |value |
152  /// |----------|------------------------------------------------|
153  /// |expression|SdfBooleanExpression representing `width > 10.0`|
154  /// |op | UnaryOp::Not |
155  /// \see MakeUnaryOp()
156  using UnaryVisitor = TfFunctionRef<void(SdfBooleanExpression const&,
158 
159  /// The Visit() method will invoke this callback if the receiver represents
160  /// a variable.
161  ///
162  /// \see MakeVariable()
164 
165  /// The Visit() method will invoke this callback if the receiver represents
166  /// a constant.
167  ///
168  /// \see MakeConstant()
170 
171  /// Invokes one of the given callbacks based on the type of the expression.
172  SDF_API void Visit(VariableVisitor variable,
173  ConstantVisitor constant,
175  UnaryVisitor unary) const;
176 
177  /// Provides the current value for a given variable. Used by Evaluate()
178  /// when evaluating the expression.
180 
181  /// Evaluates the expression. If the expression contains any variables,
182  /// \p variableCallback will be invoked to determine their current values.
183  SDF_API bool Evaluate(VariableCallback const& variableCallback) const;
184 
185  /// Encapsulates a transformation that may be applied to a variable name.
186  /// Used by RenameVariables().
188 
189  /// Applies the provided transform to each variable name and returns the
190  /// resulting expression.
191  SDF_API SdfBooleanExpression
193 
194  /// Determines if the provided string can be parsed as an expression.
195  /// Returns `true` if the expression is valid, otherwise returns `false`.
196  /// If the string is not a valid expression, \p errorMessage (if non-null)
197  /// will be filled with an explanatory error message.
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 
206 private:
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 
217 SDF_API std::ostream&
218 operator<<(std::ostream& os, SdfBooleanExpression::BinaryOperator const& rhs);
219 
220 SDF_API std::ostream&
221 operator<<(std::ostream& os, SdfBooleanExpression::UnaryOperator const& rhs);
222 
224 
225 #endif // PXR_USD_SDF_BOOLEAN_EXPRESSION_H
static SDF_API SdfBooleanExpression MakeBinaryOp(SdfBooleanExpression lhs, BinaryOperator op, SdfBooleanExpression rhs)
SDF_API bool Evaluate(VariableCallback const &variableCallback) const
void
Definition: png.h:1083
SDF_API SdfBooleanExpression RenameVariables(NameTransform const &transform) const
SDF_API std::string const & GetParseError() const
SDF_API std::string GetText() const
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
SDF_API friend std::ostream & operator<<(std::ostream &, SdfBooleanExpression const &)
SDF_API bool IsEmpty() const
TfFunctionRef< void(SdfBooleanExpression const &, BinaryOperator, SdfBooleanExpression const &)> BinaryVisitor
static SDF_API SdfBooleanExpression MakeUnaryOp(SdfBooleanExpression expression, UnaryOperator op)
Definition: token.h:70
static SDF_API bool Validate(std::string const &expression, std::string *errorMessage=nullptr)
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.
GA_API const UT_StringHolder transform
#define SDF_API
Definition: api.h:23
SDF_API std::set< TfToken > GetVariableNames() const
Provides the collection of variable names referenced by the expression.
SDF_API std::ostream & operator<<(std::ostream &os, SdfBooleanExpression::BinaryOperator const &rhs)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SdfBooleanExpression()=default
Constructs an empty expression.
static SDF_API SdfBooleanExpression MakeVariable(TfToken const &variableName)
static SDF_API SdfBooleanExpression MakeConstant(VtValue const &value)
Definition: value.h:89