1/*!
2 * \file ostream_utility.hpp
3 * \brief file ostream_utility.hpp
4 *
5 * Adapted from: ostream_utility.hpp of WRATH:
6 *
7 * Copyright 2013 by Nomovok Ltd.
8 * Contact: info@nomovok.com
9 * This Source Code Form is subject to the
10 * terms of the Mozilla Public License, v. 2.0.
11 * If a copy of the MPL was not distributed with
12 * this file, You can obtain one at
13 * http://mozilla.org/MPL/2.0/.
14 *
15 * \author Kevin Rogovin <kevin.rogovin@nomovok.com>
16 * \author Kevin Rogovin <kevin.rogovin@gmail.com>
17 *
18 */
19
20
21
22#ifndef FASTUIDRAW_DEMO_OSTREAM_UTILITY_HPP
23#define FASTUIDRAW_DEMO_OSTREAM_UTILITY_HPP
24
25
26#include <iostream>
27#include <iomanip>
28#include <string>
29#include <vector>
30#include <list>
31#include <map>
32#include <set>
33#include <utility>
34
35#include <fastuidraw/util/util.hpp>
36#include <fastuidraw/util/c_array.hpp>
37#include <fastuidraw/util/vecN.hpp>
38#include <fastuidraw/util/matrix.hpp>
39
40/*!\class format_tabbing
41 Simple class with overloaded operator<<
42 to print a number of indenting characters
43 to an std::ostream.
44*/
45class format_tabbing
46{
47public:
48 /*!\fn format_tabbing
49 construct a format_tabbing
50 \param ct number of times to print the indent character
51 \param c indent character, default is a tab (i.e. \\t)
52 */
53 explicit
54 format_tabbing(unsigned int ct, char c='\t'):m_count(ct), m_char(c) {}
55
56 /*!\var m_count
57 Number of times to print \ref m_char.
58 */
59 unsigned int m_count;
60
61 /*!\var m_char
62 Indent character to print.
63 */
64 char m_char;
65};
66
67/*!\class print_range_type
68 Simple type to print an STL range of elements
69 via overloading operator<<.
70*/
71template<typename iterator>
72class print_range_type
73{
74public:
75 /*!\fn print_range_type
76 Ctor.
77 \param begin iterator to 1st element to print
78 \param end iterator to one past the last element to print
79 \param spacingCharacter string to print between consecutive elements
80 */
81 print_range_type(iterator begin, iterator end,
82 const std::string &spacingCharacter=", "):
83 m_begin(begin), m_end(end), m_spacingCharacter(spacingCharacter) {}
84
85 /*!\var m_begin
86 Iterator to first element to print
87 */
88 iterator m_begin;
89
90 /*!\var m_end
91 Iterator to one element past the last element to print
92 */
93 iterator m_end;
94
95 /*!\var m_spacingCharacter
96 string to print between consecutive elements
97 */
98 std::string m_spacingCharacter;
99};
100
101/*!\fn print_range_type<iterator> print_range(iterator, iterator, const std::string&)
102 Returns a print_range_type to print a
103 range of elements to an std::ostream.
104 \tparam iterator type
105 \param begin iterator to 1st element to print
106 \param end iterator to one past the last element to print
107 \param str string to print between consecutive elements
108*/
109template<typename iterator>
110print_range_type<iterator>
111print_range(iterator begin, iterator end, const std::string &str=", ")
112{
113 return print_range_type<iterator>(begin, end, str);
114}
115
116/*!\class print_range_as_matrix_type
117 */
118template<typename iterator>
119class print_range_as_matrix_type
120{
121public:
122 /*!\fn print_range_as_matrix_type
123 Ctor.
124 \param begin iterator to 1st element to print
125 \param end iterator to one past the last element to print
126 \param leadingDimension how many elements to print between rows
127 \param spacingCharacter string to print between consecutive elements
128 */
129 print_range_as_matrix_type(iterator begin, iterator end,
130 unsigned int leadingDimension,
131 const std::string &beginOfLine="",
132 const std::string &endOfLine="\n",
133 const std::string &spacingCharacter=", "):
134 m_begin(begin),
135 m_end(end),
136 m_spacingCharacter(spacingCharacter),
137 m_leadingDimension(leadingDimension),
138 m_endOfLine(endOfLine),
139 m_beginOfLine(beginOfLine)
140 {}
141
142 /*!\var m_begin
143 Iterator to first element to print
144 */
145 iterator m_begin;
146
147 /*!\var m_end
148 Iterator to one element past the last element to print
149 */
150 iterator m_end;
151
152 /*!\var m_spacingCharacter
153 string to print between consecutive elements
154 */
155 std::string m_spacingCharacter;
156
157 /*!\var m_leadingDimension;
158 */
159 unsigned int m_leadingDimension;
160
161 /*!\var m_endOfLine
162 */
163 std::string m_endOfLine;
164
165 /*!\var m_beginOfLine
166 */
167 std::string m_beginOfLine;
168};
169
170template<typename iterator>
171print_range_as_matrix_type<iterator>
172print_range_as_matrix(iterator begin, iterator end,
173 unsigned int leadingDimension,
174 const std::string &beginOfLine="",
175 const std::string &endOfLine="\n",
176 const std::string &str=", ")
177{
178 return print_range_as_matrix_type<iterator>(begin, end, leadingDimension,
179 beginOfLine, endOfLine, str);
180}
181
182
183
184/*!\fn std::ostream& operator<<(std::ostream&, const fastuidraw::range_type<T>&)
185 conveniance operator<< to print the values of a range_type.
186 \param ostr std::ostream to which to print
187 \param obj range_type to print to str
188*/
189template<typename T>
190std::ostream&
191operator<<(std::ostream &ostr, const fastuidraw::range_type<T> &obj)
192{
193 ostr << "[" << obj.m_begin << "," << obj.m_end << ")";
194 return ostr;
195}
196
197/*!\fn std::ostream& operator<<(std::ostream&, const print_range_type<iterator>&)
198 overload of operator<< to print the contents of
199 an iterator range to an std::ostream.
200 \param ostr std::ostream to which to print
201 \param obj print_range_type object to print to ostr
202*/
203template<typename iterator>
204std::ostream&
205operator<<(std::ostream &ostr,
206 const print_range_type<iterator> &obj)
207{
208 iterator iter;
209
210 for(iter=obj.m_begin; iter!=obj.m_end; ++iter)
211 {
212 if (iter!=obj.m_begin)
213 {
214 ostr << obj.m_spacingCharacter;
215 }
216
217 ostr << (*iter);
218 }
219 return ostr;
220}
221
222/*!\fn std::ostream& operator<<(std::ostream&, const print_range_as_matrix_type<iterator>&)
223 overload of operator<< to print the contents of
224 an iterator range to an std::ostream.
225 \param ostr std::ostream to which to print
226 \param obj print_range_as_matrix_type object to print to ostr
227*/
228template<typename iterator>
229std::ostream&
230operator<<(std::ostream &ostr,
231 const print_range_as_matrix_type<iterator> &obj)
232{
233 iterator iter;
234 int idx;
235
236 for(iter=obj.m_begin, idx=0; iter!=obj.m_end; ++iter)
237 {
238 if (idx == 0)
239 {
240 ostr << obj.m_beginOfLine;
241 }
242 else
243 {
244 ostr << obj.m_spacingCharacter;
245 }
246
247 ostr << (*iter);
248
249 ++idx;
250 if (idx == obj.m_leadingDimension)
251 {
252 ostr << obj.m_endOfLine;
253 idx = 0;
254 }
255 }
256 return ostr;
257}
258
259/*!\fn std::ostream& operator<<(std::ostream&, const format_tabbing&)
260 overload of operator<< to print a format_tabbing object
261 to an std::ostream.
262 \param str std::ostream to which to print
263 \param tabber format_tabbing object to print to str
264*/
265inline
266std::ostream&
267operator<<(std::ostream &str, const format_tabbing &tabber)
268{
269 for(unsigned int i=0;i<tabber.m_count;++i)
270 {
271 str << tabber.m_char;
272 }
273 return str;
274}
275
276/*!\fn std::ostream& operator<<(std::ostream&, c_array<const T>)
277 Conveniance overload of operator<< to print the contents
278 pointed to by a c_array to an std::ostream
279 \param str std::ostream to which to print
280 \param obj c_array to print contents
281*/
282template<typename T>
283inline
284std::ostream&
285operator<<(std::ostream &str, fastuidraw::c_array<const T> obj)
286{
287 str << "( " << print_range(obj.begin(), obj.end(), ", ") << " )";
288 return str;
289}
290
291/*!\fn std::ostream& operator<<(std::ostream&, c_array<T>)
292 Conveniance overload of operator<< to print the contents
293 pointed to by a const_c_array to an std::ostream
294 \param str std::ostream to which to print
295 \param obj const_c_array to print contents
296*/
297template<typename T>
298inline
299std::ostream&
300operator<<(std::ostream &str, fastuidraw::c_array<T> obj)
301{
302 str << "( " << print_range(obj.begin(), obj.end(), ", ") << " )";
303 return str;
304}
305
306/*!\fn std::ostream& operator<<(std::ostream&, const vecN<T,N>&)
307 Overloaded operator<< to print to an std::stream
308 the contents of a vecN
309 \param str std::stream to which to print
310 \param obj contents to print to obj
311*/
312template<typename T, size_t N>
313inline
314std::ostream&
315operator<<(std::ostream &str, const fastuidraw::vecN<T,N> &obj)
316{
317 str << "( " << print_range(obj.begin(), obj.end(), ", ") << " )";
318 return str;
319}
320
321/*!\fn std::ostream& operator<<(std::ostream&, const std::pair<T,S>&)
322 conveniance operator<< to print an std::pair to an std::ostream.
323 \param str std::ostream to which to print
324 \param obj std::pair to print to str
325 */
326template<typename T, typename S>
327inline
328std::ostream&
329operator<<(std::ostream &str, const std::pair<T,S> &obj)
330{
331 str << "(" << obj.first << "," << obj.second << ")";
332 return str;
333}
334
335/*!\fn std::ostream& operator<<(std::ostream&, const std::set<T,_Compare,_Alloc>&)
336 conveniance operator<< to print the contents
337 of an std::set to an std::ostream.
338 \param str std::ostream to which to print
339 \param obj std::set to print to str
340 */
341template<typename T, typename _Compare, typename _Alloc>
342inline
343std::ostream&
344operator<<(std::ostream &str, const std::set<T,_Compare,_Alloc> &obj)
345{
346 str << "{ " << print_range(obj.begin(), obj.end(), ", ") << " }";
347 return str;
348}
349
350/*!\fn std::ostream& operator<<(std::ostream&, const std::list<T,_Alloc>&)
351 conveniance operator<< to print the contents
352 of an std::list to an std::ostream.
353 \param str std::ostream to which to print
354 \param obj std::list to print to str
355 */
356template<typename T, typename _Alloc>
357inline
358std::ostream&
359operator<<(std::ostream &str, const std::list<T,_Alloc> &obj)
360{
361 str << "( " << print_range(obj.begin(), obj.end(), ", ") << " )";
362 return str;
363}
364
365template<size_t N, size_t M, typename T>
366std::ostream&
367operator<<(std::ostream &str, const fastuidraw::matrixNxM<N, M, T> &matrix)
368{
369 for(unsigned int row = 0; row < N; ++row)
370 {
371 str << "|";
372 for(unsigned int col = 0; col < M; ++col)
373 {
374 str << std::setw(10) << matrix(row, col) << " ";
375 }
376 str << "|\n";
377 }
378 return str;
379}
380
381
382/*! @} */
383
384#endif
385