HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Value.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_VALUE_H
7 #define MATERIALX_VALUE_H
8 
9 /// @file
10 /// Generic value classes
11 
13 
14 #include <MaterialXCore/Types.h>
15 #include <MaterialXCore/Util.h>
16 
18 
19 /// A vector of integers.
20 using IntVec = vector<int>;
21 /// A vector of booleans.
22 using BoolVec = vector<bool>;
23 /// A vector of floats.
24 using FloatVec = vector<float>;
25 
26 class Value;
27 class AggregateValue;
28 
29 /// A shared pointer to a Value
30 using ValuePtr = shared_ptr<Value>;
31 /// A shared pointer to a const Value
32 using ConstValuePtr = shared_ptr<const Value>;
33 
34 /// A shared pointer to an Aggregate Value
35 using AggregateValuePtr = shared_ptr<AggregateValue>;
36 /// A shared pointer to a const Aggregate Value
37 using ConstAggregateValuePtr = shared_ptr<const AggregateValue>;
38 
39 class TypeDef;
40 using ConstTypeDefPtr = shared_ptr<const TypeDef>;
41 
42 template <class T> class TypedValue;
43 
44 /// A generic, discriminated value, whose type may be queried dynamically.
46 {
47  public:
48  /// Float formats to use when converting values to strings.
50  {
51  FloatFormatDefault = 0,
52  FloatFormatFixed = 1,
53  FloatFormatScientific = 2
54  };
55 
56  public:
58  {
59  }
60  virtual ~Value() { }
61 
62  /// Create a new value from an object of any valid MaterialX type.
63  template <class T> static ValuePtr createValue(const T& data)
64  {
65  return std::make_shared<TypedValue<T>>(data);
66  }
67 
68  // Create a new value from a C-style string.
69  static ValuePtr createValue(const char* data)
70  {
71  return createValue(data ? string(data) : EMPTY_STRING);
72  }
73 
74  /// Create a new value instance from value and type strings.
75  /// @return A shared pointer to a typed value, or an empty shared pointer
76  /// if the conversion to the given data type cannot be performed.
77  static ValuePtr createValueFromStrings(const string& value, const string& type, ConstTypeDefPtr typeDef = nullptr);
78 
79  /// Create a deep copy of the value.
80  virtual ValuePtr copy() const = 0;
81 
82  /// @name Data Accessors
83  /// @{
84 
85  /// Return true if this value is of the given type.
86  template <class T> bool isA() const;
87 
88  /// Return our underlying data as an object of the given type.
89  /// If the given type doesn't match our own data type, then an
90  /// exception is thrown.
91  template <class T> const T& asA() const;
92 
93  /// Return the type string for this value.
94  virtual const string& getTypeString() const = 0;
95 
96  /// Return the value string for this value.
97  virtual string getValueString() const = 0;
98 
99  /// Set float formatting for converting values to strings.
100  /// Formats to use are FloatFormatFixed, FloatFormatScientific
101  /// or FloatFormatDefault to set default format.
102  static void setFloatFormat(FloatFormat format);
103 
104  /// Set float precision for converting values to strings.
105  static void setFloatPrecision(int precision);
106 
107  /// Return the current float format.
108  static FloatFormat getFloatFormat();
109 
110  /// Return the current float precision.
111  static int getFloatPrecision();
112 
113  // Returns true if value data matches.
114  virtual bool isEqual(ConstValuePtr other) const = 0;
115 
116  protected:
117  template <class T> friend class ValueRegistry;
118 
119  using CreatorFunction = ValuePtr (*)(const string&);
120  using CreatorMap = std::unordered_map<string, CreatorFunction>;
121 
122  private:
123  static CreatorMap _creatorMap;
124 };
125 
126 /// The class template for typed subclasses of Value
127 template <class T> class TypedValue : public Value
128 {
129  public:
131  _data{}
132  {
133  }
134  MX_CORE_API explicit TypedValue(const T& value) :
135  _data(value)
136  {
137  }
138  MX_CORE_API virtual ~TypedValue() { }
139 
140  /// Create a deep copy of the value.
141  MX_CORE_API ValuePtr copy() const override
142  {
143  return Value::createValue<T>(_data);
144  }
145 
146  /// Set stored data object.
147  MX_CORE_API void setData(const T& value)
148  {
149  _data = value;
150  }
151 
152  /// Set stored data object.
154  {
155  _data = value._data;
156  }
157 
158  /// Return stored data object.
159  MX_CORE_API const T& getData() const
160  {
161  return _data;
162  }
163 
164  /// Return type string.
165  MX_CORE_API const string& getTypeString() const override;
166 
167  /// Return value string.
168  MX_CORE_API string getValueString() const override;
169 
170  // Returns true if value data matches.
171  MX_CORE_API bool isEqual(ConstValuePtr other) const override
172  {
173  if (!other || !other->isA<T>())
174  {
175  return false;
176  }
177  return _data == other->asA<T>();
178  }
179 
180  //
181  // Static helper methods
182  //
183 
184  /// Create a new value of this type from a value string.
185  /// @return A shared pointer to a typed value, or an empty shared pointer
186  /// if the conversion to the given data type cannot be performed.
187  MX_CORE_API static ValuePtr createFromString(const string& value);
188 
189  public:
190  MX_CORE_API static const string TYPE;
191 
192  private:
193  T _data;
194 };
195 
196 /// A subclass for aggregate values with multiple members
198 {
199  public:
200  AggregateValue(const string& typeName) :
201  _typeName(typeName)
202  {
203  }
204  virtual ~AggregateValue() { }
205 
206  /// Create a deep copy of the value.
207  ValuePtr copy() const override
208  {
209  auto result = createAggregateValue(_typeName);
210  for (const auto& val : _data)
211  {
212  result->appendValue(val->copy());
213  }
214  return result;
215  }
216 
217  /// Append a member value to the aggregate.
218  void appendValue(ConstValuePtr valuePtr)
219  {
220  _data.emplace_back(valuePtr);
221  }
222 
223  const vector<ConstValuePtr>& getMembers() const
224  {
225  return _data;
226  }
227 
228  /// Query an indexed member value from the aggregate.
230  {
231  return _data[index];
232  }
233 
234  /// Return type string.
235  const string& getTypeString() const override { return _typeName; }
236 
237  /// Return value string.
238  string getValueString() const override;
239 
240  // Returns true if value data matches.
241  bool isEqual(ConstValuePtr other) const override;
242 
243  //
244  // Static helper methods
245  //
246 
247  /// Create a new value from an object of any valid MaterialX type.
248  static AggregateValuePtr createAggregateValue(const string& typeName)
249  {
250  return std::make_shared<AggregateValue>(typeName);
251  }
252 
253  static AggregateValuePtr createAggregateValueFromString(const string& value, const string& type, ConstTypeDefPtr typeDefPtr);
254 
255  private:
256  const string _typeName;
257 
258  vector<ConstValuePtr> _data;
259 };
260 
261 /// @class ScopedFloatFormatting
262 /// An RAII class for controlling the float formatting of values.
264 {
265  public:
268 
269  private:
270  Value::FloatFormat _format;
271  int _precision;
272 };
273 
274 /// Return the type string associated with the given data type.
275 template <class T> MX_CORE_API const string& getTypeString();
276 
277 /// Convert the given data value to a value string.
278 template <class T> MX_CORE_API string toValueString(const T& data);
279 
280 /// Convert the given value string to a data value of the given type.
281 /// @throws ExceptionTypeError if the conversion cannot be performed.
282 template <class T> MX_CORE_API T fromValueString(const string& value);
283 
284 /// Tokenize the string representation of a struct value i.e, "{1;2;3}" into a
285 /// vector of substrings.
286 /// Note: "{1;2;{3;4;5}}" will be split in to ["1", "2", "{3;4;5}"]
288 
289 /// Forward declaration of specific template instantiations.
290 /// Base types
302 
303 /// Array types
308 
309 /// Alias types
312 
314 
315 #endif
vector< bool > BoolVec
A vector of booleans.
Definition: Value.h:22
MX_CORE_API ValuePtr copy() const override
Create a deep copy of the value.
Definition: Value.h:141
shared_ptr< const TypeDef > ConstTypeDefPtr
A shared pointer to a const TypeDef.
Definition: Definition.h:44
std::unordered_map< string, CreatorFunction > CreatorMap
Return true if this value is of the given type.
Definition: Value.h:120
ValuePtr copy() const override
Create a deep copy of the value.
Definition: Value.h:207
const vector< ConstValuePtr > & getMembers() const
Definition: Value.h:223
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:61
GLboolean * data
Definition: glcorearb.h:131
virtual string getValueString() const =0
Return the value string for this value.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
SYS_FORCE_INLINE bool isA(const InstancablePtr *o)
static MX_CORE_API const string TYPE
Definition: Value.h:190
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:32
virtual MX_CORE_API ~TypedValue()
Definition: Value.h:138
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
static ValuePtr createValue(const char *data)
Definition: Value.h:69
Value()
Definition: Value.h:57
**But if you need a result
Definition: thread.h:622
MX_CORE_API const string & getTypeString()
Return the type string associated with the given data type.
#define MX_CORE_API
Definition: Export.h:18
The class template for typed subclasses of Value.
Definition: Value.h:42
MX_CORE_API bool isEqual(ConstValuePtr other) const override
Return true if this value is of the given type.
Definition: Value.h:171
MX_CORE_API TypedValue()
Definition: Value.h:130
MX_CORE_API const string & getTypeString() const override
Return type string.
MX_CORE_API TypedValue(const T &value)
Definition: Value.h:134
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
A generic, discriminated value, whose type may be queried dynamically.
Definition: Value.h:45
shared_ptr< AggregateValue > AggregateValuePtr
A shared pointer to an Aggregate Value.
Definition: Value.h:35
static AggregateValuePtr createAggregateValue(const string &typeName)
Create a new value from an object of any valid MaterialX type.
Definition: Value.h:248
AggregateValue(const string &typeName)
Definition: Value.h:200
virtual bool isEqual(ConstValuePtr other) const =0
Return true if this value is of the given type.
void appendValue(ConstValuePtr valuePtr)
Append a member value to the aggregate.
Definition: Value.h:218
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
virtual ~Value()
Definition: Value.h:60
vector< float > FloatVec
A vector of floats.
Definition: Value.h:24
MX_CORE_API T fromValueString(const string &value)
GLenum GLint GLint * precision
Definition: glcorearb.h:1925
MX_CORE_EXTERN_TEMPLATE(TypedValue< int >)
ValuePtr(*)(const string &) CreatorFunction
Return true if this value is of the given type.
Definition: Value.h:119
ConstValuePtr getMemberValue(size_t index) const
Query an indexed member value from the aggregate.
Definition: Value.h:229
GLuint index
Definition: glcorearb.h:786
MX_CORE_API const T & getData() const
Return stored data object.
Definition: Value.h:159
GLuint GLfloat * val
Definition: glcorearb.h:1608
MX_CORE_API StringVec parseStructValueString(const string &value)
const string & getTypeString() const override
Return type string.
Definition: Value.h:235
shared_ptr< const AggregateValue > ConstAggregateValuePtr
A shared pointer to a const Aggregate Value.
Definition: Value.h:37
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
static ValuePtr createValue(const T &data)
Create a new value from an object of any valid MaterialX type.
Definition: Value.h:63
MX_CORE_API string getValueString() const override
Return value string.
vector< int > IntVec
A vector of integers.
Definition: Value.h:20
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
FloatFormat
Float formats to use when converting values to strings.
Definition: Value.h:49
static MX_CORE_API ValuePtr createFromString(const string &value)
A subclass for aggregate values with multiple members.
Definition: Value.h:197
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
MX_CORE_API void setData(const TypedValue< T > &value)
Set stored data object.
Definition: Value.h:153
Definition: format.h:1821
virtual ~AggregateValue()
Definition: Value.h:204
MX_CORE_API void setData(const T &value)
Set stored data object.
Definition: Value.h:147