MaterialX 1.38.2
Value.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
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
17namespace MaterialX
18{
19
21using IntVec = vector<int>;
23using BoolVec = vector<bool>;
25using FloatVec = vector<float>;
26
27class Value;
28
30using ValuePtr = shared_ptr<Value>;
32using ConstValuePtr = shared_ptr<const Value>;
33
34template <class T> class TypedValue;
35
38class MX_CORE_API ExceptionTypeError : public Exception
39{
40 public:
41 using Exception::Exception;
42};
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);
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
199class MX_CORE_API ScopedFloatFormatting
200{
201 public:
202 explicit ScopedFloatFormatting(Value::FloatFormat format, int precision = 6);
204
205 private:
206 Value::FloatFormat _format;
207 int _precision;
208};
209
211template<class T> MX_CORE_API const string& getTypeString();
212
214template <class T> MX_CORE_API string toValueString(const T& data);
215
218template <class T> MX_CORE_API T fromValueString(const string& value);
219
222MX_CORE_EXTERN_TEMPLATE(TypedValue<int>);
223MX_CORE_EXTERN_TEMPLATE(TypedValue<bool>);
224MX_CORE_EXTERN_TEMPLATE(TypedValue<float>);
225MX_CORE_EXTERN_TEMPLATE(TypedValue<Color3>);
226MX_CORE_EXTERN_TEMPLATE(TypedValue<Color4>);
227MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector2>);
228MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector3>);
229MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector4>);
230MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix33>);
231MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix44>);
232MX_CORE_EXTERN_TEMPLATE(TypedValue<string>);
233
235MX_CORE_EXTERN_TEMPLATE(TypedValue<IntVec>);
236MX_CORE_EXTERN_TEMPLATE(TypedValue<BoolVec>);
237MX_CORE_EXTERN_TEMPLATE(TypedValue<FloatVec>);
238MX_CORE_EXTERN_TEMPLATE(TypedValue<StringVec>);
239
241MX_CORE_EXTERN_TEMPLATE(TypedValue<long>);
242MX_CORE_EXTERN_TEMPLATE(TypedValue<double>);
243
244} // namespace MaterialX
245
246#endif
Base exception classes.
Data type classes.
Utility methods.
vector< bool > BoolVec
A vector of booleans.
Definition: Value.h:23
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:32
vector< int > IntVec
A vector of integers.
Definition: Value.h:21
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
vector< float > FloatVec
A vector of floats.
Definition: Value.h:25
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:23
An exception that is thrown when a type mismatch is encountered.
Definition: Value.h:39
An RAII class for controlling the float formatting of values.
Definition: Value.h:200
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
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
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.
static FloatFormat getFloatFormat()
Return the current float format.
Definition: Value.h:114
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