forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue.h
More file actions
245 lines (196 loc) · 6.42 KB
/
Value.h
File metadata and controls
245 lines (196 loc) · 6.42 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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#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;
/// A shared pointer to a Value
using ValuePtr = shared_ptr<Value>;
/// A shared pointer to a const Value
using ConstValuePtr = shared_ptr<const Value>;
template <class T> class TypedValue;
/// @class ExceptionTypeError
/// An exception that is thrown when a type mismatch is encountered.
class MX_CORE_API ExceptionTypeError : public Exception
{
public:
using Exception::Exception;
};
/// 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);
/// 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;
}
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;
//
// 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;
};
/// @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 = 6);
~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);
/// 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