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 
28 /// A shared pointer to a Value
29 using ValuePtr = shared_ptr<Value>;
30 /// A shared pointer to a const Value
31 using ConstValuePtr = shared_ptr<const Value>;
32 
33 template <class T> class TypedValue;
34 
35 /// @class ExceptionTypeError
36 /// An exception that is thrown when a type mismatch is encountered.
38 {
39  public:
41 };
42 
43 /// A generic, discriminated value, whose type may be queried dynamically.
45 {
46  public:
47  /// Float formats to use when converting values to strings.
49  {
50  FloatFormatDefault = 0,
51  FloatFormatFixed = 1,
52  FloatFormatScientific = 2
53  };
54 
55  public:
57  {
58  }
59  virtual ~Value() { }
60 
61  /// Create a new value from an object of any valid MaterialX type.
62  template <class T> static ValuePtr createValue(const T& data)
63  {
64  return std::make_shared<TypedValue<T>>(data);
65  }
66 
67  // Create a new value from a C-style string.
68  static ValuePtr createValue(const char* data)
69  {
70  return createValue(data ? string(data) : EMPTY_STRING);
71  }
72 
73  /// Create a new value instance from value and type strings.
74  /// @return A shared pointer to a typed value, or an empty shared pointer
75  /// if the conversion to the given data type cannot be performed.
76  static ValuePtr createValueFromStrings(const string& value, const string& type);
77 
78  /// Create a deep copy of the value.
79  virtual ValuePtr copy() const = 0;
80 
81  /// @name Data Accessors
82  /// @{
83 
84  /// Return true if this value is of the given type.
85  template <class T> bool isA() const;
86 
87  /// Return our underlying data as an object of the given type.
88  /// If the given type doesn't match our own data type, then an
89  /// exception is thrown.
90  template <class T> const T& asA() const;
91 
92  /// Return the type string for this value.
93  virtual const string& getTypeString() const = 0;
94 
95  /// Return the value string for this value.
96  virtual string getValueString() const = 0;
97 
98  /// Set float formatting for converting values to strings.
99  /// Formats to use are FloatFormatFixed, FloatFormatScientific
100  /// or FloatFormatDefault to set default format.
102  {
103  _floatFormat = format;
104  }
105 
106  /// Set float precision for converting values to strings.
107  static void setFloatPrecision(int precision)
108  {
109  _floatPrecision = precision;
110  }
111 
112  /// Return the current float format.
114  {
115  return _floatFormat;
116  }
117 
118  /// Return the current float precision.
119  static int getFloatPrecision()
120  {
121  return _floatPrecision;
122  }
123 
124  protected:
125  template <class T> friend class ValueRegistry;
126 
127  using CreatorFunction = ValuePtr (*)(const string&);
128  using CreatorMap = std::unordered_map<string, CreatorFunction>;
129 
130  private:
131  static CreatorMap _creatorMap;
132  static FloatFormat _floatFormat;
133  static int _floatPrecision;
134 };
135 
136 /// The class template for typed subclasses of Value
137 template <class T> class MX_CORE_API TypedValue : public Value
138 {
139  public:
141  _data{}
142  {
143  }
144  explicit TypedValue(const T& value) :
145  _data(value)
146  {
147  }
148  virtual ~TypedValue() { }
149 
150  /// Create a deep copy of the value.
151  ValuePtr copy() const override
152  {
153  return Value::createValue<T>(_data);
154  }
155 
156  /// Set stored data object.
157  void setData(const T& value)
158  {
159  _data = value;
160  }
161 
162  /// Set stored data object.
164  {
165  _data = value._data;
166  }
167 
168  /// Return stored data object.
169  const T& getData() const
170  {
171  return _data;
172  }
173 
174  /// Return type string.
175  const string& getTypeString() const override;
176 
177  /// Return value string.
178  string getValueString() const override;
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  static ValuePtr createFromString(const string& value);
188 
189  public:
190  static const string TYPE;
191 
192  private:
193  T _data;
194 };
195 
196 /// @class ScopedFloatFormatting
197 /// An RAII class for controlling the float formatting of values.
199 {
200  public:
203 
204  private:
205  Value::FloatFormat _format;
206  int _precision;
207 };
208 
209 /// Return the type string associated with the given data type.
210 template <class T> MX_CORE_API const string& getTypeString();
211 
212 /// Convert the given data value to a value string.
213 template <class T> MX_CORE_API string toValueString(const T& data);
214 
215 /// Convert the given value string to a data value of the given type.
216 /// @throws ExceptionTypeError if the conversion cannot be performed.
217 template <class T> MX_CORE_API T fromValueString(const string& value);
218 
219 /// Forward declaration of specific template instantiations.
220 /// Base types
232 
233 /// Array types
238 
239 /// Alias types
242 
244 
245 #endif
vector< bool > BoolVec
A vector of booleans.
Definition: Value.h:22
std::unordered_map< string, CreatorFunction > CreatorMap
Return true if this value is of the given type.
Definition: Value.h:128
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
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
static FloatFormat getFloatFormat()
Return the current float format.
Definition: Value.h:113
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:31
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
TypedValue()
Definition: Value.h:140
static ValuePtr createValue(const char *data)
Definition: Value.h:68
Value()
Definition: Value.h:56
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:33
void setData(const T &value)
Set stored data object.
Definition: Value.h:157
static void setFloatFormat(FloatFormat format)
Definition: Value.h:101
virtual const string & getTypeString() const =0
Return the type string for this value.
static const string TYPE
Definition: Value.h:190
void setData(const TypedValue< T > &value)
Set stored data object.
Definition: Value.h:163
A generic, discriminated value, whose type may be queried dynamically.
Definition: Value.h:44
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
virtual ~TypedValue()
Definition: Value.h:148
virtual ~Value()
Definition: Value.h:59
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:119
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:127
Exception(const string &msg)
Definition: Exception.h:24
ValuePtr copy() const override
Create a deep copy of the value.
Definition: Value.h:151
const T & getData() const
Return stored data object.
Definition: Value.h:169
Definition: core.h:1131
#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:62
vector< int > IntVec
A vector of integers.
Definition: Value.h:20
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
type
Definition: core.h:1059
static void setFloatPrecision(int precision)
Set float precision for converting values to strings.
Definition: Value.h:107
TypedValue(const T &value)
Definition: Value.h:144
FloatFormat
Float formats to use when converting values to strings.
Definition: Value.h:48
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
Definition: format.h:895