forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.h
More file actions
196 lines (158 loc) · 8.51 KB
/
Input.h
File metadata and controls
196 lines (158 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
</editor-fold> */
#include <vsg/core/Data.h>
#include <vsg/core/Object.h>
#include <vsg/core/Version.h>
#include <vsg/maths/box.h>
#include <vsg/maths/mat4.h>
#include <vsg/maths/plane.h>
#include <vsg/maths/sphere.h>
#include <vsg/maths/vec2.h>
#include <vsg/maths/vec3.h>
#include <vsg/maths/vec4.h>
#include <vsg/io/FileSystem.h>
#include <vsg/io/ObjectFactory.h>
#include <unordered_map>
namespace vsg
{
// forward declare
class Options;
class VSG_DECLSPEC Input
{
public:
Input(ref_ptr<ObjectFactory> in_objectFactory, ref_ptr<const Options> in_options = {});
Options& operator=(const Options& rhs) = delete;
/// return true if property name matches the next token in the stream, or if property names are not required for format
virtual bool matchPropertyName(const char* propertyName) = 0;
// read value(s)
virtual void read(size_t num, int8_t* value) = 0;
virtual void read(size_t num, uint8_t* value) = 0;
virtual void read(size_t num, int16_t* value) = 0;
virtual void read(size_t num, uint16_t* value) = 0;
virtual void read(size_t num, int32_t* value) = 0;
virtual void read(size_t num, uint32_t* value) = 0;
virtual void read(size_t num, int64_t* value) = 0;
virtual void read(size_t num, uint64_t* value) = 0;
virtual void read(size_t num, float* value) = 0;
virtual void read(size_t num, double* value) = 0;
virtual void read(size_t num, std::string* value) = 0;
// read object
virtual ref_ptr<Object> read() = 0;
// map char to int8_t
void read(size_t num, char* value) { read(num, reinterpret_cast<int8_t*>(value)); }
void read(size_t num, bool* value) { read(num, reinterpret_cast<int8_t*>(value)); }
// vec/mat versions of read methods
void read(size_t num, vec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, vec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, vec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, dvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, dvec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, dvec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, bvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, bvec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, bvec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, ubvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, ubvec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, ubvec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, svec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, svec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, svec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, usvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, usvec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, usvec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, ivec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, ivec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, ivec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, uivec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, uivec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, uivec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, mat4* value) { read(num * value->size(), value->data()); }
void read(size_t num, dmat4* value) { read(num * value->size(), value->data()); }
void read(size_t num, sphere* value) { read(num * value->size(), value->data()); }
void read(size_t num, dsphere* value) { read(num * value->size(), value->data()); }
void read(size_t num, box* value) { read(num * value->size(), value->data()); }
void read(size_t num, dbox* value) { read(num * value->size(), value->data()); }
void read(size_t num, plane* value) { read(num * value->size(), value->data()); }
void read(size_t num, dplane* value) { read(num * value->size(), value->data()); }
// treat non standard type as raw data,
template<typename T>
void read(size_t num, T* value)
{
if constexpr (has_read_write<T>())
{
for (size_t i = 0; i < num; ++i) value[i].read(*this);
}
else
{
read(num * sizeof(T), reinterpret_cast<uint8_t*>(value));
}
}
// match property name and read value(s)
template<typename... Args>
void read(const char* propertyName, Args&... args)
{
if (!matchPropertyName(propertyName)) return;
// use fold expression to expand arguments and map to appropriate read method
(read(1, &(args)), ...);
}
// read object of a particular type
ref_ptr<Object> readObject(const char* propertyName)
{
if (!matchPropertyName(propertyName)) return ref_ptr<Object>();
return read();
}
// read object of a particular type
template<class T>
ref_ptr<T> readObject(const char* propertyName)
{
if (!matchPropertyName(propertyName)) return ref_ptr<T>();
ref_ptr<Object> object = read();
return ref_ptr<T>(dynamic_cast<T*>(object.get()));
}
// read object of a particular type
template<class T>
void readObject(const char* propertyName, ref_ptr<T>& arg)
{
if (!matchPropertyName(propertyName)) return;
arg = read().cast<T>();
}
// read a value of particular type
template<typename T>
T readValue(const char* propertyName)
{
T value{};
read(propertyName, value);
return value;
}
/// read a value as a type, then cast it another type
template<typename W, typename T>
void readValue(const char* propertyName, T& value)
{
W read_value{};
read(propertyName, read_value);
value = static_cast<T>(read_value);
}
using ObjectID = uint32_t;
using ObjectIDMap = std::map<ObjectID, ref_ptr<Object>>;
ObjectIDMap objectIDMap;
ref_ptr<ObjectFactory> objectFactory;
ref_ptr<const Options> options;
Path filename;
VsgVersion version;
virtual bool version_less(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
virtual bool version_greater_equal(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
protected:
virtual ~Input();
};
template<>
inline void Input::readObject(const char* propertyName, ref_ptr<Object>& arg)
{
if (!matchPropertyName(propertyName)) return;
arg = read();
}
} // namespace vsg