Chemical Data Processing Library C++ API - Version 1.1.1
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 
49  template <typename C, typename T, typename E>
50  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const VectorExpression<E>& e)
51  {
52  typename std::basic_ostream<C, T>::sentry se(os);
53  std::ios_base::iostate state(std::ios_base::goodbit);
54 
55  if (se) {
56  try {
57  std::basic_ostringstream<C, T, std::allocator<C> > oss;
58 
59  oss.flags(os.flags());
60  oss.imbue(os.getloc());
61  oss.precision(os.precision());
62 
63  typedef typename E::SizeType SizeType;
64 
65  SizeType size = e().getSize();
66 
67  oss << '[' << size << "](";
68 
69  if (size > 0)
70  oss << e()(0);
71 
72  for (SizeType i = 1; i < size; i++)
73  oss << ',' << e()(i);
74 
75  oss << ')';
76 
77  if (!oss.good())
78  state |= std::ios_base::failbit;
79  else
80  os << oss.str().c_str();
81 
82  } catch (...) {
83  os.setstate(std::ios_base::failbit);
84  throw;
85  }
86  }
87 
88  if (state != std::ios_base::goodbit)
89  os.setstate(state);
90 
91  return os;
92  }
93 
94  template <typename C, typename T, typename E>
95  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const MatrixExpression<E>& e)
96  {
97  typename std::basic_ostream<C, T>::sentry se(os);
98  std::ios_base::iostate state(std::ios_base::goodbit);
99 
100  if (se) {
101  try {
102  std::basic_ostringstream<C, T, std::allocator<C> > oss;
103 
104  oss.flags(os.flags());
105  oss.imbue(os.getloc());
106  oss.precision(os.precision());
107 
108  typedef typename E::SizeType SizeType;
109 
110  SizeType size1 = e().getSize1();
111  SizeType size2 = e().getSize2();
112 
113  oss << '[' << size1 << ',' << size2 << "](";
114 
115  if (size1 > 0 && size2 > 0) {
116  for (SizeType i = 0; i < size1; i++) {
117  if (i > 0)
118  oss << ',';
119 
120  oss << '(';
121 
122  for (SizeType j = 0; j < size2; j++) {
123  if (j > 0)
124  oss << ',';
125 
126  oss << e()(i, j);
127  }
128 
129  oss << ')';
130  }
131  }
132 
133  oss << ')';
134 
135  if (!oss.good())
136  state |= std::ios_base::failbit;
137  else
138  os << oss.str().c_str();
139 
140  } catch (...) {
141  os.setstate(std::ios_base::failbit);
142  throw;
143  }
144  }
145 
146  if (state != std::ios_base::goodbit)
147  os.setstate(state);
148 
149  return os;
150  }
151 
152  template <typename C, typename T, typename E>
153  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const QuaternionExpression<E>& e)
154  {
155  typename std::basic_ostream<C, T>::sentry se(os);
156  std::ios_base::iostate state(std::ios_base::goodbit);
157 
158  if (se) {
159  try {
160  std::basic_ostringstream<C, T, std::allocator<C> > oss;
161 
162  oss.flags(os.flags());
163  oss.imbue(os.getloc());
164  oss.precision(os.precision());
165 
166  oss << '(' << e().getC1() << ',' << e().getC2() << ',' << e().getC3() << ',' << e().getC4() << ')';
167 
168  if (!oss.good())
169  state |= std::ios_base::failbit;
170  else
171  os << oss.str().c_str();
172 
173  } catch (...) {
174  os.setstate(std::ios_base::failbit);
175  throw;
176  }
177  }
178 
179  if (state != std::ios_base::goodbit)
180  os.setstate(state);
181 
182  return os;
183  }
184 
185  template <typename C, typename T, typename E>
186  std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, const GridExpression<E>& e)
187  {
188  typename std::basic_ostream<C, T>::sentry se(os);
189  std::ios_base::iostate state(std::ios_base::goodbit);
190 
191  if (se) {
192  try {
193  std::basic_ostringstream<C, T, std::allocator<C> > oss;
194 
195  oss.flags(os.flags());
196  oss.imbue(os.getloc());
197  oss.precision(os.precision());
198 
199  typedef typename E::SizeType SizeType;
200 
201  SizeType size1 = e().getSize1();
202  SizeType size2 = e().getSize2();
203  SizeType size3 = e().getSize3();
204 
205  oss << '[' << size1 << ',' << size2 << ',' << size3 << "](";
206 
207  if (size1 > 0 && size2 > 0 && size3 > 0) {
208  for (SizeType i = 0; i < size1; i++) {
209  if (i > 0)
210  oss << ',';
211 
212  oss << '(';
213 
214  for (SizeType j = 0; j < size2; j++) {
215  if (j > 0)
216  oss << ',';
217 
218  oss << '(';
219 
220  for (SizeType k = 0; k < size3; k++) {
221  if (k > 0)
222  oss << ',';
223 
224  oss << e()(i, j, k);
225  }
226 
227  oss << ')';
228  }
229 
230  oss << ')';
231  }
232  }
233 
234  oss << ')';
235 
236  if (!oss.good())
237  state |= std::ios_base::failbit;
238  else
239  os << oss.str().c_str();
240 
241  } catch (...) {
242  os.setstate(std::ios_base::failbit);
243  throw;
244  }
245  }
246 
247  if (state != std::ios_base::goodbit)
248  os.setstate(state);
249 
250  return os;
251  }
252  } // namespace Math
253 } // namespace CDPL
254 
255 #endif // CDPL_MATH_IO_HPP
CDPL::Math::VectorExpression
Definition: Expression.hpp:54
CDPL::Math::MatrixExpression
Definition: Expression.hpp:76
CDPL::Math::QuaternionExpression
Definition: Expression.hpp:98
CDPL::Math::operator<<
std::basic_ostream< C, T > & operator<<(std::basic_ostream< C, T > &os, const VectorExpression< E > &e)
Definition: IO.hpp:50
CDPL::Math::GridExpression
Definition: Expression.hpp:120
CDPL
The namespace of the Chemical Data Processing Library.