MaterialX 1.39.4
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
105 static void setFloatPrecision(int precision);
106
109
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
127template <class T> class MX_CORE_API TypedValue : public Value
128{
129 public:
130 TypedValue() :
131 _data{}
132 {
133 }
134 explicit TypedValue(const T& value) :
135 _data(value)
136 {
137 }
138 virtual ~TypedValue() { }
139
141 ValuePtr copy() const override
142 {
143 return Value::createValue<T>(_data);
144 }
145
147 void setData(const T& value)
148 {
149 _data = value;
150 }
151
153 void setData(const TypedValue<T>& value)
154 {
155 _data = value._data;
156 }
157
159 const T& getData() const
160 {
161 return _data;
162 }
163
165 const string& getTypeString() const override;
166
168 string getValueString() const override;
169
170 // Returns true if value data matches.
171 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
187 static ValuePtr createFromString(const string& value);
188
189 public:
190 static const string TYPE;
191
192 private:
193 T _data;
194};
195
197class MX_CORE_API AggregateValue : public Value
198{
199 public:
200 AggregateValue(const string& typeName) :
201 _typeName(typeName)
202 {
203 }
204 virtual ~AggregateValue() { }
205
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
219 {
220 _data.emplace_back(valuePtr);
221 }
222
223 const vector<ConstValuePtr>& getMembers() const
224 {
225 return _data;
226 }
227
229 ConstValuePtr getMemberValue(size_t index) const
230 {
231 return _data[index];
232 }
233
235 const string& getTypeString() const override { return _typeName; }
236
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
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
263class MX_CORE_API ScopedFloatFormatting
264{
265 public:
266 explicit ScopedFloatFormatting(Value::FloatFormat format, int precision = -1);
267 ~ScopedFloatFormatting();
268
269 private:
270 Value::FloatFormat _format;
271 int _precision;
272};
273
275template <class T> MX_CORE_API const string& getTypeString();
276
278template <class T> MX_CORE_API string toValueString(const T& data);
279
282template <class T> MX_CORE_API T fromValueString(const string& value);
283
287MX_CORE_API StringVec parseStructValueString(const string& value);
288
291MX_CORE_EXTERN_TEMPLATE(TypedValue<int>);
292MX_CORE_EXTERN_TEMPLATE(TypedValue<bool>);
293MX_CORE_EXTERN_TEMPLATE(TypedValue<float>);
294MX_CORE_EXTERN_TEMPLATE(TypedValue<Color3>);
295MX_CORE_EXTERN_TEMPLATE(TypedValue<Color4>);
296MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector2>);
297MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector3>);
298MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector4>);
299MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix33>);
300MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix44>);
301MX_CORE_EXTERN_TEMPLATE(TypedValue<string>);
302
304MX_CORE_EXTERN_TEMPLATE(TypedValue<IntVec>);
305MX_CORE_EXTERN_TEMPLATE(TypedValue<BoolVec>);
306MX_CORE_EXTERN_TEMPLATE(TypedValue<FloatVec>);
307MX_CORE_EXTERN_TEMPLATE(TypedValue<StringVec>);
308
310MX_CORE_EXTERN_TEMPLATE(TypedValue<long>);
311MX_CORE_EXTERN_TEMPLATE(TypedValue<double>);
312
313MATERIALX_NAMESPACE_END
314
315#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:61
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:198
ConstValuePtr getMemberValue(size_t index) const
Query an indexed member value from the aggregate.
Definition Value.h:229
void appendValue(ConstValuePtr valuePtr)
Append a member value to the aggregate.
Definition Value.h:218
ValuePtr copy() const override
Create a deep copy of the value.
Definition Value.h:207
static AggregateValuePtr createAggregateValue(const string &typeName)
Create a new value from an object of any valid MaterialX type.
Definition Value.h:248
string getValueString() const override
Return value string.
const string & getTypeString() const override
Return type string.
Definition Value.h:235
A type definition element within a Document.
Definition Definition.h:312
The class template for typed subclasses of Value.
Definition Value.h:128
const T & getData() const
Return stored data object.
Definition Value.h:159
void setData(const TypedValue< T > &value)
Set stored data object.
Definition Value.h:153
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:141
void setData(const T &value)
Set stored data object.
Definition Value.h:147
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.
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.
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.
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.