vsg 1.1.9
VulkanSceneGraph library
 
Loading...
Searching...
No Matches
Output.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#include <vsg/core/type_name.h>
19
20#include <vsg/maths/box.h>
21#include <vsg/maths/mat3.h>
22#include <vsg/maths/mat4.h>
23#include <vsg/maths/plane.h>
24#include <vsg/maths/quat.h>
25#include <vsg/maths/sphere.h>
26#include <vsg/maths/vec2.h>
27#include <vsg/maths/vec3.h>
28#include <vsg/maths/vec4.h>
29
30#include <vsg/io/FileSystem.h>
31
32#include <set>
33#include <unordered_map>
34
35namespace vsg
36{
37
40 class VSG_DECLSPEC Output
41 {
42 public:
43 Output(ref_ptr<const Options> in_options = {});
44
45 Output& operator=(const Output& rhs) = delete;
46
48 virtual void writePropertyName(const char* propertyName) = 0;
49
51 virtual void writeEndOfLine() = 0;
52
54 virtual void write(size_t num, const int8_t* value) = 0;
55 virtual void write(size_t num, const uint8_t* value) = 0;
56 virtual void write(size_t num, const int16_t* value) = 0;
57 virtual void write(size_t num, const uint16_t* value) = 0;
58 virtual void write(size_t num, const int32_t* value) = 0;
59 virtual void write(size_t num, const uint32_t* value) = 0;
60 virtual void write(size_t num, const int64_t* value) = 0;
61 virtual void write(size_t num, const uint64_t* value) = 0;
62 virtual void write(size_t num, const float* value) = 0;
63 virtual void write(size_t num, const double* value) = 0;
64 virtual void write(size_t num, const long double* value) = 0;
65 virtual void write(size_t num, const std::string* value) = 0;
66 virtual void write(size_t num, const std::wstring* value) = 0;
67 virtual void write(size_t num, const Path* value) = 0;
68
70 virtual void write(const Object* object) = 0;
71
73 void write(size_t num, const char* value) { write(num, reinterpret_cast<const int8_t*>(value)); }
74 void write(size_t num, const bool* value) { write(num, reinterpret_cast<const int8_t*>(value)); }
75
76 // vec/mat versions of write methods
77 void write(size_t num, const vec2* value) { write(num * value->size(), value->data()); }
78 void write(size_t num, const vec3* value) { write(num * value->size(), value->data()); }
79 void write(size_t num, const vec4* value) { write(num * value->size(), value->data()); }
80 void write(size_t num, const dvec2* value) { write(num * value->size(), value->data()); }
81 void write(size_t num, const dvec3* value) { write(num * value->size(), value->data()); }
82 void write(size_t num, const dvec4* value) { write(num * value->size(), value->data()); }
83 void write(size_t num, const ldvec2* value) { write(num * value->size(), value->data()); }
84 void write(size_t num, const ldvec3* value) { write(num * value->size(), value->data()); }
85 void write(size_t num, const ldvec4* value) { write(num * value->size(), value->data()); }
86 void write(size_t num, const bvec2* value) { write(num * value->size(), value->data()); }
87 void write(size_t num, const bvec3* value) { write(num * value->size(), value->data()); }
88 void write(size_t num, const bvec4* value) { write(num * value->size(), value->data()); }
89 void write(size_t num, const ubvec2* value) { write(num * value->size(), value->data()); }
90 void write(size_t num, const ubvec3* value) { write(num * value->size(), value->data()); }
91 void write(size_t num, const ubvec4* value) { write(num * value->size(), value->data()); }
92 void write(size_t num, const svec2* value) { write(num * value->size(), value->data()); }
93 void write(size_t num, const svec3* value) { write(num * value->size(), value->data()); }
94 void write(size_t num, const svec4* value) { write(num * value->size(), value->data()); }
95 void write(size_t num, const usvec2* value) { write(num * value->size(), value->data()); }
96 void write(size_t num, const usvec3* value) { write(num * value->size(), value->data()); }
97 void write(size_t num, const usvec4* value) { write(num * value->size(), value->data()); }
98 void write(size_t num, const ivec2* value) { write(num * value->size(), value->data()); }
99 void write(size_t num, const ivec3* value) { write(num * value->size(), value->data()); }
100 void write(size_t num, const ivec4* value) { write(num * value->size(), value->data()); }
101 void write(size_t num, const uivec2* value) { write(num * value->size(), value->data()); }
102 void write(size_t num, const uivec3* value) { write(num * value->size(), value->data()); }
103 void write(size_t num, const uivec4* value) { write(num * value->size(), value->data()); }
104 void write(size_t num, const quat* value) { write(num * value->size(), value->data()); }
105 void write(size_t num, const dquat* value) { write(num * value->size(), value->data()); }
106 void write(size_t num, const mat3* value) { write(num * value->size(), value->data()); }
107 void write(size_t num, const dmat3* value) { write(num * value->size(), value->data()); }
108 void write(size_t num, const mat4* value) { write(num * value->size(), value->data()); }
109 void write(size_t num, const dmat4* value) { write(num * value->size(), value->data()); }
110 void write(size_t num, const sphere* value) { write(num * value->size(), value->data()); }
111 void write(size_t num, const dsphere* value) { write(num * value->size(), value->data()); }
112 void write(size_t num, const box* value) { write(num * value->size(), value->data()); }
113 void write(size_t num, const dbox* value) { write(num * value->size(), value->data()); }
114 void write(size_t num, const plane* value) { write(num * value->size(), value->data()); }
115 void write(size_t num, const dplane* value) { write(num * value->size(), value->data()); }
116
117 template<typename T>
118 void write(size_t num, const T* value)
119 {
120 if constexpr (has_read_write<T>())
121 {
122 for (size_t i = 0; i < num; ++i) value[i].write(*this);
123 }
124 else
125 {
126 write(num * sizeof(T), reinterpret_cast<const uint8_t*>(value));
127 }
128 }
129
130 template<typename T>
131 void write(const char* propertyName, const ref_ptr<T>& object)
132 {
133 writePropertyName(propertyName);
134 write(object);
135 }
136
137 template<typename T>
138 void writeObjects(const char* propertyName, const T& values)
139 {
140 uint32_t numElements = static_cast<uint32_t>(values.size());
141 write(propertyName, numElements);
142
143 using element_type = typename T::value_type::element_type;
144 const char* element_name = type_name<element_type>();
145
146 for (uint32_t i = 0; i < numElements; ++i)
147 {
148 write(element_name, values[i]);
149 }
150 }
151
152 template<typename T>
153 void writeValues(const char* propertyName, const std::vector<T>& values)
154 {
155 uint32_t numElements = static_cast<uint32_t>(values.size());
156 write(propertyName, numElements);
157
158 for (uint32_t i = 0; i < numElements; ++i)
159 {
160 write("element", values[i]);
161 }
162 }
163
164 template<typename T>
165 void writeValues(const char* propertyName, const std::set<T>& values)
166 {
167 uint32_t numElements = static_cast<uint32_t>(values.size());
168 write(propertyName, numElements);
169
170 for (const auto& v : values)
171 {
172 write("element", v);
173 }
174 }
175
177 template<typename... Args>
178 void write(const char* propertyName, Args&... args)
179 {
180 writePropertyName(propertyName);
181
182 // use fold expression to expand arguments and map to appropriate write method
183 (write(1, &(args)), ...);
184
186 }
187
188 void writeObject(const char* propertyName, const Object* object)
189 {
190 writePropertyName(propertyName);
191 write(object);
192 }
193
195 template<typename W, typename T>
196 void writeValue(const char* propertyName, T value)
197 {
198 W v{static_cast<W>(value)};
199 write(propertyName, v);
200 }
201
202 using ObjectID = uint32_t;
203 using ObjectIDMap = std::unordered_map<const vsg::Object*, ObjectID>;
204
205 ObjectID objectID = 1;
206 ObjectIDMap objectIDMap;
208
209 VsgVersion version;
210
211 virtual bool version_less(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
212 virtual bool version_greater_equal(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
213
214 protected:
215 virtual ~Output();
216 };
217
218} // namespace vsg
Definition Object.h:60
Definition Output.h:41
void writeValue(const char *propertyName, T value)
write a value casting it to specified type i.e. output.write<uint32_t>("Value", value);
Definition Output.h:196
void write(size_t num, const char *value)
map char to int8_t
Definition Output.h:73
virtual void write(const Object *object)=0
write object
virtual void write(size_t num, const int8_t *value)=0
write contiguous array of value(s)
virtual void writeEndOfLine()=0
write end of line character if required.
void write(const char *propertyName, Args &... args)
match propertyname and write value(s)
Definition Output.h:178
virtual void writePropertyName(const char *propertyName)=0
write property name if appropriate for format
Definition Path.h:34
Definition ref_ptr.h:22