eco_strstream.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of EcoLab
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
12 /*
13 
14 In particular, operator<< adds spaces in between its arguments, and a
15 new operator| is defined that is similar to strstream operator<<. This
16 makes it easier to construct TCL commands.
17 
18 Note that | has higher precedence than <<, so when mixing the two,
19 ensure that no << operator appears to the right of |, or use brackets
20 to ensure the correct interpretaion:
21 
22 eg.
23  s << a << b | c;
24 or
25  (s | a) << b | c;
26 
27 but not
28  s | a << b | c;
29 
30 In any case, you'll most likely get a compiler warning if you do the
31 wrong thing.
32 
33 eco_strstream is derived from ostringstream class, so has all the ostringstream
34 behaviour, aside from the above variant streaming behaviour.
35 
36 */
37 
38 #ifndef ECO_STRSTREAM_H
39 #define ECO_STRSTREAM_H
40 
41 #include <stdio.h>
42 #include <iostream>
43 #include <sstream>
44 #include <typeinfo>
45 
46 #include "error.h"
47 #include "classdesc.h"
48 
49 namespace ecolab
50 {
51  using namespace classdesc;
52  // we do this to define a catch-all operator<< that doesn't leak
53  using std::operator<<;
54  template<class T, class charT, class Traits>
55  std::basic_ostream<charT,Traits>& operator<<
56  (std::basic_ostream<charT,Traits>& o, const T& x)
57  {throw error("operator<< not defined for %s",typeid(T).name());}
58 
60 
65  {
66  std::ostringstream impl;
67  public:
68  eco_strstream() {}
70  {(*this) << x.str();}
71  eco_strstream(const std::ostringstream& x)
72  {(*this) << x.str();}
73 
74  string str() const {return impl.str();}
75 
76  /*
77  some implementations of ostringstream do not provide explicit
78  definitions character string constants
79  */
80  eco_strstream& operator|(const char* const& x)
81  {impl<<const_cast<const char*>(x); return *this;}
82  eco_strstream& operator|(char* const& x)
83  {impl<<const_cast<const char*>(x); return *this;}
84 
85  template <class E>
87  operator|(E x)
88  {
89  return operator|(enum_keys<E>()(x));
90  }
91 
92  template <class T>
94  operator|(const T& x)
95  {impl<<x; return *this;}
96 
97  template<class T>
98  eco_strstream& operator<<(const T& x)
99  {
100  if (this->str()[0]=='\0')
101  return (*this)|x;
102  else
103  return (*this)|' '|x;
104  }
105 
106  void clear() {impl.str(std::string());}
107 
108  };
109 
110  inline std::ostream& operator<<(std::ostream& x, const eco_strstream& y)
111  {return x<<y.str();}
112 }
113 
114 #endif
An EcoLab string stream class.
Definition: eco_strstream.h:64
EcoLab exception class.
Definition: TCL_obj_base.h:327
controlled template specialisation: stolen from boost::enable_if.
Definition: TCL_obj_base.h:250
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
_OPENMP
Definition: accessor.h:16