BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/include/facilities/binarystream.h
Go to the documentation of this file.
1// $Id: binarystream.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $
2// binarystream.h
3// Author: Sawyer Gillespie
4// UW Department of Physics
5// hgillesp@u.washington.edu
6// Class definitions for i/o strictly into a buffer (rather than some real I/O)
7
8#ifndef binarystreams_h_
9#define binarystreams_h_
10
11#include "streamdef.h"
12
13/* basic_binstreambuf - class definition
14 The basic_binstreambuf class provides a means for sequential access
15 to a block of memory. This class manages a buffer of a pre-determined
16 size - which is passed in to its constructor. From that point on, the
17 standard streambuf pointers pput, pget, pstart, pend all point to the
18 appropriate locations within the allocated buffer. The size of the buffer
19 is the first thing written into the buffer and will always occupy the
20 first 4 bytes (sizeof (std::streamsize)) of the buffer.
21*/
22template <class _Ch, class _Tr = std::char_traits<_Ch>>
23#ifdef _MSC_VER
24class basic_binstreambuf : virtual public std::basic_streambuf<_Ch, _Tr> {
25#else
26class basic_binstreambuf : virtual public streambuf {
27#endif
28public:
29 basic_binstreambuf( std::streamsize sz )
30 : _outbuf( new _Ch[sz + sizeof( std::streamsize ) / sizeof( _Ch )] ), _inbuf( 0 ) {
31 memcpy( _outbuf, &sz, sizeof( std::streamsize ) );
32 setp( _outbuf + sizeof( std::streamsize ),
33 _outbuf + sz * sizeof( _Ch ) + sizeof( unsigned int ) );
34 }
35 basic_binstreambuf( const _Ch* p ) : _outbuf( 0 ), _inbuf( 0 ) {
36 std::streamsize sz = *( (const std::streamsize*)p );
37 _inbuf = new _Ch[sz];
38 memcpy( _inbuf, p + sizeof( std::streamsize ), sz );
39 setg( _inbuf, _inbuf, _inbuf + sz * sizeof( _Ch ) );
40 }
42 delete _outbuf;
43 delete _inbuf;
44 }
45
46 // givebuf - hand the output buffer over to someone who can use it...
47 _Ch* givebuf() {
48 _Ch* p = _outbuf;
49 _outbuf = 0;
50 return p;
51 }
52
53 // return the total size of the output buffer, in terms of the standard allocation
54 // unit
55 size_t outbufsize() const {
56 return ( _outbuf )
57 ? ( sizeof( _Ch ) * ( *(std::streamsize*)_outbuf ) + sizeof( std::streamsize ) )
58 : 0;
59 }
60
61 // static method to compute the size of the buffer needed to store an object of
62 // size s.
63 static size_t computesize( size_t s ) { return s + sizeof( std::streamsize ); }
64
65private:
66 _Ch* _outbuf;
67 _Ch* _inbuf;
68};
69
70/* basic_binostream - class definition
71 The basic_binostream class creates and manages an instance of basic_binstreambuf
72 which it uses as a storage buffer for the data which is written to the stream.
73 The only constructor which is available specifies the size of the data to come. Thus
74 basic_binostreams can only handle data of a pre-determined size! Any data written to
75 a basic_binostream must be extracted by using basic_binistream!
76*/
77template <class _Ch, class _Tr = std::char_traits<_Ch>>
78#ifdef _MSC_VER
79class basic_binostream : public std::basic_ostream<_Ch, _Tr> {
80#else
81class basic_binostream : public std::basic_ostream {
82#endif
83public:
84 basic_binostream( std::streamsize sz )
85#ifdef _MSC_VER
86 : std::basic_ostream<_Ch, _Tr>( _buf = new basic_binstreambuf<_Ch, _Tr>( sz ) ){}
87#else
88 : ostream( _buf = new basic_binstreambuf<_Ch, _Tr>( sz ) ) {
89 }
90#endif
92 delete _buf;
93 }
94
95 // popbuf - returns the stored data as a void* pointer and removes it from the
96 // streambuffer object (the streambuffer is destroyed!)
97 void* popbuf( size_t& sz ) {
98 sz = _buf->outbufsize();
99 return _buf->givebuf();
100 }
101
102 // computesize - compute the size of the buffer needed to store an object of
103 // size s.
104 static size_t computesize( size_t s ) {
106 }
107
108private:
110};
111
112/* basic_binostream - class definition
113 The basic_binistream class extracts data from a buffer created by basic_binostream.
114 Note that the buffer passed to the constructor must have been created by an
115 instance of basic_binostream, otherwise the results will be unreliable, if not
116 fatal.
117*/
118template <class _Ch, class _Tr = std::char_traits<_Ch>>
119#ifdef _MSC_VER
120class basic_binistream : public std::basic_istream<_Ch, _Tr> {
121#else
122class basic_binistream : public istream {
123#endif
124public:
125 basic_binistream( const _Ch* ptr )
126#ifdef _MSC_VER
127 : std::basic_istream<_Ch, _Tr>( _buf = new basic_binstreambuf<_Ch, _Tr>( ptr ) ){}
128#else
129 : basic_istream( _buf = new basic_binstreambuf<_Ch, _Tr>( ptr ) ) {
130 }
131#endif
133 delete _buf;
134 }
135
136private:
138};
139
140template <class _Ty, class _Ch, class _Tr>
141inline basic_binostream<_Ch, _Tr>& operator<<( basic_binostream<_Ch, _Tr>& o, const _Ty t ) {
142 int sz = sizeof( _Ty );
143 o.write( (const _Ch*)( &t ), sz );
144 return o;
145}
146
147template <class _Ty, class _Ch, class _Tr>
149 int sz = sizeof( _Ty );
150 i.read( (_Ch*)( &t ), sz );
151 return i;
152}
153
154/* The classes binostream, binistream, and binstreambuf define binary stream
155 implementations based upon the char datatype (using byte resolution for allocation).
156*/
160
161#endif
basic_binstreambuf< char > binstreambuf
std::istream & operator>>(std::istream &is, CosmicEventParser &ev)
ostream & operator<<(ostream &s, const EvtComplex &c)
Definition EvtComplex.cc:27
XmlRpcServer s
int t()
Definition t.c:1