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

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