MaterialX 1.39.1
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;
27
29using ValuePtr = shared_ptr<Value>;
31using ConstValuePtr = shared_ptr<const Value>;
32
33template <class T> class TypedValue;
34
37class MX_CORE_API ExceptionTypeError : public Exception
38{
39 public:
40 using Exception::Exception;
41};
42
44class MX_CORE_API Value
45{
46 public:
49 {
50 FloatFormatDefault = 0,
51 FloatFormatFixed = 1,
52 FloatFormatScientific = 2
53 };
54
55 public:
56 Value()
57 {
58 }
59 virtual ~Value() { }
60
62 template <class T> static ValuePtr createValue(const T& data)
63 {
64 return std::make_shared<TypedValue<T>>(data);
65 }
66
67 // Create a new value from a C-style string.
68 static ValuePtr createValue(const char* data)
69 {
70 return createValue(data ? string(data) : EMPTY_STRING);
71 }
72
76 static ValuePtr createValueFromStrings(const string& value, const string& type);
77
79 virtual ValuePtr copy() const = 0;
80
83
85 template <class T> bool isA() const;
86
90 template <class T> const T& asA() const;
91
93 virtual const string& getTypeString() const = 0;
94
96 virtual string getValueString() const = 0;
97
101 static void setFloatFormat(FloatFormat format)
102 {
103 _floatFormat = format;
104 }
105
107 static void setFloatPrecision(int precision)
108 {
109 _floatPrecision = precision;
110 }
111
114 {
115 return _floatFormat;
116 }
117
119 static int getFloatPrecision()
120 {
121 return _floatPrecision;
122 }
123
124 protected:
125 template <class T> friend class ValueRegistry;
126
127 using CreatorFunction = ValuePtr (*)(const string&);
128 using CreatorMap = std::unordered_map<string, CreatorFunction>;
129
130 private:
131 static CreatorMap _creatorMap;
132 static FloatFormat _floatFormat;
133 static int _floatPrecision;
134};
135
137template <class T> class MX_CORE_API TypedValue : public Value
138{
139 public:
140 TypedValue() :
141 _data{}
142 {
143 }
144 explicit TypedValue(const T& value) :
145 _data(value)
146 {
147 }
148 virtual ~TypedValue() { }
149
151 ValuePtr copy() const override
152 {
153 return Value::createValue<T>(_data);
154 }
155
157 void setData(const T& value)
158 {
159 _data = value;
160 }
161
163 void setData(const TypedValue<T>& value)
164 {
165 _data = value._data;
166 }
167
169 const T& getData() const
170 {
171 return _data;
172 }
173
175 const string& getTypeString() const override;
176
178 string getValueString() const override;
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
198class MX_CORE_API ScopedFloatFormatting
199{
200 public:
201 explicit ScopedFloatFormatting(Value::FloatFormat format, int precision = -1);
203
204 private:
205 Value::FloatFormat _format;
206 int _precision;
207};
208
210template <class T> MX_CORE_API const string& getTypeString();
211
213template <class T> MX_CORE_API string toValueString(const T& data);
214
217template <class T> MX_CORE_API T fromValueString(const string& value);
218
221MX_CORE_EXTERN_TEMPLATE(TypedValue<int>);
222MX_CORE_EXTERN_TEMPLATE(TypedValue<bool>);
223MX_CORE_EXTERN_TEMPLATE(TypedValue<float>);
224MX_CORE_EXTERN_TEMPLATE(TypedValue<Color3>);
225MX_CORE_EXTERN_TEMPLATE(TypedValue<Color4>);
226MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector2>);
227MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector3>);
228MX_CORE_EXTERN_TEMPLATE(TypedValue<Vector4>);
229MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix33>);
230MX_CORE_EXTERN_TEMPLATE(TypedValue<Matrix44>);
231MX_CORE_EXTERN_TEMPLATE(TypedValue<string>);
232
234MX_CORE_EXTERN_TEMPLATE(TypedValue<IntVec>);
235MX_CORE_EXTERN_TEMPLATE(TypedValue<BoolVec>);
236MX_CORE_EXTERN_TEMPLATE(TypedValue<FloatVec>);
237MX_CORE_EXTERN_TEMPLATE(TypedValue<StringVec>);
238
240MX_CORE_EXTERN_TEMPLATE(TypedValue<long>);
241MX_CORE_EXTERN_TEMPLATE(TypedValue<double>);
242
243MATERIALX_NAMESPACE_END
244
245#endif
Base exception classes.
Data type classes.
Utility methods.
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:31
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:29
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
MX_CORE_API const string & getTypeString()
Return the type string associated with the given data type.
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:22
An exception that is thrown when a type mismatch is encountered.
Definition: Value.h:38
An RAII class for controlling the float formatting of values.
Definition: Value.h:199
The class template for typed subclasses of Value.
Definition: Value.h:138
const T & getData() const
Return stored data object.
Definition: Value.h:169
void setData(const TypedValue< T > &value)
Set stored data object.
Definition: Value.h:163
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:151
void setData(const T &value)
Set stored data object.
Definition: Value.h:157
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:45
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:101
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.
Definition: Value.h:113
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:49
static void setFloatPrecision(int precision)
Set float precision for converting values to strings.
Definition: Value.h:107
static ValuePtr createValue(const T &data)
Create a new value from an object of any valid MaterialX type.
Definition: Value.h:62
static int getFloatPrecision()
Return the current float precision.
Definition: Value.h:119
static ValuePtr createValueFromStrings(const string &value, const string &type)
Create a new value instance from value and type strings.