vsg  1.1.0
VulkanSceneGraph library
Value.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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/mat3.h>
20 #include <vsg/maths/mat4.h>
21 #include <vsg/maths/quat.h>
22 #include <vsg/maths/sphere.h>
23 #include <vsg/maths/vec2.h>
24 #include <vsg/maths/vec3.h>
25 #include <vsg/maths/vec4.h>
26 
27 #include <vsg/io/Input.h>
28 #include <vsg/io/Output.h>
29 
30 #define VSG_value(N, T) \
31  using N = Value<T>; \
32  template<> \
33  constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
34 
35 namespace vsg
36 {
37  template<typename T>
38  class Value : public Data
39  {
40  public:
41  using value_type = T;
42 
43  Value() :
44  _value{} { dirty(); }
45  Value(const Value& rhs) :
46  Data(rhs), _value(rhs._value) { dirty(); }
47  explicit Value(const value_type& in_value) :
48  _value(in_value) { dirty(); }
49 
50  template<typename... Args>
51  explicit Value(Args&&... args) :
52  _value(args...) { dirty(); }
53 
54  template<typename... Args>
55  static ref_ptr<Value> create(Args&&... args)
56  {
57  return ref_ptr<Value>(new Value(args...));
58  }
59 
60  ref_ptr<Data> clone() const override
61  {
62  return ref_ptr<Value>(new Value(*this));
63  }
64 
65  size_t sizeofObject() const noexcept override { return sizeof(Value); }
66  const char* className() const noexcept override { return type_name<Value>(); }
67  const std::type_info& type_info() const noexcept override { return typeid(*this); }
68  bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Value) == type || Data::is_compatible(type); }
69 
70  // implementation provided by Visitor.h
71  void accept(Visitor& visitor) override;
72  void accept(ConstVisitor& visitor) const override;
73 
74  void read(Input& input) override
75  {
76  Data::read(input);
77  if (input.version_greater_equal(0, 6, 1))
78  input.read("value", _value);
79  else
80  input.read("Value", _value);
81  dirty();
82  }
83 
84  void write(Output& output) const override
85  {
86  Data::write(output);
87  if (output.version_greater_equal(0, 6, 1))
88  output.write("value", _value);
89  else
90  output.write("Value", _value);
91  }
92 
93  size_t valueSize() const override
94  {
95  if constexpr (std::is_same_v<T, std::string>)
96  return _value.size();
97  else
98  return sizeof(value_type);
99  }
100  size_t valueCount() const override { return 1; }
101 
102  bool dataAvailable() const override { return true; }
103  size_t dataSize() const override { return valueSize(); }
104 
105  void* dataPointer() override
106  {
107  if constexpr (std::is_same_v<T, std::string>)
108  return _value.data();
109  else
110  return &_value;
111  }
112 
113  const void* dataPointer() const override
114  {
115  if constexpr (std::is_same_v<T, std::string>)
116  return _value.data();
117  else
118  return &_value;
119  }
120 
121  void* dataPointer(size_t) override { return dataPointer(); }
122  const void* dataPointer(size_t) const override { return dataPointer(); }
123 
124  void* dataRelease() override { return nullptr; }
125 
126  uint32_t dimensions() const override { return 0; }
127 
128  uint32_t width() const override { return 1; }
129  uint32_t height() const override { return 1; }
130  uint32_t depth() const override { return 1; }
131 
132  Value& operator=(const Value& rhs)
133  {
134  _value = rhs._value;
135  return *this;
136  }
137  Value& operator=(const value_type& rhs)
138  {
139  _value = rhs;
140  return *this;
141  }
142 
143  operator value_type&() { return _value; }
144  operator const value_type&() const { return _value; }
145 
146  value_type& value() { return _value; }
147  const value_type& value() const { return _value; }
148 
149  void set(const value_type& value) { _value = value; }
150 
151  protected:
152  virtual ~Value() {}
153 
154  private:
155  value_type _value;
156  };
157 
158  template<typename T>
159  void Object::setValue(const std::string& key, const T& value)
160  {
161  using ValueT = Value<T>;
162  setObject(key, ValueT::create(value));
163  }
164 
165  template<typename T>
166  bool Object::getValue(const std::string& key, T& value) const
167  {
168  using ValueT = Value<T>;
169  const Object* object = getObject(key);
170  if (object && (typeid(*object) == typeid(ValueT)))
171  {
172  const ValueT* vo = static_cast<const ValueT*>(getObject(key));
173  value = *vo;
174  return true;
175  }
176  else
177  {
178  return false;
179  }
180  }
181 
185  template<typename T, typename... Args>
186  T value(T defaultValue, const std::string& match, Args&&... args)
187  {
188  T v{defaultValue};
189  ((args && args->getValue(match, v)) || ...);
190  return v;
191  }
192 
193  VSG_value(stringValue, std::string);
194  VSG_value(wstringValue, std::wstring);
195 
196  VSG_value(boolValue, bool);
197  VSG_value(intValue, int);
198  VSG_value(uintValue, unsigned int);
199  VSG_value(floatValue, float);
200  VSG_value(doubleValue, double);
201 
202  VSG_value(vec2Value, vec2);
203  VSG_value(vec3Value, vec3);
204  VSG_value(vec4Value, vec4);
205 
206  VSG_value(dvec2Value, dvec2);
207  VSG_value(dvec3Value, dvec3);
208  VSG_value(dvec4Value, dvec4);
209 
210  VSG_value(bvec2Value, bvec2);
211  VSG_value(bvec3Value, bvec3);
212  VSG_value(bvec4Value, bvec4);
213 
214  VSG_value(ubvec2Value, ubvec2);
215  VSG_value(ubvec3Value, ubvec3);
216  VSG_value(ubvec4Value, ubvec4);
217 
218  VSG_value(svec2Value, svec2);
219  VSG_value(svec3Value, svec3);
220  VSG_value(svec4Value, svec4);
221 
222  VSG_value(usvec2Value, usvec2);
223  VSG_value(usvec3Value, usvec3);
224  VSG_value(usvec4Value, usvec4);
225 
226  VSG_value(ivec2Value, ivec2);
227  VSG_value(ivec3Value, ivec3);
228  VSG_value(ivec4Value, ivec4);
229 
230  VSG_value(uivec2Value, uivec2);
231  VSG_value(uivec3Value, uivec3);
232  VSG_value(uivec4Value, uivec4);
233 
234  VSG_value(mat3Value, mat3);
235  VSG_value(dmat3Value, dmat3);
236 
237  VSG_value(mat4Value, mat4);
238  VSG_value(dmat4Value, dmat4);
239 
240  VSG_value(quatValue, quat);
241  VSG_value(dquatValue, dquat);
242 
243  VSG_value(sphereValue, sphere);
244  VSG_value(dsphereValue, dsphere);
245 
246  VSG_value(boxValue, box);
247  VSG_value(dboxValue, dbox);
248 
249 } // namespace vsg
Definition: Data.h:110
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition: Data.h:196
Definition: Object.h:42
void setObject(const std::string &key, ref_ptr< Object > object)
assign an Object associated with key
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:166
void setValue(const std::string &key, const T &value)
Definition: Value.h:159
Object * getObject(const std::string &key)
get Object pointer associated with key, return nullptr if no object associated with key has been assi...
Definition: Value.h:39
const std::type_info & type_info() const noexcept override
return the std::type_info of this Object
Definition: Value.h:67
Definition: ref_ptr.h:22