streamable.h

Go to the documentation of this file.
00001 #ifndef streamable_h_included
00002 #define streamable_h_included
00003 //
00004 // Copyright 2002, Lowell Boggs Jr.
00005 //
00006 // This file or directory, containing source code for a computer program,
00007 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00008 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00009 // source file without charge or obligation so long as you agree to
00010 // the following:
00011 //
00012 //  1.  You must indemnify Lowell Boggs against any and all financial
00013 //      obligations caused by its use, misuse, function, or malfunction.
00014 //      Further, you acknowledge that there is no warranty of any kind,
00015 //      whatsoever.
00016 //
00017 //  2.  You agree not to attempt to patent any portion of this original
00018 //      work -- though you may attempt to patent your own extensions to
00019 //      it if you so choose.
00020 //
00021 //  3.  You keep this copyright notice with the file and all copies
00022 //      of the file and do not change it anyway except language translation.
00023 //
00024 // You are responsible for enforcing your own compliance with these
00025 // conditions and may not use this source file if you cannot agree to the
00026 // above terms and conditions.
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 ()); // calls Owner::ostreamable_helper() 
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     // note that this is not a proper implemenation of overflow() for 
00077     // streams where you want to actually use the data in stream pointers.
00078     // This one should be copied with extreme caution.
00079     // See  http://stdcxx.apache.org/doc/stdlibug/39-2.html for a better
00080     // example, but still a wierd example...
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;    //2
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;  // SPOS MSVC 6.0
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 } // namespace cxxtls
00197 
00198 #endif
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3