forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue.cpp
More file actions
319 lines (272 loc) · 7.97 KB
/
Value.cpp
File metadata and controls
319 lines (272 loc) · 7.97 KB
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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXCore/Value.h>
#include <MaterialXCore/Util.h>
#include <iomanip>
#include <sstream>
#include <type_traits>
namespace MaterialX
{
Value::CreatorMap Value::_creatorMap;
Value::FloatFormat Value::_floatFormat = Value::FloatFormatDefault;
int Value::_floatPrecision = 6;
namespace {
template <class T> using enable_if_mx_vector_t =
typename std::enable_if<std::is_base_of<VectorBase, T>::value, T>::type;
template <class T> using enable_if_mx_matrix_t =
typename std::enable_if<std::is_base_of<MatrixBase, T>::value, T>::type;
template <class T> class is_std_vector : public std::false_type { };
template <class T> class is_std_vector< vector<T> > : public std::true_type { };
template <class T> using enable_if_std_vector_t =
typename std::enable_if<is_std_vector<T>::value, T>::type;
template <class T> void stringToData(const string& value, T& data)
{
std::stringstream ss(value);
if (!(ss >> data))
{
throw ExceptionTypeError("Type mismatch in generic stringToData: " + value);
}
}
template <> void stringToData(const string& str, bool& data)
{
if (str == VALUE_STRING_TRUE)
data = true;
else if (str == VALUE_STRING_FALSE)
data = false;
else
throw ExceptionTypeError("Type mismatch in boolean stringToData: " + str);
}
template <> void stringToData(const string& str, string& data)
{
data = str;
}
template <class T> void stringToData(const string& str, enable_if_mx_vector_t<T>& data)
{
StringVec tokens = splitString(str, ARRAY_VALID_SEPARATORS);
if (tokens.size() != data.numElements())
{
throw ExceptionTypeError("Type mismatch in vector stringToData: " + str);
}
for (size_t i = 0; i < data.numElements(); i++)
{
stringToData(tokens[i], data[i]);
}
}
template <class T> void stringToData(const string& str, enable_if_mx_matrix_t<T>& data)
{
StringVec tokens = splitString(str, ARRAY_VALID_SEPARATORS);
if (tokens.size() != data.numRows() * data.numColumns())
{
throw ExceptionTypeError("Type mismatch in matrix stringToData: " + str);
}
for (size_t i = 0; i < data.numRows(); i++)
{
for (size_t j = 0; j < data.numColumns(); j++)
{
stringToData(tokens[i * data.numRows() + j], data[i][j]);
}
}
}
template <class T> void stringToData(const string& str, enable_if_std_vector_t<T>& data)
{
for (const string& token : splitString(str, ARRAY_VALID_SEPARATORS))
{
typename T::value_type val;
stringToData(token, val);
data.push_back(val);
}
}
template <class T> void dataToString(const T& data, string& str)
{
std::stringstream ss;
// Set float format and precision for the stream
const Value::FloatFormat fmt = Value::getFloatFormat();
ss.setf(std::ios_base::fmtflags(
(fmt == Value::FloatFormatFixed ? std::ios_base::fixed :
(fmt == Value::FloatFormatScientific ? std::ios_base::scientific : 0))),
std::ios_base::floatfield);
ss.precision(Value::getFloatPrecision());
ss << data;
str = ss.str();
}
template <> void dataToString(const bool& data, string& str)
{
str = data ? VALUE_STRING_TRUE : VALUE_STRING_FALSE;
}
template <> void dataToString(const string& data, string& str)
{
str = data;
}
template <class T> void dataToString(const enable_if_mx_vector_t<T>& data, string& str)
{
for (size_t i = 0; i < data.numElements(); i++)
{
string token;
dataToString(data[i], token);
str += token;
if (i + 1 < data.numElements())
{
str += ARRAY_PREFERRED_SEPARATOR;
}
}
}
template <class T> void dataToString(const enable_if_mx_matrix_t<T>& data, string& str)
{
for (size_t i = 0; i < data.numRows(); i++)
{
for (size_t j = 0; j < data.numColumns(); j++)
{
string token;
dataToString(data[i][j], token);
str += token;
if (i + 1 < data.numRows() ||
j + 1 < data.numColumns())
{
str += ARRAY_PREFERRED_SEPARATOR;
}
}
}
}
template <class T> void dataToString(const enable_if_std_vector_t<T>& data, string& str)
{
for (size_t i = 0; i < data.size(); i++)
{
string token;
dataToString<typename T::value_type>(data[i], token);
str += token;
if (i + 1 < data.size())
{
str += ARRAY_PREFERRED_SEPARATOR;
}
}
}
} // anonymous namespace
//
// Global functions
//
template<class T> const string& getTypeString()
{
return TypedValue<T>::TYPE;
}
template <class T> string toValueString(const T& data)
{
string value;
dataToString<T>(data, value);
return value;
}
template <class T> T fromValueString(const string& value)
{
T data;
stringToData<T>(value, data);
return data;
}
//
// TypedValue methods
//
template <class T> const string& TypedValue<T>::getTypeString() const
{
return TYPE;
}
template <class T> string TypedValue<T>::getValueString() const
{
return toValueString<T>(_data);
}
template <class T> ValuePtr TypedValue<T>::createFromString(const string& value)
{
try
{
return Value::createValue<T>(fromValueString<T>(value));
}
catch (ExceptionTypeError&)
{
}
return ValuePtr();
}
//
// Value methods
//
ValuePtr Value::createValueFromStrings(const string& value, const string& type)
{
CreatorMap::iterator it = _creatorMap.find(type);
if (it != _creatorMap.end())
return it->second(value);
return TypedValue<string>::createFromString(value);
}
template<class T> bool Value::isA() const
{
return dynamic_cast<const TypedValue<T>*>(this) != nullptr;
}
template<class T> const T& Value::asA() const
{
const TypedValue<T>* typedVal = dynamic_cast<const TypedValue<T>*>(this);
if (!typedVal)
{
throw ExceptionTypeError("Incorrect type specified for value");
}
return typedVal->getData();
}
ScopedFloatFormatting::ScopedFloatFormatting(Value::FloatFormat format, int precision) :
_format(Value::getFloatFormat()),
_precision(Value::getFloatPrecision())
{
Value::setFloatFormat(format);
Value::setFloatPrecision(precision);
}
ScopedFloatFormatting::~ScopedFloatFormatting()
{
Value::setFloatFormat(_format);
Value::setFloatPrecision(_precision);
}
//
// Value registry class
//
template <class T> class ValueRegistry
{
public:
ValueRegistry()
{
if (!Value::_creatorMap.count(TypedValue<T>::TYPE))
{
Value::_creatorMap[TypedValue<T>::TYPE] = TypedValue<T>::createFromString;
}
}
~ValueRegistry() { }
};
//
// Template instantiations
//
using IntVec = vector<int>;
using BoolVec = vector<bool>;
using FloatVec = vector<float>;
#define INSTANTIATE_TYPE(T, name) \
template <> const string TypedValue<T>::TYPE = name; \
template bool Value::isA<T>() const; \
template const T& Value::asA<T>() const; \
template const string& getTypeString<T>(); \
template string toValueString(const T& data); \
template T fromValueString(const string& value); \
ValueRegistry<T> registry##T;
// Base types
INSTANTIATE_TYPE(int, "integer")
INSTANTIATE_TYPE(bool, "boolean")
INSTANTIATE_TYPE(float, "float")
INSTANTIATE_TYPE(Color2, "color2")
INSTANTIATE_TYPE(Color3, "color3")
INSTANTIATE_TYPE(Color4, "color4")
INSTANTIATE_TYPE(Vector2, "vector2")
INSTANTIATE_TYPE(Vector3, "vector3")
INSTANTIATE_TYPE(Vector4, "vector4")
INSTANTIATE_TYPE(Matrix33, "matrix33")
INSTANTIATE_TYPE(Matrix44, "matrix44")
INSTANTIATE_TYPE(string, "string")
// Array types
INSTANTIATE_TYPE(IntVec, "integerarray")
INSTANTIATE_TYPE(BoolVec, "booleanarray")
INSTANTIATE_TYPE(FloatVec, "floatarray")
INSTANTIATE_TYPE(StringVec, "stringarray")
// Alias types
INSTANTIATE_TYPE(long, "integer")
INSTANTIATE_TYPE(double, "float")
} // namespace MaterialX