Chemical Data Processing Library C++ API - Version 1.4.0
IO.hpp
Go to the documentation of this file.
1 /*
2  * IO.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_IO_HPP
28 #define CDPL_MATH_IO_HPP
29 
30 #include <ostream>
31 #include <sstream>
32 
33 
34 namespace CDPL
35 {
36 
37  namespace Math
38  {
39 
40  template <typename E>
41  class VectorExpression;
42  template <typename E>
43  class MatrixExpression;
44  template <typename E>
46  template <typename E>
47  class GridExpression;
48 
61  template <typename C, typename T, typename E>
62  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const VectorExpression<E>& e)
63  {
64  typename std::basic_ostream<C, T>::sentry se(os);
65  std::ios_base::iostate state(std::ios_base::goodbit);
66 
67  if (se) {
68  try {
69  std::basic_ostringstream<C, T, std::allocator<C> > oss;
70 
71  oss.flags(os.flags());
72  oss.imbue(os.getloc());
73  oss.precision(os.precision());
74 
75  typedef typename E::SizeType SizeType;
76 
77  SizeType size = e().getSize();
78 
79  oss << '[' << size << "](";
80 
81  if (size > 0)
82  oss << e()(0);
83 
84  for (SizeType i = 1; i < size; i++)
85  oss << ',' << e()(i);
86 
87  oss << ')';
88 
89  if (!oss.good())
90  state |= std::ios_base::failbit;
91  else
92  os << oss.str().c_str();
93 
94  } catch (...) {
95  os.setstate(std::ios_base::failbit);
96  throw;
97  }
98  }
99 
100  if (state != std::ios_base::goodbit)
101  os.setstate(state);
102 
103  return os;
104  }
105 
122  template <typename C, typename T, typename E>
123  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const MatrixExpression<E>& e)
124  {
125  typename std::basic_ostream<C, T>::sentry se(os);
126  std::ios_base::iostate state(std::ios_base::goodbit);
127 
128  if (se) {
129  try {
130  std::basic_ostringstream<C, T, std::allocator<C> > oss;
131 
132  oss.flags(os.flags());
133  oss.imbue(os.getloc());
134  oss.precision(os.precision());
135 
136  typedef typename E::SizeType SizeType;
137 
138  SizeType size1 = e().getSize1();
139  SizeType size2 = e().getSize2();
140 
141  oss << '[' << size1 << ',' << size2 << "](";
142 
143  if (size1 > 0 && size2 > 0) {
144  for (SizeType i = 0; i < size1; i++) {
145  if (i > 0)
146  oss << ',';
147 
148  oss << '(';
149 
150  for (SizeType j = 0; j < size2; j++) {
151  if (j > 0)
152  oss << ',';
153 
154  oss << e()(i, j);
155  }
156 
157  oss << ')';
158  }
159  }
160 
161  oss << ')';
162 
163  if (!oss.good())
164  state |= std::ios_base::failbit;
165  else
166  os << oss.str().c_str();
167 
168  } catch (...) {
169  os.setstate(std::ios_base::failbit);
170  throw;
171  }
172  }
173 
174  if (state != std::ios_base::goodbit)
175  os.setstate(state);
176 
177  return os;
178  }
179 
192  template <typename C, typename T, typename E>
193  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const QuaternionExpression<E>& e)
194  {
195  typename std::basic_ostream<C, T>::sentry se(os);
196  std::ios_base::iostate state(std::ios_base::goodbit);
197 
198  if (se) {
199  try {
200  std::basic_ostringstream<C, T, std::allocator<C> > oss;
201 
202  oss.flags(os.flags());
203  oss.imbue(os.getloc());
204  oss.precision(os.precision());
205 
206  oss << '(' << e().getC1() << ',' << e().getC2() << ',' << e().getC3() << ',' << e().getC4() << ')';
207 
208  if (!oss.good())
209  state |= std::ios_base::failbit;
210  else
211  os << oss.str().c_str();
212 
213  } catch (...) {
214  os.setstate(std::ios_base::failbit);
215  throw;
216  }
217  }
218 
219  if (state != std::ios_base::goodbit)
220  os.setstate(state);
221 
222  return os;
223  }
224 
242  template <typename C, typename T, typename E>
243  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const GridExpression<E>& e)
244  {
245  typename std::basic_ostream<C, T>::sentry se(os);
246  std::ios_base::iostate state(std::ios_base::goodbit);
247 
248  if (se) {
249  try {
250  std::basic_ostringstream<C, T, std::allocator<C> > oss;
251 
252  oss.flags(os.flags());
253  oss.imbue(os.getloc());
254  oss.precision(os.precision());
255 
256  typedef typename E::SizeType SizeType;
257 
258  SizeType size1 = e().getSize1();
259  SizeType size2 = e().getSize2();
260  SizeType size3 = e().getSize3();
261 
262  oss << '[' << size1 << ',' << size2 << ',' << size3 << "](";
263 
264  if (size1 > 0 && size2 > 0 && size3 > 0) {
265  for (SizeType i = 0; i < size1; i++) {
266  if (i > 0)
267  oss << ',';
268 
269  oss << '(';
270 
271  for (SizeType j = 0; j < size2; j++) {
272  if (j > 0)
273  oss << ',';
274 
275  oss << '(';
276 
277  for (SizeType k = 0; k < size3; k++) {
278  if (k > 0)
279  oss << ',';
280 
281  oss << e()(i, j, k);
282  }
283 
284  oss << ')';
285  }
286 
287  oss << ')';
288  }
289  }
290 
291  oss << ')';
292 
293  if (!oss.good())
294  state |= std::ios_base::failbit;
295  else
296  os << oss.str().c_str();
297 
298  } catch (...) {
299  os.setstate(std::ios_base::failbit);
300  throw;
301  }
302  }
303 
304  if (state != std::ios_base::goodbit)
305  os.setstate(state);
306 
307  return os;
308  }
309  } // namespace Math
310 } // namespace CDPL
311 
312 #endif // CDPL_MATH_IO_HPP
CRTP base class for all grid expression types.
Definition: Expression.hpp:180
CRTP base class for all matrix expression types.
Definition: Expression.hpp:104
CRTP base class for all quaternion expression types.
Definition: Expression.hpp:142
CRTP base class for all vector expression types.
Definition: Expression.hpp:66
std::basic_ostream< C, T > & operator<<(std::basic_ostream< C, T > &os, const VectorExpression< E > &e)
Writes a textual representation of the vector expression e to os.
Definition: IO.hpp:62
The namespace of the Chemical Data Processing Library.