1 #ifndef GAUDIKERNEL_STREAMBUFFER_H
2 #define GAUDIKERNEL_STREAMBUFFER_H 1
14 #include "GaudiKernel/Kernel.h"
15 #include "GaudiKernel/swab.h"
51 throw(
"Not acceptable stream mode!");
73 class Istream :
public DataIO {
78 Istream(std::istream& str) : m_stream(&str) {
90 m_stream->read(stream.
data(),len);
94 class Ostream :
public DataIO {
98 Ostream(std::ostream& str) : m_stream(&str) {
118 class ContainedLink {
126 : first(copy.first), second(copy.second), third(copy.third) {
129 : first(pObj), second(hint), third(link) {
133 class IdentifiedLink {
140 : first(pObj), second(hint) {
184 IdentifiedLink& link = m_identifiedLinks.back();
186 m_identifiedLinks.pop_back();
187 refpObject =
dynamic_cast<TYPE*
>(pObj);
194 ContainedLink& link = m_containedLinks.back();
196 m_containedLinks.pop_back();
197 refpObject =
dynamic_cast<TYPE*
>(pObj);
203 m_swapEnabled(do_swap) {
210 const char*
data()
const {
223 if ( len > m_length ) {
224 m_length = (len < 16384) ? 16384 : len;
225 m_buffer = (
char*)::realloc (m_buffer,m_length);
230 if ( len + m_pointer > m_length ) {
233 long new_len = (m_length < 16384) ? 16384 : 2*m_length;
234 if ( m_length < len ) new_len += len;
264 m_containedLinks.erase (m_containedLinks.begin(), m_containedLinks.end());
265 m_identifiedLinks.erase(m_identifiedLinks.begin(),m_identifiedLinks.end());
297 extend( m_pointer+len+4 );
305 IdentifiedLink&
l = m_identifiedLinks.back();
308 m_identifiedLinks.pop_back();
311 m_identifiedLinks.push_back( IdentifiedLink((
DataObject*)pObject, hint) );
315 ContainedLink& l = m_containedLinks.back();
319 m_containedLinks.pop_back();
322 m_containedLinks.push_back( ContainedLink((
ContainedObject*)pObject, hint, link) );
325 #ifdef USE_STREAM_ANALYSER
326 #define STREAM_ANALYSE(data, len) if ( 0 != m_analyzer ) m_analyzer(&data, len, typeid(data))
328 #define STREAM_ANALYSE(data, len)
332 #define IMPLEMENT_STREAMER(TYPE) \
334 StreamBuffer& operator<<(TYPE data) { \
335 swapToBuffer(&data, sizeof(data)); \
336 STREAM_ANALYSE(data, sizeof(data)); \
340 StreamBuffer& operator>>(TYPE & data) { \
341 swapFromBuffer(&data, sizeof(data)); \
346 #undef IMPLEMENT_STREAMER
473 for ( i = 0, data[0]=0; i < len; i++ ) {
474 data[
i] = m_buffer[m_pointer++];
480 const char* ptr = 0 == data ?
"" :
data;
481 int len = strlen(ptr)+1;
482 if ( 0 == m_analyzer )
493 for ( i = 0, data =
""; i < len; i++ ) {
494 data.append( 1, m_buffer[m_pointer++] );
500 if ( 0 == m_analyzer) {
501 const char* ptr = data.c_str();
502 long len = data.length();
551 ioObject.serialize ( *
this );
556 #undef STREAM_ANALYSE
564 #if defined(__alpha) && !defined(__VMS)
567 #elif defined(__sun) && defined(__SVR4) && defined(__i386)
570 #elif defined(__APPLE__)
573 #elif defined(__linux) && !defined(__powerpc)
576 #elif defined(BORLAND) || defined(_WIN32) || defined(WIN32)
588 char buff[8], *tar, *src = (
char*)source;
590 tar = (
char*)m_buffer+m_pointer;
597 for(
int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
604 std::copy_n(src,siz,tar);
612 char* tar = (
char*)target;
613 char* src = (
char*)m_buffer+m_pointer;
620 for(
int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
626 std::copy_n(src,siz,tar);
633 template <
class T>
inline
634 StreamBuffer& operator << (StreamBuffer& s, const std::vector<T>& v) {
636 for (
const auto& i : v )
s <<
i;
641 template <
class T>
inline
646 for ( i = 0; i < len; i++ ) {
655 template <
class T>
inline
656 StreamBuffer& operator << (StreamBuffer& s, const std::list<T>&
l) {
658 for (
const auto& i : l ) s <<
i ;
663 template <
class T>
inline
668 for (
long i = 0; i < len; i++ ) {
675 #endif // GAUDIKERNEL_STREAMBUFFER_H
virtual void load(StreamBuffer &)
Template function to load stream data.
StreamBuffer(bool do_swap=true)
Standard constructor.
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.
DataIO()=default
Standard constructor.
The stream buffer is a small object collecting object data.
SwapAction
Data Sawp actions.
ContainedLinks m_containedLinks
Container with links to contained objects.
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.
virtual ~Ostream()=default
Standard Destructor.
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 setAnalyzer(AnalyzeFunction fun=nullptr)
Enable user analysis function.
virtual ~Istream()=default
Destructor.
void setBuffPointer(long ptr)
Retrieve current buffer pointer.
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 addIdentifiedLink(const DataObject *pObject, long hint)
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
virtual ~DataIO()=default
Standard destructor.
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.
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.