Mathter
A configurable 3D math library for game developers.
IoStream.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Vector.hpp"
4 
5 #include <iostream>
6 
7 namespace mathter {
8 
9 enum class eEnclosingBracket {
10  NONE,
11  PARANTHESE,
12  BRACKET,
13  BRACE,
14 };
15 
17 template <class T, int Dim, bool Packed>
18 std::ostream& operator<<(std::ostream& os, const mathter::Vector<T, Dim, Packed>& v) {
19  os << "[";
20  for (int x = 0; x < Dim; ++x) {
21  os << v(x) << (x == Dim - 1 ? "" : ", ");
22  }
23  os << "]";
24  return os;
25 }
26 
27 
28 namespace impl {
29  template <class T>
30  struct dependent_false {
31  static constexpr bool value = false;
32  };
33  template <class T>
34  constexpr bool dependent_false_v = dependent_false<T>::value;
35 
36  template <class AritT, typename std::enable_if<std::is_integral<AritT>::value && std::is_signed<AritT>::value, int>::type = 0>
37  AritT strtonum(const char* str, const char** end) {
38  AritT value;
39  value = (AritT)strtoll(str, (char**)end, 10);
40  return value;
41  }
42  template <class AritT, typename std::enable_if<std::is_integral<AritT>::value && !std::is_signed<AritT>::value, int>::type = 0>
43  AritT strtonum(const char* str, const char** end) {
44  AritT value;
45  value = (AritT)strtoull(str, (char**)end, 10);
46  return value;
47  }
48  template <class AritT, typename std::enable_if<std::is_floating_point<AritT>::value, int>::type = 0>
49  AritT strtonum(const char* str, const char** end) {
50  AritT value;
51  value = (AritT)strtold(str, (char**)end);
52  return value;
53  }
54 
55  inline const char* StripSpaces(const char* str) {
56  while (*str != '\0' && isspace(*str))
57  ++str;
58  return str;
59  };
60 
61 } // namespace impl
62 
64 template <class T, int Dim, bool Packed>
65 Vector<T, Dim, Packed> strtovec(const char* str, const char** end) {
67 
68  const char* strproc = str;
69 
70  // parse initial bracket if any
71  strproc = impl::StripSpaces(strproc);
72  if (*strproc == '\0') {
73  *end = str;
74  return ret;
75  }
76 
77  char startBracket = *strproc;
78  char endBracket;
79  bool hasBrackets = false;
80  switch (startBracket) {
81  case '(':
82  endBracket = ')';
83  hasBrackets = true;
84  ++strproc;
85  break;
86  case '[':
87  endBracket = ']';
88  hasBrackets = true;
89  ++strproc;
90  break;
91  case '{':
92  endBracket = '}';
93  hasBrackets = true;
94  ++strproc;
95  break;
96  }
97 
98  // parse elements
99  for (int i = 0; i < Dim; ++i) {
100  const char* elemend;
101  T elem = impl::strtonum<T>(strproc, &elemend);
102  if (elemend == strproc) {
103  *end = str;
104  return ret;
105  }
106  else {
107  ret[i] = elem;
108  strproc = elemend;
109  }
110  strproc = impl::StripSpaces(strproc);
111  if (*strproc == ',') {
112  ++strproc;
113  }
114  }
115 
116  // parse ending bracket corresponding to initial bracket
117  if (hasBrackets) {
118  strproc = impl::StripSpaces(strproc);
119  if (*strproc != endBracket) {
120  *end = str;
121  return ret;
122  }
123  ++strproc;
124  }
125 
126  *end = strproc;
127  return ret;
128 }
129 
130 template <class VectorT>
131 VectorT strtovec(const char* str, const char** end) {
132  static_assert(traits::IsVector<VectorT>::value, "This type is not a Vector, dumbass.");
133 
134  return strtovec<
138 }
139 
140 
141 
142 
143 
144 
145 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
146 std::ostream& operator<<(std::ostream& os, const Matrix<T, Rows, Columns, Order, Layout, Packed>& mat) {
147  os << "[";
148  for (int i = 0; i < mat.Height(); ++i) {
149  for (int j = 0; j < mat.Width(); ++j) {
150  os << mat(i, j) << (j == mat.Width() - 1 ? "" : ", ");
151  }
152  if (i < Rows - 1) {
153  os << "; ";
154  }
155  }
156  os << "]";
157  return os;
158 }
159 
160 
161 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
162 Matrix<T, Rows, Columns, Order, Layout, Packed> strtomat(const char* str, const char** end) {
164  using VectorT = Vector<T, Columns, Packed>;
165  MatrixT ret;
166 
167  const char* strproc = str;
168 
169  // parse initial bracket if any
170  strproc = impl::StripSpaces(strproc);
171  if (*strproc == '\0') {
172  *end = str;
173  return ret;
174  }
175 
176  char startBracket = *strproc;
177  char endBracket;
178  bool hasBrackets = false;
179  switch (startBracket) {
180  case '(':
181  endBracket = ')';
182  hasBrackets = true;
183  ++strproc;
184  break;
185  case '[':
186  endBracket = ']';
187  hasBrackets = true;
188  ++strproc;
189  break;
190  case '{':
191  endBracket = '}';
192  hasBrackets = true;
193  ++strproc;
194  break;
195  }
196 
197  // parse rows
198  for (int i = 0; i < Rows; ++i) {
199  const char* rowend;
200  VectorT row = strtovec<VectorT>(strproc, &rowend);
201  if (rowend == strproc) {
202  *end = str;
203  return ret;
204  }
205  else {
206  ret.Row(i) = row;
207  strproc = rowend;
208  }
209  strproc = impl::StripSpaces(strproc);
210  if (i < Rows - 1) {
211  if (*strproc == ';') {
212  ++strproc;
213  }
214  else {
215  *end = str;
216  return ret;
217  }
218  }
219  }
220 
221  // parse ending bracket corresponding to initial bracket
222  if (hasBrackets) {
223  strproc = impl::StripSpaces(strproc);
224  if (*strproc != endBracket) {
225  *end = str;
226  return ret;
227  }
228  ++strproc;
229  }
230 
231  *end = strproc;
232  return ret;
233 }
234 
235 template <class MatrixT>
236 MatrixT strtomat(const char* str, const char** end) {
237  static_assert(traits::IsMatrix<MatrixT>::value, "This type if not a matrix, dumbass.");
238 
239  return strtomat<
246 }
247 
248 
249 
250 template <class T, bool Packed>
251 std::ostream& operator<<(std::ostream& os, const Quaternion<T, Packed>& q) {
252  os << "["
253  << q.Angle() * T(180.0) / T(3.1415926535897932384626)
254  << " deg @ "
255  << q.Axis()
256  << "]";
257  return os;
258 }
259 
260 
261 } // namespace mathter
Matrix< T, Rows, Columns, Order, Layout, Packed > strtomat(const char *str, const char **end)
Definition: IoStream.hpp:162
Definition: Traits.hpp:113
Definition: Traits.hpp:144
Definition: Traits.hpp:29
eEnclosingBracket
Definition: IoStream.hpp:9
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
Definition: Traits.hpp:48
const char * StripSpaces(const char *str)
Definition: IoStream.hpp:55
constexpr bool dependent_false_v
Definition: IoStream.hpp:34
Definition: Approx.hpp:11
Vector< T, Dim, Packed > strtovec(const char *str, const char **end)
Parses a vector from a string.
Definition: IoStream.hpp:65
Definition: Definitions.hpp:63
AritT strtonum(const char *str, const char **end)
Definition: IoStream.hpp:37