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.
103  {
104  _floatFormat = format;
105  }
106 
107  /// Set float precision for converting values to strings.
108  static void setFloatPrecision(int precision)
109  {
110  _floatPrecision = precision;
111  }
112 
113  /// Return the current float format.
115  {
116  return _floatFormat;
117  }
118 
119  /// Return the current float precision.
120  static int getFloatPrecision()
121  {
122  return _floatPrecision;
123  }
124 
125  protected:
126  template <class T> friend class ValueRegistry;
127 
128  using CreatorFunction = ValuePtr (*)(const string&);
129  using CreatorMap = std::unordered_map<string, CreatorFunction>;
130 
131  private:
132  static CreatorMap _creatorMap;
133  static FloatFormat _floatFormat;
134  static int _floatPrecision;
135 };
136 
137 /// The class template for typed subclasses of Value
138 template <class T> class MX_CORE_API TypedValue : public Value
139 {
140  public:
142  _data{}
143  {
144  }
145  explicit TypedValue(const T& value) :
146  _data(value)
147  {
148  }
149  virtual ~TypedValue() { }
150 
151  /// Create a deep copy of the value.
152  ValuePtr copy() const override
153  {
154  return Value::createValue<T>(_data);
155  }
156 
157  /// Set stored data object.
158  void setData(const T& value)
159  {
160  _data = value;
161  }
162 
163  /// Set stored data object.
165  {
166  _data = value._data;
167  }
168 
169  /// Return stored data object.
170  const T& getData() const
171  {
172  return _data;
173  }
174 
175  /// Return type string.
176  const string& getTypeString() const override;
177 
178  /// Return value string.
179  string getValueString() const override;
180 
181  //
182  // Static helper methods
183  //
184 
185  /// Create a new value of this type from a value string.
186  /// @return A shared pointer to a typed value, or an empty shared pointer
187  /// if the conversion to the given data type cannot be performed.
188  static ValuePtr createFromString(const string& value);
189 
190  public:
191  static const string TYPE;
192 
193  private:
194  T _data;
195 };
196 
197 /// A subclass for aggregate values with multiple members
199 {
200  public:
201  AggregateValue(const string& typeName) :
202  _typeName(typeName)
203  {
204  }
205  virtual ~AggregateValue() { }
206 
207  /// Create a deep copy of the value.
208  ValuePtr copy() const override
209  {
210  auto result = createAggregateValue(_typeName);
211  for (const auto& val : _data)
212  {
213  result->appendValue(val->copy());
214  }
215  return result;
216  }
217 
218  /// Append a member value to the aggregate.
219  void appendValue(ConstValuePtr valuePtr)
220  {
221  _data.emplace_back(valuePtr);
222  }
223 
224  const vector<ConstValuePtr>& getMembers() const
225  {
226  return _data;
227  }
228 
229  /// Query an indexed member value from the aggregate.
231  {
232  return _data[index];
233  }
234 
235  /// Return type string.
236  const string& getTypeString() const override { return _typeName; }
237 
238  /// Return value string.
239  string getValueString() const override;
240 
241  //
242  // Static helper methods
243  //
244 
245  /// Create a new value from an object of any valid MaterialX type.
246  static AggregateValuePtr createAggregateValue(const string& typeName)
247  {
248  return std::make_shared<AggregateValue>(typeName);
249  }
250 
251  static AggregateValuePtr createAggregateValueFromString(const string& value, const string& type, ConstTypeDefPtr typeDefPtr);
252 
253  private:
254  const string _typeName;
255 
256  vector<ConstValuePtr> _data;
257 };
258 
259 /// @class ScopedFloatFormatting
260 /// An RAII class for controlling the float formatting of values.
262 {
263  public:
266 
267  private:
268  Value::FloatFormat _format;
269  int _precision;
270 };
271 
272 /// Return the type string associated with the given data type.
273 template <class T> MX_CORE_API const string& getTypeString();
274 
275 /// Convert the given data value to a value string.
276 template <class T> MX_CORE_API string toValueString(const T& data);
277 
278 /// Convert the given value string to a data value of the given type.
279 /// @throws ExceptionTypeError if the conversion cannot be performed.
280 template <class T> MX_CORE_API T fromValueString(const string& value);
281 
282 /// Tokenize the string representation of a struct value i.e, "{1;2;3}" into a
283 /// vector of substrings.
284 /// Note: "{1;2;{3;4;5}}" will be split in to ["1", "2", "{3;4;5}"]
286 
287 /// Forward declaration of specific template instantiations.
288 /// Base types
300 
301 /// Array types
306 
307 /// Alias types
310 
312 
313 #endif
vector< bool > BoolVec
A vector of booleans.
Definition: Value.h:22
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:129
ValuePtr copy() const override
Create a deep copy of the value.
Definition: Value.h:208
const vector< ConstValuePtr > & getMembers() const
Definition: Value.h:224
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:60
GLboolean * data
Definition: glcorearb.h:131
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
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 FloatFormat getFloatFormat()
Return the current float format.
Definition: Value.h:114
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:32
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
TypedValue()
Definition: Value.h:141
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
void setData(const T &value)
Set stored data object.
Definition: Value.h:158
static void setFloatFormat(FloatFormat format)
Definition: Value.h:102
virtual const string & getTypeString() const =0
Return the type string for this value.
static const string TYPE
Definition: Value.h:191
void setData(const TypedValue< T > &value)
Set stored data object.
Definition: Value.h:164
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:246
AggregateValue(const string &typeName)
Definition: Value.h:201
void appendValue(ConstValuePtr valuePtr)
Append a member value to the aggregate.
Definition: Value.h:219
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
virtual ~TypedValue()
Definition: Value.h:149
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)
static int getFloatPrecision()
Return the current float precision.
Definition: Value.h:120
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:128
ConstValuePtr getMemberValue(size_t index) const
Query an indexed member value from the aggregate.
Definition: Value.h:230
GLuint index
Definition: glcorearb.h:786
ValuePtr copy() const override
Create a deep copy of the value.
Definition: Value.h:152
GLuint GLfloat * val
Definition: glcorearb.h:1608
const T & getData() const
Return stored data object.
Definition: Value.h:170
MX_CORE_API StringVec parseStructValueString(const string &value)
const string & getTypeString() const override
Return type string.
Definition: Value.h:236
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
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
static void setFloatPrecision(int precision)
Set float precision for converting values to strings.
Definition: Value.h:108
TypedValue(const T &value)
Definition: Value.h:145
FloatFormat
Float formats to use when converting values to strings.
Definition: Value.h:49
A subclass for aggregate values with multiple members.
Definition: Value.h:198
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
Definition: format.h:1821
virtual ~AggregateValue()
Definition: Value.h:205