CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DataStream.hh
Go to the documentation of this file.
1 #ifndef DATASTREAM_HH
2 #define DATASTREAM_HH
3 #include <functional>
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 namespace CASM {
8 
9 class jsonParser;
10 
13 class DataStream {
14  public:
15  enum DataStreamTraits { none = 0, skipfail = (1u << 0), failbit = (1u << 1) };
16 
17  static DataStream &endl(DataStream &_strm) { return _strm.newline(); }
18 
19  static std::function<DataStream &(DataStream &_strm)> failure(
20  std::string const &_msg);
21 
22  DataStream(DataStreamTraits _traits = none) : m_traits(_traits) {}
23 
24  virtual ~DataStream() {}
25 
26  virtual DataStream &operator<<(const std::string &) { return *this; }
27 
28  virtual DataStream &operator<<(long) { return *this; }
29 
30  virtual DataStream &operator<<(double) { return *this; }
31 
32  virtual DataStream &operator<<(bool) { return *this; }
33 
34  virtual DataStream &operator<<(char) { return *this; }
35 
36  /* Some day? This seems annoying...
37  virtual DataStream &operator<<(std::complex<double>) {
38  return *this;
39  }
40  */
41 
42  virtual DataStream &operator<<(jsonParser const &) {
43  throw std::runtime_error(
44  "Error in DataStream input: JSON may not be input to DataStream");
45  }
46 
47  DataStream &operator<<(DataStream &(*F)(DataStream &)) { return F(*this); }
48 
49  DataStream &operator<<(const std::function<DataStream &(DataStream &)> &F) {
50  return F(*this);
51  }
52 
53  virtual DataStream &newline() { return *this; }
54 
56  m_traits |= set_bits;
57  return *this;
58  }
59 
60  bool fail() const { return m_traits & failbit; }
61 
62  void clear_fail() {
63  m_traits &= ~failbit;
64  m_err_msg.clear();
65  }
66 
67  std::string const &err_msg() const { return m_err_msg; }
68 
69  protected:
70  void _set_err_msg(std::string const &_msg) { m_err_msg = _msg; }
71 
72  bool _skipfail() { return m_traits & skipfail; }
73 
74  private:
75  int m_traits;
76  std::string m_err_msg;
77 };
78 
79 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 // Count the number of POD objects streamed
82 class CountDataStream : public DataStream {
83  public:
84  CountDataStream(bool newline_reset = true)
85  : DataStream(none), m_newline_reset(newline_reset), m_count(0) {}
86 
87  DataStream &operator<<(const std::string &) override { return increment(); }
88 
89  DataStream &operator<<(long) override { return increment(); }
90 
91  DataStream &operator<<(double) override { return increment(); }
92 
93  DataStream &operator<<(bool) override { return increment(); }
94 
95  DataStream &operator<<(char) override { return increment(); }
96 
97  DataStream &newline() override {
98  if (m_newline_reset) m_count = 0;
99  return *this;
100  }
101 
102  int count() const { return m_count; }
103 
104  private:
106  m_count++;
107  return *this;
108  }
109 
111  int m_count;
112 };
113 
114 inline std::function<DataStream &(DataStream &_strm)> DataStream::failure(
115  std::string const &_msg) {
116  return [&_msg](DataStream &_stream) -> DataStream & {
117  _stream << failbit;
118  _stream._set_err_msg(_msg);
119  return _stream;
120  };
121 }
122 
123 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125 
126 namespace DataStream_impl {
127 template <typename OutType>
129  template <typename InType>
130  static OutType promote(InType a) {
131  return static_cast<OutType>(a);
132  }
133 };
134 
135 template <>
136 struct DataStreamPromoter<std::string> {
137  template <typename InType>
138  static std::string promote(InType a) {
139  return static_cast<std::string>(a);
140  }
141 };
142 
143 // Specialized String Promoters
144 template <>
145 std::string DataStreamPromoter<std::string>::promote(long a);
146 
147 template <>
148 std::string DataStreamPromoter<std::string>::promote(double a);
149 
150 template <>
151 std::string DataStreamPromoter<std::string>::promote(char a);
152 
153 template <>
154 std::string DataStreamPromoter<std::string>::promote(bool a);
155 //\End specialized string promoters
156 
157 template <>
158 struct DataStreamPromoter<double> {
159  template <typename InType>
160  static double promote(InType a) {
161  return static_cast<double>(a);
162  }
163 };
164 
165 // Specialized double promoter
166 template <>
167 double DataStreamPromoter<double>::promote(std::string a);
168 
169 template <>
170 struct DataStreamPromoter<long> {
171  template <typename InType>
172  static long promote(InType a) {
173  return static_cast<long>(a);
174  }
175 };
176 
177 // Specialized long promotion
178 template <>
179 long DataStreamPromoter<long>::promote(double a);
180 
181 template <>
182 long DataStreamPromoter<long>::promote(std::string a);
183 //\End Specialized long promotion
184 
185 template <>
186 struct DataStreamPromoter<bool> {
187  template <typename InType>
188  static bool promote(InType a) {
189  return static_cast<bool>(a);
190  }
191 };
192 
193 // Specialized bool promotion
194 template <>
195 bool DataStreamPromoter<bool>::promote(double a);
196 
197 template <>
198 bool DataStreamPromoter<bool>::promote(std::string a);
199 
200 template <>
202 //\End Specialized bool promotion
203 
204 template <>
205 struct DataStreamPromoter<char> {
206  template <typename InType>
207  static char promote(InType a) {
208  return static_cast<char>(a);
209  }
210 };
211 
212 // Specialized char promotion
213 template <>
214 char DataStreamPromoter<char>::promote(double a);
215 
216 template <>
217 char DataStreamPromoter<char>::promote(std::string a);
218 
219 template <>
221 //\End specialized char promotion
222 
223 } // namespace DataStream_impl
224 
225 template <typename T,
226  typename Promoter = DataStream_impl::DataStreamPromoter<T> >
227 class ValueDataStream : public DataStream {
228  public:
230 
231  DataStream &operator<<(const std::string &val) override {
232  m_value = Promoter::promote(val);
233  return *this;
234  }
235 
236  DataStream &operator<<(long val) override {
237  m_value = Promoter::promote(val);
238  return *this;
239  }
240 
241  DataStream &operator<<(double val) override {
242  m_value = Promoter::promote(val);
243  return *this;
244  }
245 
246  DataStream &operator<<(bool val) override {
247  m_value = Promoter::promote(val);
248  return *this;
249  }
250 
251  DataStream &operator<<(char val) override {
252  m_value = Promoter::promote(val);
253  return *this;
254  }
255 
256  const T &value() const { return m_value; }
257 
258  private:
260 };
261 
262 template <typename T,
263  typename Promoter = DataStream_impl::DataStreamPromoter<T> >
264 class VectorDataStream : public DataStream {
265  public:
267 
268  DataStream &operator<<(const std::string &val) override {
269  m_vector.push_back(Promoter::promote(val));
270  return *this;
271  }
272 
273  DataStream &operator<<(long val) override {
274  m_vector.push_back(Promoter::promote(val));
275  return *this;
276  }
277 
278  DataStream &operator<<(double val) override {
279  m_vector.push_back(Promoter::promote(val));
280  return *this;
281  }
282 
283  DataStream &operator<<(bool val) override {
284  m_vector.push_back(Promoter::promote(val));
285  return *this;
286  }
287 
288  DataStream &operator<<(char val) override {
289  m_vector.push_back(Promoter::promote(val));
290  return *this;
291  }
292 
293  const std::vector<T> &vector() const { return m_vector; }
294 
295  private:
296  std::vector<T> m_vector;
297 };
298 
299 inline DataStream &operator<<(DataStream &_stream, int i) {
300  return (_stream << (long)i);
301 }
302 
303 inline DataStream &operator<<(DataStream &_stream, float f) {
304  return (_stream << (double)f);
305 }
306 
307 inline DataStream &operator<<(DataStream &_stream, unsigned int i) {
308  return (_stream << (long)i);
309 }
310 
311 inline DataStream &operator<<(DataStream &_stream, unsigned long i) {
312  return (_stream << (long)i);
313 }
314 
315 template <class T>
316 inline DataStream &operator<<(DataStream &_stream, const std::vector<T> &vec) {
317  for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
318  _stream << *it;
319  }
320  return _stream;
321 }
322 
323 } // namespace CASM
324 
325 #include "casm/external/Eigen/Dense"
326 namespace Eigen {
327 template <typename Derived>
329  const MatrixBase<Derived> &value) {
330  for (int i = 0; i < value.rows(); i++) {
331  for (int j = 0; j < value.cols(); j++) {
332  _stream << value(i, j);
333  }
334  _stream.newline();
335  }
336  return _stream;
337 }
338 
339 } // namespace Eigen
340 #endif
DataStream & operator<<(bool) override
Definition: DataStream.hh:93
DataStream & increment()
Definition: DataStream.hh:105
DataStream & operator<<(double) override
Definition: DataStream.hh:91
CountDataStream(bool newline_reset=true)
Definition: DataStream.hh:84
DataStream & operator<<(long) override
Definition: DataStream.hh:89
DataStream & operator<<(const std::string &) override
Definition: DataStream.hh:87
DataStream & newline() override
Definition: DataStream.hh:97
DataStream & operator<<(char) override
Definition: DataStream.hh:95
DataStream & operator<<(DataStream &(*F)(DataStream &))
Definition: DataStream.hh:47
virtual DataStream & operator<<(double)
Definition: DataStream.hh:30
virtual DataStream & operator<<(const std::string &)
Definition: DataStream.hh:26
bool fail() const
Definition: DataStream.hh:60
DataStream(DataStreamTraits _traits=none)
Definition: DataStream.hh:22
virtual ~DataStream()
Definition: DataStream.hh:24
DataStream & operator<<(DataStreamTraits set_bits)
Definition: DataStream.hh:55
static DataStream & endl(DataStream &_strm)
Definition: DataStream.hh:17
virtual DataStream & operator<<(bool)
Definition: DataStream.hh:32
virtual DataStream & operator<<(long)
Definition: DataStream.hh:28
virtual DataStream & newline()
Definition: DataStream.hh:53
static std::function< DataStream &(DataStream &_strm)> failure(std::string const &_msg)
Definition: DataStream.hh:114
virtual DataStream & operator<<(char)
Definition: DataStream.hh:34
DataStream & operator<<(const std::function< DataStream &(DataStream &)> &F)
Definition: DataStream.hh:49
virtual DataStream & operator<<(jsonParser const &)
Definition: DataStream.hh:42
void _set_err_msg(std::string const &_msg)
Definition: DataStream.hh:70
std::string const & err_msg() const
Definition: DataStream.hh:67
std::string m_err_msg
Definition: DataStream.hh:76
DataStream & operator<<(bool val) override
Definition: DataStream.hh:246
DataStream & operator<<(const std::string &val) override
Definition: DataStream.hh:231
DataStream & operator<<(long val) override
Definition: DataStream.hh:236
ValueDataStream(DataStreamTraits _traits=none)
Definition: DataStream.hh:229
const T & value() const
Definition: DataStream.hh:256
DataStream & operator<<(char val) override
Definition: DataStream.hh:251
DataStream & operator<<(double val) override
Definition: DataStream.hh:241
DataStream & operator<<(const std::string &val) override
Definition: DataStream.hh:268
VectorDataStream(DataStreamTraits _traits=none)
Definition: DataStream.hh:266
DataStream & operator<<(char val) override
Definition: DataStream.hh:288
DataStream & operator<<(double val) override
Definition: DataStream.hh:278
DataStream & operator<<(long val) override
Definition: DataStream.hh:273
DataStream & operator<<(bool val) override
Definition: DataStream.hh:283
std::vector< T > m_vector
Definition: DataStream.hh:296
const std::vector< T > & vector() const
Definition: DataStream.hh:293
Main CASM namespace.
Definition: APICommand.hh:8
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
CASM::DataStream & operator<<(CASM::DataStream &_stream, const MatrixBase< Derived > &value)
Definition: DataStream.hh:328
Definition: stream_io.hh:24