MaterialX 1.39.2
Loading...
Searching...
No Matches
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
11
13
14#include <MaterialXCore/Types.h>
15#include <MaterialXCore/Util.h>
16
17MATERIALX_NAMESPACE_BEGIN
18
20using IntVec = vector<int>;
22using BoolVec = vector<bool>;
24using FloatVec = vector<float>;
25
26class Value;
27class AggregateValue;
28
30using ValuePtr = shared_ptr<Value>;
32using ConstValuePtr = shared_ptr<const Value>;
33
35using AggregateValuePtr = shared_ptr<AggregateValue>;
37using ConstAggregateValuePtr = shared_ptr<const AggregateValue>;
38
39class TypeDef;
40using ConstTypeDefPtr = shared_ptr<const TypeDef>;
41
42template <class T> class TypedValue;
43
45class MX_CORE_API Value
46{
47 public:
50 {
51 FloatFormatDefault = 0,
52 FloatFormatFixed = 1,
53 FloatFormatScientific = 2
54 };
55
56 public:
57 Value()
58 {
59 }
60 virtual ~Value() { }
61
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
77 static ValuePtr createValueFromStrings(const string& value, const string& type, ConstTypeDefPtr typeDef = nullptr);
78
80 virtual ValuePtr copy() const = 0;
81
84
86 template <class T> bool isA() const;
87
91 template <class T> const T& asA() const;
92
94 virtual const string& getTypeString() const = 0;
95
97 virtual string getValueString() const = 0;
98
102 static void setFloatFormat(FloatFormat format)
103 {
104 _floatFormat = format;
105 }
106
108 static void setFloatPrecision(int precision)
109 {
110 _floatPrecision = precision;
111 }
112
115 {
116 return _floatFormat;
117 }
118
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
138template <class T> class MX_CORE_API TypedValue : public Value
139{
140 public:
141 TypedValue() :
142 _data{}
143 {
144 }
145 explicit TypedValue(const T& value) :
146 _data(value)
147 {
148 }
149 virtual ~TypedValue() { }
150
152 ValuePtr copy() const override
153 {
154 return Value::createValue<T>(_data);
155 }
156
158 void setData(const T& value)
159 {
160 _data = value;
161 }
162
164 void setData(const TypedValue<T>& value)
165 {
166 _data = value._data;
167 }
168
170 const T& getData() const
171 {
172 return _data;
173 }
174
176 const string& getTypeString() const override;
177
179 string getValueString() const override;
180
181 //
182 // Static helper methods
183 //
184
188 static ValuePtr createFromString(const string& value);
189
190 public:
191 static const string TYPE;
192
193 private:
194 T _data;
195};
196
198class MX_CORE_API AggregateValue : public Value
199{
200 public:
201 AggregateValue(const string& typeName) :
202 _typeName(typeName)
203 {
204 }
205 virtual ~AggregateValue() { }
206
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
220 {
221 _data.emplace_back(valuePtr);
222 }
223
224 const vector<ConstValuePtr>& getMembers() const
225 {
226 return _data;
227 }
228
230 ConstValuePtr getMemberValue(size_t index) const
231 {
232 return _data[index];
233 }
234
236 const string& getTypeString() const override { return _typeName; }
237
239 string getValueString() const override;
240
241 //
242 // Static helper methods
243 //
244
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
261class MX_CORE_API ScopedFloatFormatting
262{
263 public:
264 explicit ScopedFloatFormatting(Value::FloatFormat format, int precision = -1);
265 ~ScopedFloatFormatting();
266
267 private:
268 Value::FloatFormat _format;
269 int _precision;
270};
271
273template <class T> MX_CORE_API const string& getTypeString();
274
276template <class T> MX_CORE_API string toValueString(const T& data);
277
280template <class T> MX_CORE_API T fromValueString(const string& value);
281
285MX_CORE_API StringVec parseStructValueString(const string& value);
286
289MX_CORE_EXTERN_TEMPLATE(TypedValue<int>);
290MX_CORE_EXTERN_TEMPLATE(TypedValue<bool>);
291MX_CORE_EXTERN_TEMPLATE(TypedValue<float>);
292MX_CORE_EXTERN_TEMPLATE(TypedValue<Color3>);
293MX_CORE_EXTERN_TEMPLATE(TypedValue<Color4>);
294MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector2>);
295MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector3>);
296MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector4>);
297MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix33>);
298MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix44>);
299MX_CORE_EXTERN_TEMPLATE(TypedValue<string>);
300
302MX_CORE_EXTERN_TEMPLATE(TypedValue<IntVec>);
303MX_CORE_EXTERN_TEMPLATE(TypedValue<BoolVec>);
304MX_CORE_EXTERN_TEMPLATE(TypedValue<FloatVec>);
305MX_CORE_EXTERN_TEMPLATE(TypedValue<StringVec>);
306
308MX_CORE_EXTERN_TEMPLATE(TypedValue<long>);
309MX_CORE_EXTERN_TEMPLATE(TypedValue<double>);
310
311MATERIALX_NAMESPACE_END
312
313#endif
shared_ptr< const TypeDef > ConstTypeDefPtr
A shared pointer to a const TypeDef.
Definition Definition.h:44
Base exception classes.
vector< string > StringVec
A vector of strings.
Definition Library.h:60
Data type classes.
Utility methods.
MX_CORE_API StringVec parseStructValueString(const string &value)
Tokenize the string representation of a struct value i.e, "{1;2;3}" into a vector of substrings.
shared_ptr< AggregateValue > AggregateValuePtr
A shared pointer to an Aggregate Value.
Definition Value.h:35
vector< bool > BoolVec
A vector of booleans.
Definition Value.h:22
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition Value.h:32
MX_CORE_API T fromValueString(const string &value)
Convert the given value string to a data value of the given type.
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
MX_CORE_API string toValueString(const T &data)
Convert the given data value to a value string.
vector< float > FloatVec
A vector of floats.
Definition Value.h:24
shared_ptr< const AggregateValue > ConstAggregateValuePtr
A shared pointer to a const Aggregate Value.
Definition Value.h:37
MX_CORE_API const string & getTypeString()
Return the type string associated with the given data type.
A subclass for aggregate values with multiple members.
Definition Value.h:199
ConstValuePtr getMemberValue(size_t index) const
Query an indexed member value from the aggregate.
Definition Value.h:230
void appendValue(ConstValuePtr valuePtr)
Append a member value to the aggregate.
Definition Value.h:219
ValuePtr copy() const override
Create a deep copy of the value.
Definition Value.h:208
static AggregateValuePtr createAggregateValue(const string &typeName)
Create a new value from an object of any valid MaterialX type.
Definition Value.h:246
string getValueString() const override
Return value string.
const string & getTypeString() const override
Return type string.
Definition Value.h:236
A type definition element within a Document.
Definition Definition.h:312
The class template for typed subclasses of Value.
Definition Value.h:139
const T & getData() const
Return stored data object.
Definition Value.h:170
void setData(const TypedValue< T > &value)
Set stored data object.
Definition Value.h:164
static ValuePtr createFromString(const string &value)
Create a new value of this type from a value string.
ValuePtr copy() const override
Create a deep copy of the value.
Definition Value.h:152
void setData(const T &value)
Set stored data object.
Definition Value.h:158
string getValueString() const override
Return value string.
const string & getTypeString() const override
Return type string.
A generic, discriminated value, whose type may be queried dynamically.
Definition Value.h:46
static ValuePtr createValueFromStrings(const string &value, const string &type, ConstTypeDefPtr typeDef=nullptr)
Create a new value instance from value and type strings.
virtual const string & getTypeString() const =0
Return the type string for this value.
static void setFloatFormat(FloatFormat format)
Set float formatting for converting values to strings.
Definition Value.h:102
virtual string getValueString() const =0
Return the value string for this value.
bool isA() const
Return true if this value is of the given type.
static FloatFormat getFloatFormat()
Return the current float format.
Definition Value.h:114
const T & asA() const
Return our underlying data as an object of the given type.
virtual ValuePtr copy() const =0
Create a deep copy of the value.
FloatFormat
Float formats to use when converting values to strings.
Definition Value.h:50
static void setFloatPrecision(int precision)
Set float precision for converting values to strings.
Definition Value.h:108
static ValuePtr createValue(const T &data)
Create a new value from an object of any valid MaterialX type.
Definition Value.h:63
static int getFloatPrecision()
Return the current float precision.
Definition Value.h:120