/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Calibration/facilities/facilities-00-00-04/facilities/binarystream.h

Go to the documentation of this file.
00001 // $Id: binarystream.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $
00002 //  binarystream.h
00003 //  Author: Sawyer Gillespie
00004 //          UW Department of Physics
00005 //          hgillesp@u.washington.edu
00006 //  Class definitions for i/o strictly into a buffer (rather than some real I/O)
00007 
00008 #ifndef binarystreams_h_
00009 #define binarystreams_h_
00010 
00011 #include "streamdef.h"
00012 
00013 /* basic_binstreambuf - class definition
00014     The basic_binstreambuf class provides a means for sequential access
00015     to a block of memory. This class manages a buffer of a pre-determined
00016     size - which is passed in to its constructor. From that point on, the 
00017     standard streambuf pointers pput, pget, pstart, pend all point to the 
00018     appropriate locations within the allocated buffer. The size of the buffer
00019     is the first thing written into the buffer and will always occupy the 
00020     first 4 bytes (sizeof (std::streamsize)) of the buffer.
00021 */
00022 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00023 #ifdef _MSC_VER
00024 class   basic_binstreambuf : virtual public std::basic_streambuf<_Ch, _Tr> {
00025 #else 
00026 class   basic_binstreambuf : virtual public streambuf {
00027 #endif
00028 public:
00029     basic_binstreambuf (std::streamsize sz) : _outbuf(new _Ch[sz + sizeof(std::streamsize)/sizeof(_Ch)]), _inbuf(0) {
00030         memcpy(_outbuf, &sz, sizeof(std::streamsize));
00031         setp (_outbuf + sizeof(std::streamsize), _outbuf + sz*sizeof(_Ch) + sizeof(unsigned int));
00032     }
00033     basic_binstreambuf (const _Ch* p) : _outbuf(0), _inbuf(0) {
00034         std::streamsize    sz = *((const std::streamsize*)p);
00035         _inbuf = new _Ch[sz];
00036         memcpy (_inbuf, p + sizeof(std::streamsize), sz);
00037         setg (_inbuf, _inbuf, _inbuf + sz * sizeof(_Ch));
00038     }
00039     virtual ~basic_binstreambuf () { delete _outbuf; delete _inbuf; }
00040 
00041     // givebuf - hand the output buffer over to someone who can use it...
00042     _Ch*    givebuf () {
00043         _Ch*    p = _outbuf;
00044         _outbuf = 0;
00045         return p;
00046     }
00047 
00048     // return the total size of the output buffer, in terms of the standard allocation
00049     // unit
00050     size_t  outbufsize () const {
00051         return (_outbuf) ? (sizeof(_Ch) * (*(std::streamsize*)_outbuf) + sizeof(std::streamsize)) : 0;
00052     }
00053     
00054     // static method to compute the size of the buffer needed to store an object of
00055     // size s.
00056     static size_t  computesize (size_t s) {
00057         return s + sizeof(std::streamsize);
00058     }
00059 
00060 private:
00061     _Ch*  _outbuf;
00062     _Ch*  _inbuf; 
00063 };
00064 
00065 /* basic_binostream - class definition
00066     The basic_binostream class creates and manages an instance of basic_binstreambuf
00067     which it uses as a storage buffer for the data which is written to the stream.
00068     The only constructor which is available specifies the size of the data to come. Thus
00069     basic_binostreams can only handle data of a pre-determined size! Any data written to
00070     a basic_binostream must be extracted by using basic_binistream!
00071 */
00072 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00073 #ifdef _MSC_VER
00074 class   basic_binostream : public std::basic_ostream<_Ch, _Tr>  {
00075 #else
00076 class   basic_binostream : public std::basic_ostream {
00077 #endif
00078 public:
00079     basic_binostream (std::streamsize sz) 
00080 #ifdef _MSC_VER
00081         : std::basic_ostream<_Ch,_Tr>(_buf = new basic_binstreambuf<_Ch,_Tr>(sz)) {}
00082 #else
00083     : ostream (_buf = new basic_binstreambuf<_Ch,_Tr>(sz)) {}
00084 #endif
00085     virtual ~basic_binostream () { delete _buf; }
00086 
00087     // popbuf - returns the stored data as a void* pointer and removes it from the
00088     //          streambuffer object (the streambuffer is destroyed!)
00089     void*   popbuf (size_t& sz) {
00090         sz = _buf->outbufsize();
00091         return _buf->givebuf ();
00092     }
00093 
00094     // computesize - compute the size of the buffer needed to store an object of
00095     //               size s.
00096     static size_t   computesize (size_t s) {
00097         return basic_binstreambuf<_Ch,_Tr>::computesize(s);
00098     }
00099 
00100 private:
00101     basic_binstreambuf<_Ch,_Tr>* _buf;
00102 };
00103 
00104 /* basic_binostream - class definition
00105     The basic_binistream class extracts data from a buffer created by basic_binostream.
00106     Note that the buffer passed to the constructor must have been created by an
00107     instance of basic_binostream, otherwise the results will be unreliable, if not
00108     fatal.
00109 */
00110 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00111 #ifdef _MSC_VER
00112 class   basic_binistream : public std::basic_istream<_Ch, _Tr> {
00113 #else
00114 class   basic_binistream : public istream {
00115 #endif
00116 public:
00117     basic_binistream (const _Ch* ptr)
00118 #ifdef _MSC_VER
00119         : std::basic_istream<_Ch,_Tr>(_buf = new basic_binstreambuf<_Ch,_Tr>(ptr)) {}
00120 #else
00121     : basic_istream (_buf = new basic_binstreambuf<_Ch,_Tr>(ptr)) {}
00122 #endif
00123     virtual ~basic_binistream () { delete _buf; }
00124 
00125 private:
00126     basic_binstreambuf<_Ch,_Tr>* _buf;
00127 };
00128 
00129 template <class _Ty, class _Ch, class _Tr> 
00130 inline basic_binostream<_Ch, _Tr>&  operator << ( basic_binostream<_Ch,_Tr>& o, const _Ty  t ) {
00131     int     sz = sizeof(_Ty);
00132     o.write ((const _Ch*)(&t), sz);
00133     return o;
00134 }
00135 
00136 template <class _Ty, class _Ch, class _Tr>
00137 inline basic_binistream<_Ch,_Tr>&  operator >> ( basic_binistream<_Ch,_Tr>& i, _Ty& t ) {
00138     int     sz = sizeof(_Ty);
00139     i.read ((_Ch*)(&t),sz);
00140     return i;
00141 }
00142 
00143 /*  The classes binostream, binistream, and binstreambuf define binary stream 
00144     implementations based upon the char datatype (using byte resolution for allocation). 
00145 */ 
00146 typedef basic_binostream<char>  binostream;
00147 typedef basic_binistream<char>  binistream;
00148 typedef basic_binstreambuf<char> binstreambuf;
00149 
00150 #endif

Generated on Tue Nov 29 22:57:55 2016 for BOSS_7.0.2 by  doxygen 1.4.7