vsg: include/vsg/core/Value.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
Value.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Data.h>
16#include <vsg/core/type_name.h>
17
18#include <vsg/maths/box.h>
19#include <vsg/maths/mat2.h>
20#include <vsg/maths/mat3.h>
21#include <vsg/maths/mat4.h>
22#include <vsg/maths/quat.h>
23#include <vsg/maths/sphere.h>
24#include <vsg/maths/vec2.h>
25#include <vsg/maths/vec3.h>
26#include <vsg/maths/vec4.h>
27
28#include <vsg/io/Input.h>
29#include <vsg/io/Output.h>
30
31#define VSG_value(N, T) \
32 using N = Value<T>; \
33 template<> \
34 constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
35
36namespace vsg
37{
38 template<typename T>
39 class VSG_TEMPLATE_DECLSPEC Value : public Data
40 {
41 public:
42 using value_type = T;
43
44 Value() :
45 _value{} { dirty(); }
46 Value(const Value& rhs, const CopyOp& copyop = {}) :
47 Data(rhs, copyop), _value(rhs._value) { dirty(); }
48 explicit Value(const value_type& in_value) :
49 _value(in_value) { dirty(); }
50
51 template<typename... Args>
52 explicit Value(Args&&... args) :
53 _value(std::forward<Args>(args)...) { dirty(); }
54
55 template<typename... Args>
56 static ref_ptr<Value> create(Args&&... args)
57 {
58 return ref_ptr<Value>(new Value(std::forward<Args>(args)...));
59 }
60
61 template<typename... Args>
62 static ref_ptr<Value> create_if(bool flag, Args&&... args)
63 {
64 if (flag)
65 return ref_ptr<Value>(new Value(std::forward<Args>(args)...));
66 else
67 return {};
68 }
69
70 ref_ptr<Object> clone(const CopyOp& copyop = {}) const override
71 {
72 return ref_ptr<Value>(new Value(*this, copyop));
73 }
74
75 size_t sizeofObject() const noexcept override { return sizeof(Value); }
76 const char* className() const noexcept override { return type_name<Value>(); }
77 const std::type_info& type_info() const noexcept override { return typeid(*this); }
78 bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Value) == type || Data::is_compatible(type); }
79
80 // implementation provided by Visitor.h
81 void accept(Visitor& visitor) override;
82 void accept(ConstVisitor& visitor) const override;
83
84 void read(Input& input) override
85 {
86 Data::read(input);
87 if (input.version_greater_equal(0, 6, 1))
88 input.read("value", _value);
89 else
90 input.read("Value", _value);
91 dirty();
92 }
93
94 void write(Output& output) const override
95 {
96 Data::write(output);
97 if (output.version_greater_equal(0, 6, 1))
98 output.write("value", _value);
99 else
100 output.write("Value", _value);
101 }
102
103 size_t valueSize() const override
104 {
105 if constexpr (std::is_same_v<T, std::string>)
106 return _value.size();
107 else
108 return sizeof(value_type);
109 }
110 size_t valueCount() const override { return 1; }
111
112 bool dataAvailable() const override { return true; }
113 size_t dataSize() const override { return valueSize(); }
114
115 void* dataPointer() override
116 {
117 if constexpr (std::is_same_v<T, std::string>)
118 return _value.data();
119 else
120 return &_value;
121 }
122
123 const void* dataPointer() const override
124 {
125 if constexpr (std::is_same_v<T, std::string>)
126 return _value.data();
127 else
128 return &_value;
129 }
130
131 void* dataPointer(size_t) override { return dataPointer(); }
132 const void* dataPointer(size_t) const override { return dataPointer(); }
133
134 void* dataRelease() override { return nullptr; }
135
136 uint32_t dimensions() const override { return 0; }
137
138 uint32_t width() const override { return 1; }
139 uint32_t height() const override { return 1; }
140 uint32_t depth() const override { return 1; }
141
142 Value& operator=(const Value& rhs)
143 {
144 _value = rhs._value;
145
146 _copy(rhs);
147
148 return *this;
149 }
150 Value& operator=(const value_type& rhs)
151 {
152 _value = rhs;
153 return *this;
154 }
155
156 operator value_type&() { return _value; }
157 operator const value_type&() const { return _value; }
158
159 value_type& value() { return _value; }
160 const value_type& value() const { return _value; }
161
162 void set(const value_type& value) { _value = value; }
163
164 protected:
165 virtual ~Value() {}
166
167 private:
168 value_type _value;
169 };
170
171 template<typename T>
172 void Object::setValue(const std::string& key, const T& value)
173 {
174 using ValueT = Value<T>;
175 setObject(key, ValueT::create(value));
176 }
177
178 template<typename T>
179 bool Object::getValue(const std::string& key, T& value) const
180 {
181 using ValueT = Value<T>;
182 const Object* object = getObject(key);
183 if (object && (typeid(*object) == typeid(ValueT)))
184 {
185 const ValueT* vo = static_cast<const ValueT*>(getObject(key));
186 value = *vo;
187 return true;
188 }
189 else
190 {
191 return false;
192 }
193 }
194
198 template<typename T, typename... Args>
199 T value(T defaultValue, const std::string& match, Args&&... args)
200 {
201 T v{defaultValue};
202 ((args && args->getValue(match, v)) || ...);
203 return v;
204 }
205
206 VSG_value(stringValue, std::string);
207 VSG_value(wstringValue, std::wstring);
208 VSG_value(pathValue, vsg::Path);
209
210 VSG_value(boolValue, bool);
211 VSG_value(intValue, int);
212 VSG_value(uintValue, unsigned int);
213 VSG_value(floatValue, float);
214 VSG_value(doubleValue, double);
215
216 VSG_value(vec2Value, vec2);
217 VSG_value(vec3Value, vec3);
218 VSG_value(vec4Value, vec4);
219
220 VSG_value(dvec2Value, dvec2);
221 VSG_value(dvec3Value, dvec3);
222 VSG_value(dvec4Value, dvec4);
223
224 VSG_value(bvec2Value, bvec2);
225 VSG_value(bvec3Value, bvec3);
226 VSG_value(bvec4Value, bvec4);
227
228 VSG_value(ubvec2Value, ubvec2);
229 VSG_value(ubvec3Value, ubvec3);
230 VSG_value(ubvec4Value, ubvec4);
231
232 VSG_value(svec2Value, svec2);
233 VSG_value(svec3Value, svec3);
234 VSG_value(svec4Value, svec4);
235
236 VSG_value(usvec2Value, usvec2);
237 VSG_value(usvec3Value, usvec3);
238 VSG_value(usvec4Value, usvec4);
239
240 VSG_value(ivec2Value, ivec2);
241 VSG_value(ivec3Value, ivec3);
242 VSG_value(ivec4Value, ivec4);
243
244 VSG_value(uivec2Value, uivec2);
245 VSG_value(uivec3Value, uivec3);
246 VSG_value(uivec4Value, uivec4);
247
248 VSG_value(mat2Value, mat2);
249 VSG_value(dmat2Value, dmat2);
250
251 VSG_value(mat3Value, mat3);
252 VSG_value(dmat3Value, dmat3);
253
254 VSG_value(mat4Value, mat4);
255 VSG_value(dmat4Value, dmat4);
256
257 VSG_value(quatValue, quat);
258 VSG_value(dquatValue, dquat);
259
260 VSG_value(sphereValue, sphere);
261 VSG_value(dsphereValue, dsphere);
262
263 VSG_value(boxValue, box);
264 VSG_value(dboxValue, dbox);
265
266} // namespace vsg
vsg::CopyOp
Definition Object.h:42
vsg::Data::dirty
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition Data.h:203
vsg::Object::setObject
void setObject(const std::string &key, ref_ptr< Object > object)
assign an Object associated with key
vsg::Object::getValue
bool getValue(const std::string &key, T &value) const
get specified value type, return false if value associated with key is not assigned or is not the cor...
Definition Value.h:179
vsg::Object::getObject
Object * getObject(const std::string &key)
get Object pointer associated with key, return nullptr if no object associated with key has been assi...
vsg::Object::setValue
void setValue(const std::string &key, const T &value)
Definition Value.h:172
vsg::Value
Definition Value.h:40
vsg::Value::clone
ref_ptr< Object > clone(const CopyOp &copyop={}) const override
Definition Value.h:70
vsg::Value::type_info
const std::type_info & type_info() const noexcept override
return the std::type_info of this Object
Definition Value.h:77
vsg::ref_ptr
Definition ref_ptr.h:22