Debian Codegraph @

3rdparty/xspublic/xstypes/xsstringstreaming.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSSTRINGSTREAMING_H
34 #define XSSTRINGSTREAMING_H
35
36 #ifdef __cplusplus
37 #include "xsstring.h"
38 #include "xsvector.h"
39 #include "xsmatrix.h"
40 #include "xsquaternion.h"
41
42 #ifndef XSENS_NO_STL
43 #include <ostream>
44 namespace std
45 {
46 template<typename _CharT, typename _Traits>
47 basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& o, XsVector const& xv)
48 {
49 o << "V<" << xv.size() << ">(";
50 for (XsSize i = 0; i < xv.size() - 1; i++)
51 o << xv[i] << ", ";
52 return (o << xv[xv.size() - 1] << ")");
53 }
54
55 template<typename _CharT, typename _Traits>
56 basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& o, XsMatrix const& xm)
57 {
58 o << "M<" << xm.rows() << "," << xm.cols() << ">(";
59 for (XsSize r = 0; r < xm.rows(); ++r)
60 {
61 for (XsSize c = 0; c < xm.cols() - 1; ++c)
62 o << xm[r][c] << ", ";
63 o << xm[r][xm.cols()-1];
64 if (r < xm.rows() - 1)
65 o << "\n";
66 }
67 o << ")";
68 return o;
69 }
70
71 template<typename _CharT, typename _Traits>
72 basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& o, XsQuaternion const& xq)
73 {
74 o << "Q(";
75 for (int i = 0; i < 3; i++)
76 o << xq[i] << ", ";
77 return (o << xq[3] << ")");
78 }
79 }
80 #endif
81
82 inline XsString& operator<<(XsString& o, XsSize const& v)
83 {
84 char buffer[32]; // 2e64 = 1.8e19 so this should be enough
85 sprintf(buffer, "%" PRINTF_SIZET_MODIFIER "u", v);
86 o << XsString(buffer);
87 return o;
88 }
89
90 inline XsString& operator<<(XsString& o, XsReal const& v)
91 {
92 char buffer[64];
93 sprintf(buffer, "%g", v);
94 o << XsString(buffer);
95 return o;
96 }
97
98 inline XsString& operator<<(XsString& o, XsVector const& xv)
99 {
100 o << "V<" << xv.size() << ">(";
101 for (XsSize i = 0; i < xv.size() - 1; i++)
102 o << xv[i] << ", ";
103 return (o << xv[xv.size() - 1] << ")");
104 }
105
106 inline XsString& operator<<(XsString& o, XsMatrix const& xm)
107 {
108 o << "M<" << xm.rows() << "," << xm.cols() << ">(";
109 for (XsSize r = 0; r < xm.rows(); ++r)
110 {
111 if (xm.cols())
112 {
113 if (r > 0)
114 o << "\t";
115 for (XsSize c = 0; c < xm.cols() - 1; ++c)
116 o << xm[r][c] << ", ";
117 o << xm[r][xm.cols()-1];
118 if (r < xm.rows() - 1)
119 o << "\n";
120 }
121 }
122 o << ")";
123 return o;
124 }
125
126 inline XsString& operator<<(XsString& o, XsQuaternion const& xq)
127 {
128 o << "Q(";
129 for (int i = 0; i < 3; i++)
130 o << xq[i] << ", ";
131 return (o << xq[3] << ")");
132 }
133
134 #endif
135
136 #endif