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