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

vsg 1.1.9 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
29#include <istream>
30#include <ostream>
31#include <sstream>
32
33namespace vsg
34{
35
37 struct indentation
38 {
39 int indent = 0;
40
41 indentation& operator+=(int delta)
42 {
43 indent += delta;
44 return *this;
45 }
46 indentation& operator-=(int delta)
47 {
48 indent -= delta;
49 return *this;
50 }
51 };
52
53 inline indentation operator+(const indentation& lhs, const int rhs) { return indentation{lhs.indent + rhs}; }
54 inline indentation operator-(const indentation& lhs, const int rhs) { return indentation{lhs.indent - rhs}; }
55
56 inline std::ostream& operator<<(std::ostream& output, const indentation& in)
57 {
58 for (int i = 0; i < in.indent; ++i) output.put(' ');
59 return output;
60 }
61
63 template<typename... Args>
64 std::string make_string(const Args&... args)
65 {
66 std::ostringstream stream;
67 (stream << ... << args);
68 return stream.str();
69 }
70
72 template<typename T>
73 std::ostream& operator<<(std::ostream& output, const vsg::t_vec2<T>& vec)
74 {
75 output << vec.x << " " << vec.y;
76 return output;
77 }
78
80 template<typename T>
81 std::istream& operator>>(std::istream& input, vsg::t_vec2<T>& vec)
82 {
83 input >> vec.x >> vec.y;
84 return input;
85 }
86
88 template<typename T>
89 std::ostream& operator<<(std::ostream& output, const vsg::t_vec3<T>& vec)
90 {
91 output << vec.x << " " << vec.y << " " << vec.z;
92 return output;
93 }
94
96 template<typename T>
97 std::istream& operator>>(std::istream& input, vsg::t_vec3<T>& vec)
98 {
99 input >> vec.x >> vec.y >> vec.z;
100 return input;
101 }
102
104 template<typename T>
105 std::ostream& operator<<(std::ostream& output, const vsg::t_vec4<T>& vec)
106 {
107 output << vec.x << " " << vec.y << " " << vec.z << " " << vec.w;
108 return output;
109 }
110
112 template<typename T>
113 std::istream& operator>>(std::istream& input, vsg::t_vec4<T>& vec)
114 {
115 input >> vec.x >> vec.y >> vec.z >> vec.w;
116 return input;
117 }
118
120 template<typename T>
121 std::ostream& operator<<(std::ostream& output, const vsg::t_quat<T>& q)
122 {
123 output << q.x << " " << q.y << " " << q.z << " " << q.w;
124 return output;
125 }
126
128 template<typename T>
129 std::istream& operator>>(std::istream& input, vsg::t_quat<T>& q)
130 {
131 input >> q.x >> q.y >> q.z >> q.w;
132 return input;
133 }
134
136 template<typename T>
137 std::ostream& operator<<(std::ostream& output, const vsg::t_plane<T>& vec)
138 {
139 output << vec.value[0] << " " << vec.value[1] << " " << vec.value[2] << " " << vec.value[3];
140 return output;
141 }
142
144 template<typename T>
145 std::istream& operator>>(std::istream& input, vsg::t_plane<T>& vec)
146 {
147 input >> vec.value[0] >> vec.value[1] >> vec.value[2] >> vec.value[3];
148 return input;
149 }
150
152 template<typename T>
153 std::ostream& operator<<(std::ostream& output, const vsg::t_mat3<T>& mat)
154 {
155 output << std::endl;
156 output << " " << mat(0, 0) << " " << mat(1, 0) << " " << mat(2, 0) << std::endl;
157 output << " " << mat(0, 1) << " " << mat(1, 1) << " " << mat(2, 1) << std::endl;
158 output << " " << mat(0, 2) << " " << mat(1, 2) << " " << mat(2, 2) << std::endl;
159 return output;
160 }
161
163 template<typename T>
164 std::istream& operator>>(std::istream& input, vsg::t_mat3<T>& mat)
165 {
166 input >> mat(0, 0) >> mat(1, 0) >> mat(2, 0);
167 input >> mat(0, 1) >> mat(1, 1) >> mat(2, 1);
168 input >> mat(0, 2) >> mat(1, 2) >> mat(2, 2);
169 return input;
170 }
171
173 template<typename T>
174 std::ostream& operator<<(std::ostream& output, const vsg::t_mat4<T>& mat)
175 {
176 output << std::endl;
177 output << " " << mat(0, 0) << " " << mat(1, 0) << " " << mat(2, 0) << " " << mat(3, 0) << std::endl;
178 output << " " << mat(0, 1) << " " << mat(1, 1) << " " << mat(2, 1) << " " << mat(3, 1) << std::endl;
179 output << " " << mat(0, 2) << " " << mat(1, 2) << " " << mat(2, 2) << " " << mat(3, 2) << std::endl;
180 output << " " << mat(0, 3) << " " << mat(1, 3) << " " << mat(2, 3) << " " << mat(3, 3) << std::endl;
181 return output;
182 }
183
185 template<typename T>
186 std::istream& operator>>(std::istream& input, vsg::t_mat4<T>& mat)
187 {
188 input >> mat(0, 0) >> mat(1, 0) >> mat(2, 0) >> mat(3, 0);
189 input >> mat(0, 1) >> mat(1, 1) >> mat(2, 1) >> mat(3, 1);
190 input >> mat(0, 2) >> mat(1, 2) >> mat(2, 2) >> mat(3, 2);
191 input >> mat(0, 3) >> mat(1, 3) >> mat(2, 3) >> mat(3, 3);
192 return input;
193 }
194
196 template<typename T>
197 std::ostream& operator<<(std::ostream& output, const vsg::t_sphere<T>& sp)
198 {
199 output << sp.vec;
200 return output;
201 }
202
204 template<typename T>
205 std::istream& operator>>(std::istream& input, vsg::t_sphere<T>& sp)
206 {
207 input >> sp.vec;
208 return input;
209 }
210
212 template<typename T>
213 std::ostream& operator<<(std::ostream& output, const vsg::t_box<T>& bx)
214 {
215 output << std::endl;
216 output << " " << bx.min << std::endl;
217 output << " " << bx.max << std::endl;
218 return output;
219 }
220
222 template<typename T>
223 std::istream& operator>>(std::istream& input, vsg::t_box<T>& bx)
224 {
225 input >> bx.min;
226 input >> bx.max;
227 return input;
228 }
229
231 template<typename T>
232 std::ostream& operator<<(std::ostream& output, const vsg::ref_ptr<T>& ptr)
233 {
234 if (ptr)
235 output << "ref_ptr<" << vsg::type_name<T>() << ">(" << ptr->className() << " " << ptr.get() << ")";
236 else
237 output << "ref_ptr<" << vsg::type_name<T>() << ">(nullptr)";
238 return output;
239 }
240
242 template<typename T, typename R>
243 std::ostream& operator<<(std::ostream& output, const std::pair<T, R>& wd)
244 {
245 output << wd.first << " " << wd.second;
246 return output;
247 }
248
250 template<typename T, typename R>
251 std::istream& operator>>(std::istream& input, std::pair<T, R>& wd)
252 {
253 input >> wd.first >> wd.second;
254 return input;
255 }
256
258 template<typename T>
259 std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
260 {
261 return stream << static_cast<typename std::underlying_type<T>::type>(e);
262 }
263
265 inline std::ostream& operator<<(std::ostream& output, const vsg::Path& path)
266 {
267 output << path.string();
268 return output;
269 }
270
272 inline std::istream& operator>>(std::istream& input, vsg::Path& path)
273 {
274 std::string str;
275 input >> str;
276 path = str;
277 return input;
278 }
279
281 inline std::ostream& operator<<(std::ostream& output, const vsg::Exception& e)
282 {
283 output << "Error code: " << e.result << " | " << e.message;
284 return output;
285 }
286
287} // namespace vsg
vsg::indentation
helper class for inserting indentation into streams useful for formatting output.
Definition stream.h:38