vsg  1.1.0
VulkanSceneGraph library
Output.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 #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 
35 namespace 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 std::string* value) = 0;
65  virtual void write(size_t num, const std::wstring* value) = 0;
66  virtual void write(size_t num, const Path* value) = 0;
67 
69  virtual void write(const Object* object) = 0;
70 
72  void write(size_t num, const char* value) { write(num, reinterpret_cast<const int8_t*>(value)); }
73  void write(size_t num, const bool* value) { write(num, reinterpret_cast<const int8_t*>(value)); }
74 
75  // vec/mat versions of write methods
76  void write(size_t num, const vec2* value) { write(num * value->size(), value->data()); }
77  void write(size_t num, const vec3* value) { write(num * value->size(), value->data()); }
78  void write(size_t num, const vec4* value) { write(num * value->size(), value->data()); }
79  void write(size_t num, const dvec2* value) { write(num * value->size(), value->data()); }
80  void write(size_t num, const dvec3* value) { write(num * value->size(), value->data()); }
81  void write(size_t num, const dvec4* value) { write(num * value->size(), value->data()); }
82  void write(size_t num, const bvec2* value) { write(num * value->size(), value->data()); }
83  void write(size_t num, const bvec3* value) { write(num * value->size(), value->data()); }
84  void write(size_t num, const bvec4* value) { write(num * value->size(), value->data()); }
85  void write(size_t num, const ubvec2* value) { write(num * value->size(), value->data()); }
86  void write(size_t num, const ubvec3* value) { write(num * value->size(), value->data()); }
87  void write(size_t num, const ubvec4* value) { write(num * value->size(), value->data()); }
88  void write(size_t num, const svec2* value) { write(num * value->size(), value->data()); }
89  void write(size_t num, const svec3* value) { write(num * value->size(), value->data()); }
90  void write(size_t num, const svec4* value) { write(num * value->size(), value->data()); }
91  void write(size_t num, const usvec2* value) { write(num * value->size(), value->data()); }
92  void write(size_t num, const usvec3* value) { write(num * value->size(), value->data()); }
93  void write(size_t num, const usvec4* value) { write(num * value->size(), value->data()); }
94  void write(size_t num, const ivec2* value) { write(num * value->size(), value->data()); }
95  void write(size_t num, const ivec3* value) { write(num * value->size(), value->data()); }
96  void write(size_t num, const ivec4* value) { write(num * value->size(), value->data()); }
97  void write(size_t num, const uivec2* value) { write(num * value->size(), value->data()); }
98  void write(size_t num, const uivec3* value) { write(num * value->size(), value->data()); }
99  void write(size_t num, const uivec4* value) { write(num * value->size(), value->data()); }
100  void write(size_t num, const quat* value) { write(num * value->size(), value->data()); }
101  void write(size_t num, const dquat* value) { write(num * value->size(), value->data()); }
102  void write(size_t num, const mat3* value) { write(num * value->size(), value->data()); }
103  void write(size_t num, const dmat3* value) { write(num * value->size(), value->data()); }
104  void write(size_t num, const mat4* value) { write(num * value->size(), value->data()); }
105  void write(size_t num, const dmat4* value) { write(num * value->size(), value->data()); }
106  void write(size_t num, const sphere* value) { write(num * value->size(), value->data()); }
107  void write(size_t num, const dsphere* value) { write(num * value->size(), value->data()); }
108  void write(size_t num, const box* value) { write(num * value->size(), value->data()); }
109  void write(size_t num, const dbox* value) { write(num * value->size(), value->data()); }
110  void write(size_t num, const plane* value) { write(num * value->size(), value->data()); }
111  void write(size_t num, const dplane* value) { write(num * value->size(), value->data()); }
112 
113  template<typename T>
114  void write(size_t num, const T* value)
115  {
116  if constexpr (has_read_write<T>())
117  {
118  for (size_t i = 0; i < num; ++i) value[i].write(*this);
119  }
120  else
121  {
122  write(num * sizeof(T), reinterpret_cast<const uint8_t*>(value));
123  }
124  }
125 
126  template<typename T>
127  void write(const char* propertyName, const ref_ptr<T>& object)
128  {
129  writePropertyName(propertyName);
130  write(object);
131  }
132 
133  template<typename T>
134  void writeObjects(const char* propertyName, const T& values)
135  {
136  uint32_t numElements = static_cast<uint32_t>(values.size());
137  write(propertyName, 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  write(element_name, values[i]);
145  }
146  }
147 
148  template<typename T>
149  void writeValues(const char* propertyName, const std::vector<T>& values)
150  {
151  uint32_t numElements = static_cast<uint32_t>(values.size());
152  write(propertyName, numElements);
153 
154  for (uint32_t i = 0; i < numElements; ++i)
155  {
156  write("element", values[i]);
157  }
158  }
159 
160  template<typename T>
161  void writeValues(const char* propertyName, const std::set<T>& values)
162  {
163  uint32_t numElements = static_cast<uint32_t>(values.size());
164  write(propertyName, numElements);
165 
166  for (auto& v : values)
167  {
168  write("element", v);
169  }
170  }
171 
173  template<typename... Args>
174  void write(const char* propertyName, Args&... args)
175  {
176  writePropertyName(propertyName);
177 
178  // use fold expression to expand arguments and map to appropriate write method
179  (write(1, &(args)), ...);
180 
181  writeEndOfLine();
182  }
183 
184  void writeObject(const char* propertyName, const Object* object)
185  {
186  writePropertyName(propertyName);
187  write(object);
188  }
189 
191  template<typename W, typename T>
192  void writeValue(const char* propertyName, T value)
193  {
194  W v{static_cast<W>(value)};
195  write(propertyName, v);
196  }
197 
198  using ObjectID = uint32_t;
199  using ObjectIDMap = std::unordered_map<const vsg::Object*, ObjectID>;
200 
201  ObjectID objectID = 1;
202  ObjectIDMap objectIDMap;
203  ref_ptr<const Options> options;
204 
205  VsgVersion version;
206 
207  virtual bool version_less(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
208  virtual bool version_greater_equal(uint32_t major, uint32_t minor, uint32_t patch, uint32_t soversion = 0) const;
209 
210  protected:
211  virtual ~Output();
212  };
213 
214 } // namespace vsg
Definition: Object.h:42
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:192
void write(size_t num, const char *value)
map char to int8_t
Definition: Output.h:72
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:174
virtual void writePropertyName(const char *propertyName)=0
write property name if appropriate for format
Definition: Path.h:32
Definition: ref_ptr.h:22
Definition: Version.h:40