vsg  1.0.4
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  std::size_t sizeofObject() const noexcept override { return sizeof(Value); }
61  const char* className() const noexcept override { return type_name<Value>(); }
62  const std::type_info& type_info() const noexcept override { return typeid(*this); }
63  bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Value) == type || Data::is_compatible(type); }
64 
65  // implementation provided by Visitor.h
66  void accept(Visitor& visitor) override;
67  void accept(ConstVisitor& visitor) const override;
68 
69  void read(Input& input) override
70  {
71  Data::read(input);
72  if (input.version_greater_equal(0, 6, 1))
73  input.read("value", _value);
74  else
75  input.read("Value", _value);
76  dirty();
77  }
78 
79  void write(Output& output) const override
80  {
81  Data::write(output);
82  if (output.version_greater_equal(0, 6, 1))
83  output.write("value", _value);
84  else
85  output.write("Value", _value);
86  }
87 
88  std::size_t valueSize() const override { return sizeof(value_type); }
89  std::size_t valueCount() const override { return 1; }
90 
91  bool dataAvailable() const override { return true; }
92  std::size_t dataSize() const override { return sizeof(value_type); }
93 
94  void* dataPointer() override { return &_value; }
95  const void* dataPointer() const override { return &_value; }
96 
97  void* dataPointer(size_t) override { return &_value; }
98  const void* dataPointer(size_t) const override { return &_value; }
99 
100  void* dataRelease() override { return nullptr; }
101 
102  std::uint32_t dimensions() const override { return 0; }
103 
104  std::uint32_t width() const override { return 1; }
105  std::uint32_t height() const override { return 1; }
106  std::uint32_t depth() const override { return 1; }
107 
108  Value& operator=(const Value& rhs)
109  {
110  _value = rhs._value;
111  return *this;
112  }
113  Value& operator=(const value_type& rhs)
114  {
115  _value = rhs;
116  return *this;
117  }
118 
119  operator value_type&() { return _value; }
120  operator const value_type&() const { return _value; }
121 
122  value_type& value() { return _value; }
123  const value_type& value() const { return _value; }
124 
125  void set(const value_type& value) { _value = value; }
126 
127  protected:
128  virtual ~Value() {}
129 
130  private:
131  value_type _value;
132  };
133 
134  template<typename T>
135  void Object::setValue(const std::string& key, const T& value)
136  {
137  using ValueT = Value<T>;
138  setObject(key, ValueT::create(value));
139  }
140 
141  template<typename T>
142  bool Object::getValue(const std::string& key, T& value) const
143  {
144  using ValueT = Value<T>;
145  const Object* object = getObject(key);
146  if (object && (typeid(*object) == typeid(ValueT)))
147  {
148  const ValueT* vo = static_cast<const ValueT*>(getObject(key));
149  value = *vo;
150  return true;
151  }
152  else
153  {
154  return false;
155  }
156  }
157 
161  template<typename T, typename... Args>
162  T value(T defaultValue, const std::string& match, Args&&... args)
163  {
164  T v{defaultValue};
165  ((args && args->getValue(match, v)) || ...);
166  return v;
167  }
168 
169  VSG_value(stringValue, std::string);
170  VSG_value(boolValue, bool);
171  VSG_value(intValue, int);
172  VSG_value(uintValue, unsigned int);
173  VSG_value(floatValue, float);
174  VSG_value(doubleValue, double);
175 
176  VSG_value(vec2Value, vec2);
177  VSG_value(vec3Value, vec3);
178  VSG_value(vec4Value, vec4);
179 
180  VSG_value(dvec2Value, dvec2);
181  VSG_value(dvec3Value, dvec3);
182  VSG_value(dvec4Value, dvec4);
183 
184  VSG_value(bvec2Value, bvec2);
185  VSG_value(bvec3Value, bvec3);
186  VSG_value(bvec4Value, bvec4);
187 
188  VSG_value(ubvec2Value, ubvec2);
189  VSG_value(ubvec3Value, ubvec3);
190  VSG_value(ubvec4Value, ubvec4);
191 
192  VSG_value(svec2Value, svec2);
193  VSG_value(svec3Value, svec3);
194  VSG_value(svec4Value, svec4);
195 
196  VSG_value(usvec2Value, usvec2);
197  VSG_value(usvec3Value, usvec3);
198  VSG_value(usvec4Value, usvec4);
199 
200  VSG_value(ivec2Value, ivec2);
201  VSG_value(ivec3Value, ivec3);
202  VSG_value(ivec4Value, ivec4);
203 
204  VSG_value(uivec2Value, uivec2);
205  VSG_value(uivec3Value, uivec3);
206  VSG_value(uivec4Value, uivec4);
207 
208  VSG_value(mat3Value, mat3);
209  VSG_value(dmat3Value, dmat3);
210 
211  VSG_value(mat4Value, mat4);
212  VSG_value(dmat4Value, dmat4);
213 
214  VSG_value(quatValue, quat);
215  VSG_value(dquatValue, dquat);
216 
217  VSG_value(sphereValue, sphere);
218  VSG_value(dsphereValue, dsphere);
219 
220  VSG_value(boxValue, box);
221  VSG_value(dboxValue, dbox);
222 
223 } // namespace vsg
Definition: Data.h:104
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition: Data.h:206
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:142
void setValue(const std::string &key, const T &value)
Definition: Value.h:135
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:62
Definition: ref_ptr.h:22