vsg  1.0.4
VulkanSceneGraph library
Input.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/Object.h>
17 #include <vsg/core/Version.h>
18 
19 #include <vsg/maths/box.h>
20 #include <vsg/maths/mat4.h>
21 #include <vsg/maths/plane.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/FileSystem.h>
29 #include <vsg/io/ObjectFactory.h>
30 
31 #include <set>
32 #include <unordered_map>
33 
34 namespace vsg
35 {
36 
37  // forward declare
38  class Options;
39 
42  class VSG_DECLSPEC Input
43  {
44  public:
45  Input(ref_ptr<ObjectFactory> in_objectFactory, ref_ptr<const Options> in_options = {});
46 
47  Input& operator=(const Input& rhs) = delete;
48 
50  virtual bool matchPropertyName(const char* propertyName) = 0;
51 
52  // read value(s)
53  virtual void read(size_t num, int8_t* value) = 0;
54  virtual void read(size_t num, uint8_t* value) = 0;
55  virtual void read(size_t num, int16_t* value) = 0;
56  virtual void read(size_t num, uint16_t* value) = 0;
57  virtual void read(size_t num, int32_t* value) = 0;
58  virtual void read(size_t num, uint32_t* value) = 0;
59  virtual void read(size_t num, int64_t* value) = 0;
60  virtual void read(size_t num, uint64_t* value) = 0;
61  virtual void read(size_t num, float* value) = 0;
62  virtual void read(size_t num, double* value) = 0;
63  virtual void read(size_t num, std::string* value) = 0;
64  virtual void read(size_t num, Path* value) = 0;
65 
66  // read object
67  virtual ref_ptr<Object> read() = 0;
68 
69  // map char to int8_t
70  void read(size_t num, char* value) { read(num, reinterpret_cast<int8_t*>(value)); }
71  void read(size_t num, bool* value) { read(num, reinterpret_cast<int8_t*>(value)); }
72 
73  // vec/mat versions of read methods
74  void read(size_t num, vec2* value) { read(num * value->size(), value->data()); }
75  void read(size_t num, vec3* value) { read(num * value->size(), value->data()); }
76  void read(size_t num, vec4* value) { read(num * value->size(), value->data()); }
77  void read(size_t num, dvec2* value) { read(num * value->size(), value->data()); }
78  void read(size_t num, dvec3* value) { read(num * value->size(), value->data()); }
79  void read(size_t num, dvec4* value) { read(num * value->size(), value->data()); }
80  void read(size_t num, bvec2* value) { read(num * value->size(), value->data()); }
81  void read(size_t num, bvec3* value) { read(num * value->size(), value->data()); }
82  void read(size_t num, bvec4* value) { read(num * value->size(), value->data()); }
83  void read(size_t num, ubvec2* value) { read(num * value->size(), value->data()); }
84  void read(size_t num, ubvec3* value) { read(num * value->size(), value->data()); }
85  void read(size_t num, ubvec4* value) { read(num * value->size(), value->data()); }
86  void read(size_t num, svec2* value) { read(num * value->size(), value->data()); }
87  void read(size_t num, svec3* value) { read(num * value->size(), value->data()); }
88  void read(size_t num, svec4* value) { read(num * value->size(), value->data()); }
89  void read(size_t num, usvec2* value) { read(num * value->size(), value->data()); }
90  void read(size_t num, usvec3* value) { read(num * value->size(), value->data()); }
91  void read(size_t num, usvec4* value) { read(num * value->size(), value->data()); }
92  void read(size_t num, ivec2* value) { read(num * value->size(), value->data()); }
93  void read(size_t num, ivec3* value) { read(num * value->size(), value->data()); }
94  void read(size_t num, ivec4* value) { read(num * value->size(), value->data()); }
95  void read(size_t num, uivec2* value) { read(num * value->size(), value->data()); }
96  void read(size_t num, uivec3* value) { read(num * value->size(), value->data()); }
97  void read(size_t num, uivec4* value) { read(num * value->size(), value->data()); }
98  void read(size_t num, quat* value) { read(num * value->size(), value->data()); }
99  void read(size_t num, dquat* value) { read(num * value->size(), value->data()); }
100  void read(size_t num, mat4* value) { read(num * value->size(), value->data()); }
101  void read(size_t num, dmat4* value) { read(num * value->size(), value->data()); }
102  void read(size_t num, sphere* value) { read(num * value->size(), value->data()); }
103  void read(size_t num, dsphere* value) { read(num * value->size(), value->data()); }
104  void read(size_t num, box* value) { read(num * value->size(), value->data()); }
105  void read(size_t num, dbox* value) { read(num * value->size(), value->data()); }
106  void read(size_t num, plane* value) { read(num * value->size(), value->data()); }
107  void read(size_t num, dplane* value) { read(num * value->size(), value->data()); }
108 
110  template<typename T>
111  void read(size_t num, T* value)
112  {
113  if constexpr (has_read_write<T>())
114  {
115  for (size_t i = 0; i < num; ++i) value[i].read(*this);
116  }
117  else
118  {
119  read(num * sizeof(T), reinterpret_cast<uint8_t*>(value));
120  }
121  }
122 
123  template<typename T>
124  void read(const char* propertyName, ref_ptr<T>& arg)
125  {
126  if (!matchPropertyName(propertyName)) return;
127  arg = read().cast<T>();
128  }
129 
130  template<typename T>
131  void readObjects(const char* propertyName, T& values)
132  {
133  if (!matchPropertyName(propertyName)) return;
134 
135  uint32_t numElements = 0;
136  read(1, &numElements);
137  values.resize(numElements);
138 
139  using element_type = typename T::value_type::element_type;
140  const char* element_name = type_name<element_type>();
141 
142  for (uint32_t i = 0; i < numElements; ++i)
143  {
144  read(element_name, values[i]);
145  }
146  }
147 
148  template<typename T>
149  void readValues(const char* propertyName, std::vector<T>& values)
150  {
151  if (!matchPropertyName(propertyName)) return;
152 
153  uint32_t numElements = 0;
154  read(1, &numElements);
155  values.resize(numElements);
156 
157  for (uint32_t i = 0; i < numElements; ++i)
158  {
159  read("element", values[i]);
160  }
161  }
162 
163  template<typename T>
164  void readValues(const char* propertyName, std::set<T>& values)
165  {
166  if (!matchPropertyName(propertyName)) return;
167 
168  uint32_t numElements = 0;
169  read(1, &numElements);
170 
171  for (uint32_t i = 0; i < numElements; ++i)
172  {
173  T v;
174  read("element", v);
175  values.insert(v);
176  }
177  }
178 
180  template<typename... Args>
181  void read(const char* propertyName, Args&... args)
182  {
183  if (!matchPropertyName(propertyName)) return;
184 
185  // use fold expression to expand arguments and map to appropriate read method
186  (read(1, &(args)), ...);
187  }
188 
190  ref_ptr<Object> readObject(const char* propertyName)
191  {
192  if (!matchPropertyName(propertyName)) return ref_ptr<Object>();
193 
194  return read();
195  }
196 
198  template<class T>
199  ref_ptr<T> readObject(const char* propertyName)
200  {
201  if (!matchPropertyName(propertyName)) return ref_ptr<T>();
202 
203  ref_ptr<Object> object = read();
204  return ref_ptr<T>(dynamic_cast<T*>(object.get()));
205  }
206 
208  template<class T>
209  void readObject(const char* propertyName, ref_ptr<T>& arg)
210  {
211  if (!matchPropertyName(propertyName)) return;
212 
213  arg = read().cast<T>();
214  }
215 
217  template<typename T>
218  T readValue(const char* propertyName)
219  {
220  T v{};
221  read(propertyName, v);
222  return v;
223  }
224 
226  template<typename W, typename T>
227  void readValue(const char* propertyName, T& value)
228  {
229  W read_value{};
230  read(propertyName, read_value);
231  value = static_cast<T>(read_value);
232  }
233 
234  using ObjectID = uint32_t;
235  using ObjectIDMap = std::map<ObjectID, ref_ptr<Object>>;
236 
237  ObjectIDMap objectIDMap;
238  ref_ptr<ObjectFactory> objectFactory;
239  ref_ptr<const Options> options;
240  Path filename;
241 
242  VsgVersion version;
243 
244  virtual bool version_less(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
245  virtual bool version_greater_equal(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
246 
247  protected:
248  virtual ~Input();
249  };
250 
251  template<>
252  inline void Input::readObject(const char* propertyName, ref_ptr<Object>& arg)
253  {
254  if (!matchPropertyName(propertyName)) return;
255 
256  arg = read();
257  }
258 
259 } // namespace vsg
Definition: Input.h:43
void readObject(const char *propertyName, ref_ptr< T > &arg)
read object of a particular type
Definition: Input.h:209
void readValue(const char *propertyName, T &value)
read a value as a type, then cast it another type
Definition: Input.h:227
T readValue(const char *propertyName)
read a value of particular type
Definition: Input.h:218
ref_ptr< T > readObject(const char *propertyName)
read object of a particular type
Definition: Input.h:199
virtual bool matchPropertyName(const char *propertyName)=0
return true if property name matches the next token in the stream, or if property names are not requi...
void read(const char *propertyName, Args &... args)
match property name and read value(s)
Definition: Input.h:181
ref_ptr< Object > readObject(const char *propertyName)
read object of a particular type
Definition: Input.h:190
void read(size_t num, T *value)
treat non standard type as raw data,
Definition: Input.h:111
Definition: Path.h:32
Definition: ref_ptr.h:22
Definition: Version.h:37