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