| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StreamBuffer.h
Go to the documentation of this file.
1 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/GaudiKernel/StreamBuffer.h,v 1.9 2008/10/27 16:41:33 marcocle Exp $
2 #ifndef GAUDIKERNEL_STREAMBUFFER_H
3 #define GAUDIKERNEL_STREAMBUFFER_H 1
4
5 // STL include files
6 #include <list>
7 #include <vector>
8 #include <string>
9 #include <iostream>
10 #include <cstring>
11 #include <cstdlib>
12 #include <typeinfo>
13
14 #include "GaudiKernel/Kernel.h"
15 #include "GaudiKernel/swab.h"
16
17 // forward declarations
18 class StreamBuffer;
19 class DataObject;
20 class ContainedObject;
21
40 class StreamBuffer /* : public std::string */ {
41 public:
43 class DataIO {
44 public:
46 DataIO() {
47 }
49 virtual ~DataIO() {
50 }
52 void badStreamMode() {
53 throw("Not acceptable stream mode!");
54 }
56 virtual void serialize(StreamBuffer& stream) {
57 if (stream.isReading())
58 load(stream);
59 else if (stream.isWriting())
60 dump(stream);
61 else
62 badStreamMode();
63 }
65 virtual void load(StreamBuffer&) {
66 badStreamMode();
67 }
69 virtual void dump(StreamBuffer&) {
70 badStreamMode();
71 }
72 };
73
75 class Istream : public DataIO {
77 std::istream* m_stream;
78 public:
80 Istream(std::istream& str) : m_stream(&str) {
81 }
83 virtual ~Istream() {
84 }
86 virtual void load(StreamBuffer& stream) {
87 // Generic implementation for istreams:
88 int len;
89 (*m_stream) >> len;
90 stream.erase();
91 stream.reserve(len);
92 m_stream->read(stream.data(),len);
93 }
94 };
96 class Ostream : public DataIO {
97 std::ostream* m_stream;
98 public:
100 Ostream(std::ostream& str) : m_stream(&str) {
101 }
103 virtual ~Ostream() {
104 }
106 virtual void dump(StreamBuffer& stream) {
107 // Generic implementation for ostreams:
108 (*m_stream) << stream.buffPointer();
109 m_stream->write(stream.data(), stream.buffPointer());
110 }
111 };
112 public:
114 enum Mode {UNINITIALIZED, READING, WRITING};
116 enum SwapAction {SINGLE_BYTE, SWAP, NOSWAP};
118 enum State {INVALID=-1, VALID };
120 class ContainedLink {
121 public:
122 ContainedObject* first;
123 long second;
124 long third;
125 ContainedLink() : first(0), second(INVALID), third(INVALID) {
126 }
127 ContainedLink(const ContainedLink& copy)
128 : first(copy.first), second(copy.second), third(copy.third) {
129 }
130 ContainedLink(ContainedObject* pObj, long hint, long link)
131 : first(pObj), second(hint), third(link) {
132 }
133 };
135 class IdentifiedLink {
136 public:
137 DataObject* first;
138 long second;
139 IdentifiedLink() : first(0), second(INVALID) {
140 }
141 IdentifiedLink(const IdentifiedLink& copy)
142 : first(copy.first), second(copy.second) {
143 }
144 IdentifiedLink(DataObject* pObj, long hint)
145 : first(pObj), second(hint) {
146 }
147 };
148
149 typedef std::vector<ContainedLink> ContainedLinks;
151 typedef std::vector<IdentifiedLink> IdentifiedLinks;
153 typedef void (*AnalyzeFunction)(const void* data, int siz, const std::type_info& type);
155 friend class DataObject;
156
157 protected:
159 Mode m_mode;
160
162 long m_pointer;
163
165 long m_length;
166
168 char* m_buffer;
169
171 bool m_swapEnabled;
172
174 ContainedLinks m_containedLinks;
175
177 IdentifiedLinks m_identifiedLinks;
178
180 AnalyzeFunction m_analyzer;
181
183 SwapAction swapBuffer(int siz) const;
184
188 template <class TYPE> StreamBuffer& getObjectPointer(const DataObject* /*pObject*/, TYPE*& refpObject) {
189 IdentifiedLink& link = m_identifiedLinks.back();
190 DataObject* pObj = link.first;
191 m_identifiedLinks.pop_back();
192 refpObject = dynamic_cast<TYPE*>(pObj);
193 return *this;
194 }
198 template <class TYPE> StreamBuffer& getObjectPointer(const ContainedObject* /*pObject*/, TYPE*& refpObject) {
199 ContainedLink& link = m_containedLinks.back();
200 ContainedObject* pObj = link.first;
201 m_containedLinks.pop_back();
202 refpObject = dynamic_cast<TYPE*>(pObj);
203 return *this;
204 }
205 public:
207 StreamBuffer(bool do_swap=true) :
208 m_mode(UNINITIALIZED),
209 m_pointer(0),
210 m_length(0),
211 m_buffer(0),
212 m_swapEnabled(do_swap)
213 {
214 m_analyzer = 0;
215 }
217 virtual ~StreamBuffer() {
218 ::free( m_buffer );
219 }
221 const char* data() const {
222 return m_buffer;
223 }
225 char* data() {
226 return m_buffer;
227 }
229 void erase() {
230 m_pointer = 0;
231 }
233 void reserve(long len) {
234 if ( len > m_length ) {
235 m_length = (len < 16384) ? 16384 : len;
236 m_buffer = (char*)::realloc (m_buffer,m_length);
237 }
238 }
240 void extend(long len) {
241 if ( len + m_pointer > m_length ) {
242 // We have to be a bit generous here in order not to run too often
243 // into ::realloc().
244 long new_len = (m_length < 16384) ? 16384 : 2*m_length;
245 if ( m_length < len ) new_len += len;
246 reserve(new_len);
247 }
248 }
250 long size () const {
251 return m_length;
252 }
254 ContainedLinks& containedLinks() {
255 return m_containedLinks;
256 }
258 const ContainedLinks& containedLinks() const {
259 return m_containedLinks;
260 }
261
263 IdentifiedLinks& identifiedLinks() {
264 return m_identifiedLinks;
265 }
267 const IdentifiedLinks& identifiedLinks() const {
268 return m_identifiedLinks;
269 }
270
272 void setMode(Mode m) {
273 m_mode = m;
274 m_pointer = 0;
275 m_containedLinks.erase (m_containedLinks.begin(), m_containedLinks.end());
276 m_identifiedLinks.erase(m_identifiedLinks.begin(),m_identifiedLinks.end());
277 }
278
280 bool isReading() const {
281 return m_mode == READING;
282 }
283
285 bool isWriting() const {
286 return m_mode == WRITING;
287 }
289 long buffPointer() const {
290 return m_pointer;
291 }
293 void setBuffPointer(long ptr) {
294 m_pointer = ptr;
295 }
297 void setAnalyzer(AnalyzeFunction fun=0) {
298 m_analyzer = fun;
299 }
301 void swapToBuffer(const void* source, int siz);
302
304 void swapFromBuffer(void* target, int siz);
305
307 StreamBuffer& writeBytes (const char* str, long len) {
308 extend( m_pointer+len+4 );
309 *this << len;
310 memcpy(data()+buffPointer(), str, len);
311 m_pointer += len;
312 return *this;
313 }
314
315 void getIdentifiedLink (DataObject*& pObject, long& hint) {
316 IdentifiedLink& l = m_identifiedLinks.back();
317 pObject = l.first;
318 hint = l.second;
319 m_identifiedLinks.pop_back();
320 }
321 void addIdentifiedLink (const DataObject* pObject, long hint) {
322 m_identifiedLinks.push_back( IdentifiedLink((DataObject*)pObject, hint) );
323 }
324
325 void getContainedLink (ContainedObject*& pObject, long& hint, long& link) {
326 ContainedLink& l = m_containedLinks.back();
327 pObject = l.first;
328 hint = l.second;
329 link = l.third;
330 m_containedLinks.pop_back();
331 }
332 void addContainedLink (const ContainedObject* pObject, long hint, long link) {
333 m_containedLinks.push_back( ContainedLink((ContainedObject*)pObject, hint, link) );
334 }
335
336 #ifdef USE_STREAM_ANALYSER
337 #define STREAM_ANALYSE(data, len) if ( 0 != m_analyzer ) m_analyzer(&data, len, typeid(data))
338 #else
339 #define STREAM_ANALYSE(data, len)
340 #endif
341
342 // Implement streamer macros for primivive data types.
343 #define IMPLEMENT_STREAMER(TYPE) \
344 /* Output Streamer */ \
345 StreamBuffer& operator<<(TYPE data) { \
346 swapToBuffer(&data, sizeof(data)); \
347 STREAM_ANALYSE(data, sizeof(data)); \
348 return *this; \
349 } \
350 /* Input Streamer */ \
351 StreamBuffer& operator>>(TYPE & data) { \
352 swapFromBuffer(&data, sizeof(data)); \
353 return *this; \
354 }
355 // RootCint does not understand this macro....
356 // But we can easily live without it!
357 #undef IMPLEMENT_STREAMER
358
360 StreamBuffer& operator<<(longlong data) {
361 swapToBuffer(&data, sizeof(data));
362 STREAM_ANALYSE(data, sizeof(data));
363 return *this;
364 }
366 StreamBuffer& operator>>(longlong & data) {
367 swapFromBuffer(&data, sizeof(data));
368 return *this;
369 }
371 StreamBuffer& operator<<(int data) {
372 swapToBuffer(&data, sizeof(data));
373 STREAM_ANALYSE(data, sizeof(data));
374 return *this;
375 }
377 StreamBuffer& operator>>(int & data) {
378 swapFromBuffer(&data, sizeof(data));
379 return *this;
380 }
382 StreamBuffer& operator<<(unsigned int data) {
383 swapToBuffer(&data, sizeof(data));
384 STREAM_ANALYSE(data, sizeof(data));
385 return *this;
386 }
388 StreamBuffer& operator>>(unsigned int & data) {
389 swapFromBuffer(&data, sizeof(data));
390 return *this;
391 }
393 StreamBuffer& operator<<(long data) {
394 swapToBuffer(&data, sizeof(data));
395 STREAM_ANALYSE(data, sizeof(data));
396 return *this;
397 }
399 StreamBuffer& operator>>(long & data) {
400 swapFromBuffer(&data, sizeof(data));
401 return *this;
402 }
404 StreamBuffer& operator<<(unsigned long data) {
405 swapToBuffer(&data, sizeof(data));
406 STREAM_ANALYSE(data, sizeof(data));
407 return *this;
408 }
410 StreamBuffer& operator>>(unsigned long & data) {
411 swapFromBuffer(&data, sizeof(data));
412 return *this;
413 }
415 StreamBuffer& operator<<(short data) {
416 swapToBuffer(&data, sizeof(data));
417 STREAM_ANALYSE(data, sizeof(data));
418 return *this;
419 }
421 StreamBuffer& operator>>(short & data) {
422 swapFromBuffer(&data, sizeof(data));
423 return *this;
424 }
426 StreamBuffer& operator<<(unsigned short data) {
427 swapToBuffer(&data, sizeof(data));
428 STREAM_ANALYSE(data, sizeof(data));
429 return *this;
430 }
432 StreamBuffer& operator>>(unsigned short & data) {
433 swapFromBuffer(&data, sizeof(data));
434 return *this;
435 }
437 StreamBuffer& operator<<(char data) {
438 swapToBuffer(&data, sizeof(data));
439 STREAM_ANALYSE(data, sizeof(data));
440 return *this;
441 }
443 StreamBuffer& operator>>(char & data) {
444 swapFromBuffer(&data, sizeof(data));
445 return *this;
446 }
448 StreamBuffer& operator<<(unsigned char data) {
449 swapToBuffer(&data, sizeof(data));
450 STREAM_ANALYSE(data, sizeof(data));
451 return *this;
452 }
454 StreamBuffer& operator>>(unsigned char & data) {
455 swapFromBuffer(&data, sizeof(data));
456 return *this;
457 }
459 StreamBuffer& operator<<(float data) {
460 swapToBuffer(&data, sizeof(data));
461 STREAM_ANALYSE(data, sizeof(data));
462 return *this;
463 }
465 StreamBuffer& operator>>(float & data) {
466 swapFromBuffer(&data, sizeof(data));
467 return *this;
468 }
470 StreamBuffer& operator<<(double data) {
471 swapToBuffer(&data, sizeof(data));
472 STREAM_ANALYSE(data, sizeof(data));
473 return *this;
474 }
476 StreamBuffer& operator>>(double & data) {
477 swapFromBuffer(&data, sizeof(data));
478 return *this;
479 }
481 StreamBuffer& operator>>(char* data) {
482 long i, len;
483 *this >> len;
484 for ( i = 0, data[0]=0; i < len; i++ ) {
485 data[i] = m_buffer[m_pointer++];
486 }
487 return *this;
488 }
490 StreamBuffer& operator<<(const char *data) {
491 const char* ptr = 0 == data ? "" : data;
492 int len = strlen(ptr)+1;
493 if ( 0 == m_analyzer )
494 writeBytes(ptr, len);
495 else {
496 STREAM_ANALYSE(data, len);
497 }
498 return *this;
499 }
501 StreamBuffer& operator>>(std::string& data) {
502 long i, len;
503 *this >> len;
504 for ( i = 0, data = ""; i < len; i++ ) {
505 data.append( 1, m_buffer[m_pointer++] );
506 }
507 return *this;
508 }
510 StreamBuffer& operator<<(const std::string& data) {
511 if ( 0 == m_analyzer) {
512 const char* ptr = data.c_str();
513 long len = data.length();
514 writeBytes(ptr, len);
515 }
516 else {
517 STREAM_ANALYSE(data, sizeof(data));
518 }
519 return *this;
520 }
527 template<class TYPE> StreamBuffer& operator>>(TYPE*& refpObject) {
528 return getObjectPointer(refpObject, refpObject);
529 }
530
537 StreamBuffer& operator<<(const ContainedObject* pObject) {
538 STREAM_ANALYSE(pObject, sizeof(pObject));
539 addContainedLink(pObject, INVALID, INVALID);
540 return *this;
541 }
542
549 StreamBuffer& operator<<(const DataObject* pObject) {
550 STREAM_ANALYSE(pObject, sizeof(pObject));
551 addIdentifiedLink(pObject, INVALID);
552 return *this;
553 }
554
561 void serialize(DataIO& ioObject) {
562 ioObject.serialize ( *this );
563 m_pointer = 0;
564 }
565 };
566
567 #undef STREAM_ANALYSE
568
570 inline StreamBuffer::SwapAction StreamBuffer::swapBuffer(int siz) const {
571 switch(siz) {
572 case 1:
573 return SINGLE_BYTE;
574 default:
575 #if defined(__alpha) && !defined(__VMS)
576 // return m_swapEnabled ? SWAP : NOSWAP;
577 return NOSWAP;
578 #elif defined(__sun) && defined(__SVR4) && defined(__i386)
579 // return m_swapEnabled ? SWAP : NOSWAP;
580 return NOSWAP;
581 #elif defined(__APPLE__)
582 // return m_swapEnabled ? SWAP : NOSWAP;
583 return SWAP;
584 #elif defined(__linux) && !defined(__powerpc)
585 // return m_swapEnabled ? SWAP : NOSWAP;
586 return NOSWAP;
587 #elif defined(BORLAND) || defined(_WIN32) || defined(WIN32)
588 // return m_swapEnabled ? SWAP : NOSWAP;
589 return NOSWAP;
590 #else
591 return m_swapEnabled ? SWAP : NOSWAP;
592 // return NOSWAP;
593 #endif
594 }
595 }
596
598 inline void StreamBuffer::swapToBuffer(const void* source, int siz) {
599 char buff[8], *tar, *src = (char*)source;
600 extend (m_pointer+siz);
601 tar = (char*)m_buffer+m_pointer;
602 switch ( swapBuffer(siz) ) {
603 case SINGLE_BYTE:
604 *tar = *src;
605 break;
606 case SWAP:
607 #ifdef __APPLE__
608 for(int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
609 #else
610 ::_swab (src, buff, siz);
611 #endif
612 src = buff;
613 /* no break */
614 case NOSWAP:
615 memcpy(tar, src, siz);
616 break;
617 }
618 m_pointer += siz;
619 }
620
622 inline void StreamBuffer::swapFromBuffer(void* target, int siz) {
623 char* tar = (char*)target;
624 char* src = (char*)m_buffer+m_pointer;
625 switch ( swapBuffer(siz) ) {
626 case SINGLE_BYTE:
627 *tar = *src;
628 break;
629 case SWAP:
630 #ifdef __APPLE__
631 for(int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
632 #else
633 ::_swab (src, tar, siz);
634 #endif
635 break;
636 case NOSWAP:
637 ::memcpy(tar, src, siz);
638 break;
639 }
640 m_pointer += siz;
641 }
642
643 // Output serialize a vector of items
644 template <class T> inline
645 StreamBuffer& operator << (StreamBuffer& s, const std::vector<T>& v) {
646 s << v.size();
647 for ( typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++ ) {
648 s << (*i);
649 }
650 return s;
651 }
652
653 // Input serialize a vector of items
654 template <class T> inline
655 StreamBuffer& operator >> (StreamBuffer& s, std::vector<T>& v) {
656 long i, len;
657 s >> len;
658 v.clear();
659 for ( i = 0; i < len; i++ ) {
660 T temp;
661 s >> temp;
662 v.push_back(temp);
663 }
664 return s;
665 }
666
667 // Output serialize a list of items
668 template <class T> inline
669 StreamBuffer& operator << (StreamBuffer& s, const std::list<T>& l) {
670 s << l.size();
671 for ( typename std::list<T>::const_iterator i = l.begin(); i != l.end(); i++ ) {
672 s << (*i);
673 }
674 return s;
675 }
676
677 // Input serialize a list of items
678 template <class T> inline
679 StreamBuffer& operator >> (StreamBuffer& s, std::list<T>& l) {
680 long i, len;
681 s >> len;
682 l.clear();
683 for ( i = 0; i < len; i++ ) {
684 T temp;
685 s >> temp;
686 l.push_back(temp);
687 }
688 return s;
689 }
690 #endif // GAUDIKERNEL_STREAMBUFFER_H