streamable.h
Go to the documentation of this file.00001 #ifndef streamable_h_included
00002 #define streamable_h_included
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00042
00043 #include <fstream>
00044 #include <cxxtls/classTraits.h>
00045
00046 namespace cxxtls
00047 {
00048
00049 template <class Owner, class charT=char, class traits = std::char_traits<charT> >
00050 class OstreamableBuffer: public std::basic_streambuf<charT, traits>
00054 {
00055 Owner *owner;
00056 public:
00057 typedef charT char_type;
00058 typedef traits traits_type;
00059 typedef typename traits_type::int_type int_type;
00060 typedef typename traits_type::off_type off_type;
00061 typedef typename traits_type::pos_type pos_type;
00062
00063 OstreamableBuffer(): owner(0) {}
00064
00065 void setOwner(Owner *o) { owner = o; }
00066
00067 protected:
00068 int_type overflow (int_type c = traits_type::eof ());
00069 };
00070
00071 template <class Owner, class charT, class traits>
00072 typename OstreamableBuffer<Owner, charT, traits>::int_type
00073 OstreamableBuffer<Owner, charT, traits>::
00074 overflow (typename OstreamableBuffer<Owner, charT, traits>::int_type c)
00075 {
00076
00077
00078
00079
00080
00081
00082 owner->ostreamable_helper(c);
00083 return c;
00084 }
00085
00086 template <class Derived, class charT=char, class traits = std::char_traits<charT> >
00087 class Ostreamable: public std::basic_iostream<charT, traits>
00094 {
00095 public:
00096 typedef charT char_type;
00097 typedef traits traits_type;
00098 typedef typename traits_type::int_type int_type;
00099 typedef typename traits_type::off_type off_type;
00100 typedef typename traits_type::pos_type pos_type;
00101
00102 typedef OstreamableBuffer<Derived, charT, traits> buf_type;
00103
00104 Ostreamable();
00105
00106
00107 private:
00108
00109 buf_type buf;
00110
00111 public:
00112
00113 };
00114
00115 template <class Derived, class charT, class traits>
00116 Ostreamable<Derived, charT, traits>::
00117 Ostreamable ()
00118 : std::basic_iostream<charT, traits>(&buf)
00119 {
00120 this->init (&buf);
00121
00122 Derived *owner=0;
00123
00124 derivedFromBase(this, owner);
00125
00126 buf.setOwner(owner);
00127 }
00128
00129
00130
00132
00133 class StreamableString
00134
00139
00140 : public Ostreamable<StreamableString>,
00141 public std::string
00142 {
00143 public:
00144
00145 void ostreamable_helper(char c)
00147 {
00148 std::string &p = *this;
00149
00150 p += c;
00151 }
00152
00153 StreamableString()
00154
00155 {
00156 }
00157
00158 StreamableString(StreamableString const &r)
00159 {
00160
00161 std::string &me = *this;
00162 me = r;
00163 }
00164
00165 StreamableString( std::string const &r )
00166 {
00167
00168 std::string &me = *this;
00169 me = r;
00170 }
00171
00172 StreamableString( char const * r)
00173 {
00174 std::string &me = *this;
00175 me = r;
00176 }
00177
00178 StreamableString &operator=(std::string const &rhs)
00179 {
00180 std::string &me = *this;
00181 me = rhs;
00182 return *this;
00183
00184 }
00185
00186 StreamableString &operator=(char const *rhs)
00187 {
00188 std::string &me = *this;
00189 me = rhs;
00190 return *this;
00191 }
00192
00193
00194 };
00195
00196 }
00197
00198 #endif