2 #ifndef GAUDIKERNEL_STREAMBUFFER_H
3 #define GAUDIKERNEL_STREAMBUFFER_H 1
14 #include "GaudiKernel/Kernel.h"
15 #include "GaudiKernel/swab.h"
53 throw(
"Not acceptable stream mode!");
75 class Istream :
public DataIO {
80 Istream(std::istream& str) : m_stream(&str) {
92 m_stream->read(stream.
data(),len);
96 class Ostream :
public DataIO {
100 Ostream(std::ostream& str) : m_stream(&str) {
120 class ContainedLink {
128 : first(copy.first), second(copy.second), third(copy.third) {
131 : first(pObj), second(hint), third(link) {
135 class IdentifiedLink {
142 : first(copy.first), second(copy.second) {
145 : first(pObj), second(hint) {
189 IdentifiedLink& link = m_identifiedLinks.back();
191 m_identifiedLinks.pop_back();
192 refpObject =
dynamic_cast<TYPE*
>(pObj);
199 ContainedLink& link = m_containedLinks.back();
201 m_containedLinks.pop_back();
202 refpObject =
dynamic_cast<TYPE*
>(pObj);
212 m_swapEnabled(do_swap)
221 const char*
data()
const {
234 if ( len > m_length ) {
235 m_length = (len < 16384) ? 16384 : len;
236 m_buffer = (
char*)::realloc (m_buffer,m_length);
241 if ( len + m_pointer > m_length ) {
244 long new_len = (m_length < 16384) ? 16384 : 2*m_length;
245 if ( m_length < len ) new_len += len;
275 m_containedLinks.erase (m_containedLinks.begin(), m_containedLinks.end());
276 m_identifiedLinks.erase(m_identifiedLinks.begin(),m_identifiedLinks.end());
308 extend( m_pointer+len+4 );
316 IdentifiedLink&
l = m_identifiedLinks.back();
319 m_identifiedLinks.pop_back();
322 m_identifiedLinks.push_back( IdentifiedLink((
DataObject*)pObject, hint) );
326 ContainedLink& l = m_containedLinks.back();
330 m_containedLinks.pop_back();
333 m_containedLinks.push_back( ContainedLink((
ContainedObject*)pObject, hint, link) );
336 #ifdef USE_STREAM_ANALYSER
337 #define STREAM_ANALYSE(data, len) if ( 0 != m_analyzer ) m_analyzer(&data, len, typeid(data))
339 #define STREAM_ANALYSE(data, len)
343 #define IMPLEMENT_STREAMER(TYPE) \
345 StreamBuffer& operator<<(TYPE data) { \
346 swapToBuffer(&data, sizeof(data)); \
347 STREAM_ANALYSE(data, sizeof(data)); \
351 StreamBuffer& operator>>(TYPE & data) { \
352 swapFromBuffer(&data, sizeof(data)); \
357 #undef IMPLEMENT_STREAMER
484 for ( i = 0, data[0]=0; i < len; i++ ) {
485 data[
i] = m_buffer[m_pointer++];
491 const char* ptr = 0 == data ?
"" :
data;
492 int len = strlen(ptr)+1;
493 if ( 0 == m_analyzer )
504 for ( i = 0, data =
""; i < len; i++ ) {
505 data.append( 1, m_buffer[m_pointer++] );
511 if ( 0 == m_analyzer) {
512 const char* ptr = data.c_str();
513 long len = data.length();
562 ioObject.serialize ( *
this );
567 #undef STREAM_ANALYSE
575 #if defined(__alpha) && !defined(__VMS)
578 #elif defined(__sun) && defined(__SVR4) && defined(__i386)
581 #elif defined(__APPLE__)
584 #elif defined(__linux) && !defined(__powerpc)
587 #elif defined(BORLAND) || defined(_WIN32) || defined(WIN32)
599 char buff[8], *tar, *src = (
char*)source;
601 tar = (
char*)m_buffer+m_pointer;
608 for(
int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
615 memcpy(tar, src, siz);
623 char* tar = (
char*)target;
624 char* src = (
char*)m_buffer+m_pointer;
631 for(
int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
637 ::memcpy(tar, src, siz);
644 template <
class T>
inline
645 StreamBuffer& operator << (StreamBuffer& s, const std::vector<T>& v) {
647 for (
typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++ ) {
654 template <
class T>
inline
659 for ( i = 0; i < len; i++ ) {
668 template <
class T>
inline
669 StreamBuffer& operator << (StreamBuffer& s, const std::list<T>&
l) {
671 for (
typename std::list<T>::const_iterator i = l.begin(); i != l.end(); i++ ) {
678 template <
class T>
inline
683 for ( i = 0; i < len; i++ ) {
690 #endif // GAUDIKERNEL_STREAMBUFFER_H
virtual void load(StreamBuffer &)
Template function to load stream data.
StreamBuffer(bool do_swap=true)
Standard constructor.
virtual ~DataIO()
Standard destructor.
virtual void serialize(StreamBuffer &stream)
Serialization method: loads/dumps streambuffer content.
#define _swab(source, target, radix)
std::istream * m_stream
Reference to input stream.
long buffPointer() const
Retrieve current buffer pointer.
Ostream(std::ostream &str)
Standard constructor: pass reference to stream object.
The stream buffer is a small object collecting object data.
SwapAction
Data Sawp actions.
ContainedLinks m_containedLinks
Container with links to contained objects.
virtual ~Istream()
Destructor.
Istream(std::istream &str)
Constructor.
AnalyzeFunction m_analyzer
Hook function for analysis of data to the stream.
virtual void dump(StreamBuffer &)
Template function to save stream data.
long m_length
Total buffer length.
IdentifiedLinks & identifiedLinks()
Access to identified links.
void extend(long len)
Extend the buffer.
void swapToBuffer(const void *source, int siz)
Swap buffers: int, long, short, float and double.
bool isReading() const
Get stream buffer state.
std::vector< ContainedLink > ContainedLinks
virtual void load(StreamBuffer &stream)
Data load method.
const char * data() const
Read access to data buffer.
void setBuffPointer(long ptr)
Retrieve current buffer pointer.
virtual ~Ostream()
Standard Destructor.
StreamBuffer & operator>>(longlong &data)
Input Streamer.
void reserve(long len)
Reserve buffer space; Default: 16 k buffer size.
Mode m_mode
Boolean indicating wether the stream is in read or write mode.
ContainedLinks & containedLinks()
Access to contained links.
StreamBuffer & operator<<(longlong data)
Output Streamer.
void badStreamMode()
Throw Exception.
void setAnalyzer(AnalyzeFunction fun=0)
Enable user analysis function.
void addIdentifiedLink(const DataObject *pObject, long hint)
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
char * m_buffer
Pointer to heap buffer.
bool isWriting() const
Get stream buffer state.
long m_pointer
Current buffer pointer.
double fun(const std::vector< double > &x)
#define STREAM_ANALYSE(data, len)
State
Link state defintions.
DataIO()
Standard constructor.
StreamBuffer & writeBytes(const char *str, long len)
Write string to output stream.
virtual ~StreamBuffer()
Standard destructor.
virtual void dump(StreamBuffer &stream)
Output dumper.
void erase()
Reset the buffer.
void(* AnalyzeFunction)(const void *data, int siz, const std::type_info &type)
Definition of the buffer analyzer.
StreamBuffer & operator>>(StreamBuffer &s, std::vector< T > &v)
void setMode(Mode m)
Set mode of the stream and allocate buffer.
StreamBuffer & getObjectPointer(const DataObject *, TYPE *&refpObject)
Helper to distinguish between identified pointers and contained pointers.
IdentifiedLinks m_identifiedLinks
Container with links to contained objects.
A DataObject is the base class of any identifiable object on any data store.
SwapAction swapBuffer(int siz) const
Check for byte swapping.
void addContainedLink(const ContainedObject *pObject, long hint, long link)
void getContainedLink(ContainedObject *&pObject, long &hint, long &link)
long size() const
Total buffer size.
std::vector< IdentifiedLink > IdentifiedLinks
Definition of the identifiable link set.
void serialize(DataIO &ioObject)
Serialize the buffer using an IO object.
bool m_swapEnabled
Flag indicating swapping.
void getIdentifiedLink(DataObject *&pObject, long &hint)
void swapFromBuffer(void *target, int siz)
Swap buffers: int, long, short, float and double.