vsg: include/vsg/io/Input.h Source File

vsg 1.1.9 VulkanSceneGraph library |

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