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

vsg 1.1.10 VulkanSceneGraph library |

Loading...
Searching...
No Matches
stream.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/Exception.h>
16#include <vsg/core/ref_ptr.h>
17#include <vsg/core/type_name.h>
18#include <vsg/io/Path.h>
19#include <vsg/maths/box.h>
20#include <vsg/maths/mat3.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#include <vsg/utils/CoordinateSpace.h>
29
30#include <istream>
31#include <ostream>
32#include <sstream>
33
34namespace vsg
35{
36
38 struct indentation
39 {
40 int indent = 0;
41
42 indentation& operator+=(int delta)
43 {
44 indent += delta;
45 return *this;
46 }
47 indentation& operator-=(int delta)
48 {
49 indent -= delta;
50 return *this;
51 }
52 };
53
54 inline indentation operator+(const indentation& lhs, const int rhs) { return indentation{lhs.indent + rhs}; }
55 inline indentation operator-(const indentation& lhs, const int rhs) { return indentation{lhs.indent - rhs}; }
56
57 inline std::ostream& operator<<(std::ostream& output, const indentation& in)
58 {
59 for (int i = 0; i < in.indent; ++i) output.put(' ');
60 return output;
61 }
62
64 template<typename... Args>
65 std::string make_string(const Args&... args)
66 {
67 std::ostringstream stream;
68 (stream << ... << args);
69 return stream.str();
70 }
71
73 template<typename T>
74 std::ostream& operator<<(std::ostream& output, const vsg::t_vec2<T>& vec)
75 {
76 output << vec.x << " " << vec.y;
77 return output;
78 }
79
81 template<typename T>
82 std::istream& operator>>(std::istream& input, vsg::t_vec2<T>& vec)
83 {
84 input >> vec.x >> vec.y;
85 return input;
86 }
87
89 template<typename T>
90 std::ostream& operator<<(std::ostream& output, const vsg::t_vec3<T>& vec)
91 {
92 output << vec.x << " " << vec.y << " " << vec.z;
93 return output;
94 }
95
97 template<typename T>
98 std::istream& operator>>(std::istream& input, vsg::t_vec3<T>& vec)
99 {
100 input >> vec.x >> vec.y >> vec.z;
101 return input;
102 }
103
105 template<typename T>
106 std::ostream& operator<<(std::ostream& output, const vsg::t_vec4<T>& vec)
107 {
108 output << vec.x << " " << vec.y << " " << vec.z << " " << vec.w;
109 return output;
110 }
111
113 template<typename T>
114 std::istream& operator>>(std::istream& input, vsg::t_vec4<T>& vec)
115 {
116 input >> vec.x >> vec.y >> vec.z >> vec.w;
117 return input;
118 }
119
121 template<typename T>
122 std::ostream& operator<<(std::ostream& output, const vsg::t_quat<T>& q)
123 {
124 output << q.x << " " << q.y << " " << q.z << " " << q.w;
125 return output;
126 }
127
129 template<typename T>
130 std::istream& operator>>(std::istream& input, vsg::t_quat<T>& q)
131 {
132 input >> q.x >> q.y >> q.z >> q.w;
133 return input;
134 }
135
137 template<typename T>
138 std::ostream& operator<<(std::ostream& output, const vsg::t_plane<T>& vec)
139 {
140 output << vec.value[0] << " " << vec.value[1] << " " << vec.value[2] << " " << vec.value[3];
141 return output;
142 }
143
145 template<typename T>
146 std::istream& operator>>(std::istream& input, vsg::t_plane<T>& vec)
147 {
148 input >> vec.value[0] >> vec.value[1] >> vec.value[2] >> vec.value[3];
149 return input;
150 }
151
153 template<typename T>
154 std::ostream& operator<<(std::ostream& output, const vsg::t_mat3<T>& mat)
155 {
156 output << std::endl;
157 output << " " << mat(0, 0) << " " << mat(1, 0) << " " << mat(2, 0) << std::endl;
158 output << " " << mat(0, 1) << " " << mat(1, 1) << " " << mat(2, 1) << std::endl;
159 output << " " << mat(0, 2) << " " << mat(1, 2) << " " << mat(2, 2) << std::endl;
160 return output;
161 }
162
164 template<typename T>
165 std::istream& operator>>(std::istream& input, vsg::t_mat3<T>& mat)
166 {
167 input >> mat(0, 0) >> mat(1, 0) >> mat(2, 0);
168 input >> mat(0, 1) >> mat(1, 1) >> mat(2, 1);
169 input >> mat(0, 2) >> mat(1, 2) >> mat(2, 2);
170 return input;
171 }
172
174 template<typename T>
175 std::ostream& operator<<(std::ostream& output, const vsg::t_mat4<T>& mat)
176 {
177 output << std::endl;
178 output << " " << mat(0, 0) << " " << mat(1, 0) << " " << mat(2, 0) << " " << mat(3, 0) << std::endl;
179 output << " " << mat(0, 1) << " " << mat(1, 1) << " " << mat(2, 1) << " " << mat(3, 1) << std::endl;
180 output << " " << mat(0, 2) << " " << mat(1, 2) << " " << mat(2, 2) << " " << mat(3, 2) << std::endl;
181 output << " " << mat(0, 3) << " " << mat(1, 3) << " " << mat(2, 3) << " " << mat(3, 3) << std::endl;
182 return output;
183 }
184
186 template<typename T>
187 std::istream& operator>>(std::istream& input, vsg::t_mat4<T>& mat)
188 {
189 input >> mat(0, 0) >> mat(1, 0) >> mat(2, 0) >> mat(3, 0);
190 input >> mat(0, 1) >> mat(1, 1) >> mat(2, 1) >> mat(3, 1);
191 input >> mat(0, 2) >> mat(1, 2) >> mat(2, 2) >> mat(3, 2);
192 input >> mat(0, 3) >> mat(1, 3) >> mat(2, 3) >> mat(3, 3);
193 return input;
194 }
195
197 template<typename T>
198 std::ostream& operator<<(std::ostream& output, const vsg::t_sphere<T>& sp)
199 {
200 output << sp.vec;
201 return output;
202 }
203
205 template<typename T>
206 std::istream& operator>>(std::istream& input, vsg::t_sphere<T>& sp)
207 {
208 input >> sp.vec;
209 return input;
210 }
211
213 template<typename T>
214 std::ostream& operator<<(std::ostream& output, const vsg::t_box<T>& bx)
215 {
216 output << std::endl;
217 output << " " << bx.min << std::endl;
218 output << " " << bx.max << std::endl;
219 return output;
220 }
221
223 template<typename T>
224 std::istream& operator>>(std::istream& input, vsg::t_box<T>& bx)
225 {
226 input >> bx.min;
227 input >> bx.max;
228 return input;
229 }
230
232 template<typename T>
233 std::ostream& operator<<(std::ostream& output, const vsg::ref_ptr<T>& ptr)
234 {
235 if (ptr)
236 output << "ref_ptr<" << vsg::type_name<T>() << ">(" << ptr->className() << " " << ptr.get() << ")";
237 else
238 output << "ref_ptr<" << vsg::type_name<T>() << ">(nullptr)";
239 return output;
240 }
241
243 template<typename T, typename R>
244 std::ostream& operator<<(std::ostream& output, const std::pair<T, R>& wd)
245 {
246 output << wd.first << " " << wd.second;
247 return output;
248 }
249
251 template<typename T, typename R>
252 std::istream& operator>>(std::istream& input, std::pair<T, R>& wd)
253 {
254 input >> wd.first >> wd.second;
255 return input;
256 }
257
259 template<typename T>
260 std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
261 {
262 return stream << static_cast<typename std::underlying_type<T>::type>(e);
263 }
264
266 inline std::ostream& operator<<(std::ostream& output, const vsg::Path& path)
267 {
268 output << path.string();
269 return output;
270 }
271
273 inline std::istream& operator>>(std::istream& input, vsg::Path& path)
274 {
275 std::string str;
276 input >> str;
277 path = str;
278 return input;
279 }
280
282 inline std::ostream& operator<<(std::ostream& output, const vsg::Exception& e)
283 {
284 output << "Error code: " << e.result << " | " << e.message;
285 return output;
286 }
287
288 inline std::istream& operator>>(std::istream& input, CoordinateSpace& coordinateSpace)
289 {
290 std::string value;
291 input >> value;
292
293 if (value == "LINEAR")
294 coordinateSpace = CoordinateSpace::LINEAR;
295 else if (value == "sRGB")
296 coordinateSpace = CoordinateSpace::sRGB;
297 else
298 coordinateSpace = CoordinateSpace::NO_PREFERENCE;
299
300 return input;
301 }
302
303 inline std::ostream& operator<<(std::ostream& output, const CoordinateSpace& coordinateSpace)
304 {
305 if (coordinateSpace == CoordinateSpace::LINEAR)
306 output << "LINEAR";
307 else if (coordinateSpace == CoordinateSpace::sRGB)
308 output << "sRGB";
309 else
310 output << "NO_PREFERENCE";
311
312 return output;
313 }
314
315} // namespace vsg
vsg::indentation
helper class for inserting indentation into streams useful for formatting output.
Definition stream.h:39