BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
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
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) : _outbuf(new _Ch[sz + sizeof(std::streamsize)/sizeof(_Ch)]), _inbuf(0) {
30 memcpy(_outbuf, &sz, sizeof(std::streamsize));
31 setp (_outbuf + sizeof(std::streamsize), _outbuf + sz*sizeof(_Ch) + sizeof(unsigned int));
32 }
33 basic_binstreambuf (const _Ch* p) : _outbuf(0), _inbuf(0) {
34 std::streamsize sz = *((const std::streamsize*)p);
35 _inbuf = new _Ch[sz];
36 memcpy (_inbuf, p + sizeof(std::streamsize), sz);
37 setg (_inbuf, _inbuf, _inbuf + sz * sizeof(_Ch));
38 }
39 virtual ~basic_binstreambuf () { delete _outbuf; delete _inbuf; }
40
41 // givebuf - hand the output buffer over to someone who can use it...
42 _Ch* givebuf () {
43 _Ch* p = _outbuf;
44 _outbuf = 0;
45 return p;
46 }
47
48 // return the total size of the output buffer, in terms of the standard allocation
49 // unit
50 size_t outbufsize () const {
51 return (_outbuf) ? (sizeof(_Ch) * (*(std::streamsize*)_outbuf) + sizeof(std::streamsize)) : 0;
52 }
53
54 // static method to compute the size of the buffer needed to store an object of
55 // size s.
56 static size_t computesize (size_t s) {
57 return s + sizeof(std::streamsize);
58 }
59
60private:
61 _Ch* _outbuf;
62 _Ch* _inbuf;
63};
64
65/* basic_binostream - class definition
66 The basic_binostream class creates and manages an instance of basic_binstreambuf
67 which it uses as a storage buffer for the data which is written to the stream.
68 The only constructor which is available specifies the size of the data to come. Thus
69 basic_binostreams can only handle data of a pre-determined size! Any data written to
70 a basic_binostream must be extracted by using basic_binistream!
71*/
72template <class _Ch, class _Tr = std::char_traits<_Ch> >
73#ifdef _MSC_VER
74class basic_binostream : public std::basic_ostream<_Ch, _Tr> {
75#else
76class basic_binostream : public std::basic_ostream {
77#endif
78public:
79 basic_binostream (std::streamsize sz)
80#ifdef _MSC_VER
81 : std::basic_ostream<_Ch,_Tr>(_buf = new basic_binstreambuf<_Ch,_Tr>(sz)) {}
82#else
83 : ostream (_buf = new basic_binstreambuf<_Ch,_Tr>(sz)) {}
84#endif
85 virtual ~basic_binostream () { delete _buf; }
86
87 // popbuf - returns the stored data as a void* pointer and removes it from the
88 // streambuffer object (the streambuffer is destroyed!)
89 void* popbuf (size_t& sz) {
90 sz = _buf->outbufsize();
91 return _buf->givebuf ();
92 }
93
94 // computesize - compute the size of the buffer needed to store an object of
95 // size s.
96 static size_t computesize (size_t s) {
98 }
99
100private:
102};
103
104/* basic_binostream - class definition
105 The basic_binistream class extracts data from a buffer created by basic_binostream.
106 Note that the buffer passed to the constructor must have been created by an
107 instance of basic_binostream, otherwise the results will be unreliable, if not
108 fatal.
109*/
110template <class _Ch, class _Tr = std::char_traits<_Ch> >
111#ifdef _MSC_VER
112class basic_binistream : public std::basic_istream<_Ch, _Tr> {
113#else
114class basic_binistream : public istream {
115#endif
116public:
117 basic_binistream (const _Ch* ptr)
118#ifdef _MSC_VER
119 : std::basic_istream<_Ch,_Tr>(_buf = new basic_binstreambuf<_Ch,_Tr>(ptr)) {}
120#else
121 : basic_istream (_buf = new basic_binstreambuf<_Ch,_Tr>(ptr)) {}
122#endif
123 virtual ~basic_binistream () { delete _buf; }
124
125private:
127};
128
129template <class _Ty, class _Ch, class _Tr>
131 int sz = sizeof(_Ty);
132 o.write ((const _Ch*)(&t), sz);
133 return o;
134}
135
136template <class _Ty, class _Ch, class _Tr>
138 int sz = sizeof(_Ty);
139 i.read ((_Ch*)(&t),sz);
140 return i;
141}
142
143/* The classes binostream, binistream, and binstreambuf define binary stream
144 implementations based upon the char datatype (using byte resolution for allocation).
145*/
149
150#endif
std::ostream & operator<<(std::ostream &out, const SelectInfo &info)
Definition: Coverage.cxx:39
XmlRpcServer s
Definition: HelloServer.cpp:11
basic_binistream< char > binistream
Definition: binarystream.h:147
basic_binstreambuf< char > binstreambuf
Definition: binarystream.h:148
basic_binistream< _Ch, _Tr > & operator>>(basic_binistream< _Ch, _Tr > &i, _Ty &t)
Definition: binarystream.h:137
basic_binostream< char > binostream
Definition: binarystream.h:146
TTree * t
Definition: binning.cxx:23
basic_binistream(const _Ch *ptr)
Definition: binarystream.h:117
virtual ~basic_binistream()
Definition: binarystream.h:123
static size_t computesize(size_t s)
Definition: binarystream.h:96
virtual ~basic_binostream()
Definition: binarystream.h:85
basic_binostream(std::streamsize sz)
Definition: binarystream.h:79
void * popbuf(size_t &sz)
Definition: binarystream.h:89
basic_binstreambuf(const _Ch *p)
Definition: binarystream.h:33
virtual ~basic_binstreambuf()
Definition: binarystream.h:39
size_t outbufsize() const
Definition: binarystream.h:50
static size_t computesize(size_t s)
Definition: binarystream.h:56
basic_binstreambuf(std::streamsize sz)
Definition: binarystream.h:29