ATLAS Offline Software
DbBlob.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Header: /cvs/PF/pool/StorageSvc/StorageSvc/DbBlob.h,v 1.14 2010/08/19 14:02:41 trentad Exp $
6 #ifndef POOL_STORAGESVC_DBBLOB_H
7 #define POOL_STORAGESVC_DBBLOB_H 1
8 
9 // STL include files
10 #include <list>
11 #include <vector>
12 #include <string>
13 #include <iostream>
14 #include <cstring>
15 #include <typeinfo>
16 
17 #if !(defined(WIN32) || defined(_WIN32))
18 #include <unistd.h>
19 #define _swab(source, target, radix) swab(source, target, radix)
20 #endif /* WIN32 */
21 
22 
23 /*
24  * POOL namespace declaration
25  */
26 namespace pool {
27 
47  class DbBlob {
48  public:
54  enum State {INVALID=-1, VALID };
56  typedef void (*Analyzer)(const void* data, int siz, const std::type_info& type);
57 
58  protected:
62  int m_pointer;
64  int m_length;
66  char* m_buffer;
71 
73  SwapAction swapBuffer(int siz) const;
74 
75  public:
77  DbBlob(bool do_swap=true) :
79  m_pointer(0),
80  m_length(0),
81  m_buffer(0),
82  m_swapEnabled(do_swap) { m_analyzer = 0; }
84  virtual ~DbBlob();
85 
86  DbBlob (const DbBlob&) = delete;
87  DbBlob& operator= (const DbBlob&) = delete;
88 
90  const char* data() const { return m_buffer; }
92  char* data() { return m_buffer; }
94  void erase() { m_pointer = 0; }
96  int size () const { return m_length; }
98  bool isReading() const { return m_mode == READING;}
99 
101  bool isWriting() const { return m_mode == WRITING;}
103  int buffPointer() const { return m_pointer; }
105  void setBuffPointer(int ptr) { m_pointer = ptr; }
109  inline void swapToBuffer(const void* source, int siz);
111  inline void swapFromBuffer(void* target, int siz);
113  void adopt(char* memory, int len);
115  void reserve(int len);
117  void extend(int len) {
118  if ( len + m_pointer > m_length ) {
119  // We have to be a bit generous here in order not to run too often
120  // into ::realloc().
121  int new_len = (m_length < 16384) ? 16384 : 2*m_length;
122  if ( m_length < len ) new_len += len;
123  reserve(new_len);
124  }
125  }
127  void setMode(Mode m) {
128  m_mode = m;
129  m_pointer = 0;
130  }
132  DbBlob& writeBytes (const char* str, int len);
133 
134  #ifdef USE_STREAM_ANALYSER
135  #define STREAM_ANALYSE(data, len) if ( 0 != m_analyzer ) m_analyzer(&data, len, typeid(data))
136  #else
137  #define STREAM_ANALYSE(data, len)
138  #endif
139 
141  DbBlob& operator<<(long long int data) {
142  swapToBuffer(&data, sizeof(data));
143  STREAM_ANALYSE(data, sizeof(data));
144  return *this;
145  }
147  DbBlob& operator>>(long long int & data) {
148  swapFromBuffer(&data, sizeof(data));
149  return *this;
150  }
153  swapToBuffer(&data, sizeof(data));
154  STREAM_ANALYSE(data, sizeof(data));
155  return *this;
156  }
158  DbBlob& operator>>(int & data) {
159  swapFromBuffer(&data, sizeof(data));
160  return *this;
161  }
163  DbBlob& operator<<(unsigned int data) {
164  swapToBuffer(&data, sizeof(data));
165  STREAM_ANALYSE(data, sizeof(data));
166  return *this;
167  }
169  DbBlob& operator>>(unsigned int & data) {
170  swapFromBuffer(&data, sizeof(data));
171  return *this;
172  }
175  swapToBuffer(&data, sizeof(data));
176  STREAM_ANALYSE(data, sizeof(data));
177  return *this;
178  }
180  DbBlob& operator>>(long & data) {
181  swapFromBuffer(&data, sizeof(data));
182  return *this;
183  }
185  DbBlob& operator<<(unsigned long data) {
186  swapToBuffer(&data, sizeof(data));
187  STREAM_ANALYSE(data, sizeof(data));
188  return *this;
189  }
191  DbBlob& operator>>(unsigned long & data) {
192  swapFromBuffer(&data, sizeof(data));
193  return *this;
194  }
196  DbBlob& operator<<(short data) {
197  swapToBuffer(&data, sizeof(data));
198  STREAM_ANALYSE(data, sizeof(data));
199  return *this;
200  }
202  DbBlob& operator>>(short & data) {
203  swapFromBuffer(&data, sizeof(data));
204  return *this;
205  }
207  DbBlob& operator<<(unsigned short data) {
208  swapToBuffer(&data, sizeof(data));
209  STREAM_ANALYSE(data, sizeof(data));
210  return *this;
211  }
213  DbBlob& operator>>(unsigned short & data) {
214  swapFromBuffer(&data, sizeof(data));
215  return *this;
216  }
219  swapToBuffer(&data, sizeof(data));
220  STREAM_ANALYSE(data, sizeof(data));
221  return *this;
222  }
224  DbBlob& operator>>(char & data) {
225  swapFromBuffer(&data, sizeof(data));
226  return *this;
227  }
229  DbBlob& operator<<(unsigned char data) {
230  swapToBuffer(&data, sizeof(data));
231  STREAM_ANALYSE(data, sizeof(data));
232  return *this;
233  }
235  DbBlob& operator>>(unsigned char & data) {
236  swapFromBuffer(&data, sizeof(data));
237  return *this;
238  }
240  DbBlob& operator<<(float data) {
241  swapToBuffer(&data, sizeof(data));
242  STREAM_ANALYSE(data, sizeof(data));
243  return *this;
244  }
246  DbBlob& operator>>(float & data) {
247  swapFromBuffer(&data, sizeof(data));
248  return *this;
249  }
251  DbBlob& operator<<(double data) {
252  swapToBuffer(&data, sizeof(data));
253  STREAM_ANALYSE(data, sizeof(data));
254  return *this;
255  }
257  DbBlob& operator>>(double & data) {
258  swapFromBuffer(&data, sizeof(data));
259  return *this;
260  }
264  DbBlob& operator<<(const char *data);
266  DbBlob& operator>>(std::string& data);
268  DbBlob& operator<<(const std::string& data);
269  };
270  #undef STREAM_ANALYSE
271 
273  inline DbBlob::SwapAction DbBlob::swapBuffer(int siz) const {
274  switch(siz) {
275  case 1:
276  return SINGLE_BYTE;
277  default:
278  #if defined(__alpha) && !defined(__VMS)
279  return NOSWAP;
280  #elif defined(__sun) && defined(__SVR4) && defined(__i386)
281  return NOSWAP;
282  #elif defined(__linux) && !defined(__powerpc)
283  return NOSWAP;
284  #elif defined(BORLAND) || defined(_WIN32) || defined(WIN32)
285  return NOSWAP;
286  #elif defined(__APPLE__) && defined(__i386__)
287  return NOSWAP;
288  #elif defined(__APPLE__)
289  return SWAP;
290  #else
291  return m_swapEnabled ? SWAP : NOSWAP;
292  #endif
293  }
294  }
295 
297  inline void DbBlob::swapToBuffer(const void* source, int siz) {
298  char buff[8], *tar;
299  const char* src = reinterpret_cast<const char*> (source);
300  extend (m_pointer+siz);
301  tar = (char*)m_buffer+m_pointer;
302  switch ( swapBuffer(siz) ) {
303  case SINGLE_BYTE:
304  *tar = *src;
305  break;
306  case SWAP:
307 #ifdef __APPLE__
308  for(int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
309 #else
310  ::_swab (src, buff, siz);
311 #endif
312  src = buff;
313  // Fall through.
314  case NOSWAP:
315  memcpy(tar, src, siz);
316  break;
317  }
318  m_pointer += siz;
319  }
320 
322  inline void DbBlob::swapFromBuffer(void* target, int siz) {
323  char* tar = (char*)target;
324  char* src = (char*)m_buffer+m_pointer;
325  switch ( swapBuffer(siz) ) {
326  case SINGLE_BYTE:
327  *tar = *src;
328  break;
329  case SWAP:
330 #ifdef __APPLE__
331  for(int i = 0,j = siz-1;i<siz;i++,j--) tar[j] = src[i];
332 #else
333  ::_swab (src, tar, siz);
334 #endif
335  break;
336  case NOSWAP:
337  ::memcpy(tar, src, siz);
338  break;
339  }
340  m_pointer += siz;
341  }
342 
343  // Output serialize a vector of items
344  template <class T> inline
345  DbBlob& operator << (DbBlob& s, const std::vector<T>& v) {
346  int len = (int)v.size();
347  s << len;
348  for ( typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i ) {
349  s << (*i);
350  }
351  return s;
352  }
353 
354  // Input serialize a vector of items
355  template <class T> inline
356  DbBlob& operator >> (DbBlob& s, std::vector<T>& v) {
357  int i, len;
358  s >> len;
359  v.clear();
360  for ( i = 0; i < len; i++ ) {
361  T temp;
362  s >> temp;
363  v.push_back(temp);
364  }
365  return s;
366  }
367 
368  // Output serialize a list of items
369  template <class T> inline
370  DbBlob& operator << (DbBlob& s, const std::list<T>& l) {
371  int len = (int)l.size();
372  s << len;
373  for ( typename std::list<T>::const_iterator i = l.begin(); i != l.end(); ++i ) {
374  s << (*i);
375  }
376  return s;
377  }
378 
379  // Input serialize a list of items
380  template <class T> inline
381  DbBlob& operator >> (DbBlob& s, std::list<T>& l) {
382  int i, len;
383  s >> len;
384  l.clear();
385  for ( i = 0; i < len; i++ ) {
386  T temp;
387  s >> temp;
388  l.push_back(temp);
389  }
390  return s;
391  }
392 
393 } // End namespace pool
394 #endif // POOL_STORAGESVC_DBBLOB_H
pool::DbBlob::operator>>
DbBlob & operator>>(short &data)
Input Streamer
Definition: DbBlob.h:202
pool::DbBlob::operator>>
DbBlob & operator>>(long long int &data)
Input Streamer
Definition: DbBlob.h:147
get_hdefs.buff
buff
Definition: get_hdefs.py:61
generateReferenceFile.fun
fun
Definition: generateReferenceFile.py:18
pool::DbBlob::m_analyzer
Analyzer m_analyzer
Hook function for analysis of data to the stream.
Definition: DbBlob.h:70
pool::DbBlob::operator<<
DbBlob & operator<<(double data)
Output Streamer
Definition: DbBlob.h:251
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
pool::DbBlob::operator<<
DbBlob & operator<<(short data)
Output Streamer
Definition: DbBlob.h:196
pool::DbBlob::buffPointer
int buffPointer() const
Retrieve current buffer pointer.
Definition: DbBlob.h:103
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
pool::DbBlob::setAnalyzer
void setAnalyzer(Analyzer fun=0)
Enable user analysis function.
Definition: DbBlob.h:107
pool::DbBlob::Mode
Mode
Streamer mode.
Definition: DbBlob.h:50
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
pool::DbBlob::operator<<
DbBlob & operator<<(unsigned short data)
Output Streamer
Definition: DbBlob.h:207
pool::DbBlob::DbBlob
DbBlob(bool do_swap=true)
Standard constructor.
Definition: DbBlob.h:77
WriteCellNoiseToCool.src
src
Definition: WriteCellNoiseToCool.py:513
pool::DbBlob::operator<<
DbBlob & operator<<(const std::string &data)
Streamer to write strings in (std::string) format.
pool::DbBlob::operator<<
DbBlob & operator<<(const char *data)
Streamer to write strings in (char*) format.
pool::DbBlob::m_buffer
char * m_buffer
Pointer to heap buffer.
Definition: DbBlob.h:66
pool::DbBlob::swapFromBuffer
void swapFromBuffer(void *target, int siz)
Swap buffers: int, long, short, float and double.
Definition: DbBlob.h:322
pool
pool namespace
Definition: libname.h:15
pool::DbBlob::operator>>
DbBlob & operator>>(long &data)
Input Streamer
Definition: DbBlob.h:180
pool::DbBlob::isWriting
bool isWriting() const
Get stream buffer state.
Definition: DbBlob.h:101
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
pool::DbBlob::operator>>
DbBlob & operator>>(double &data)
Input Streamer
Definition: DbBlob.h:257
pool::DbBlob::SINGLE_BYTE
@ SINGLE_BYTE
Definition: DbBlob.h:52
pool::DbBlob::adopt
void adopt(char *memory, int len)
Adopt an external memory buffer.
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
pool::DbBlob::setBuffPointer
void setBuffPointer(int ptr)
Retrieve current buffer pointer.
Definition: DbBlob.h:105
pool::DbBlob::m_swapEnabled
bool m_swapEnabled
Flag indicating swapping.
Definition: DbBlob.h:68
pool::DbBlob::operator<<
DbBlob & operator<<(long long int data)
Output Streamer
Definition: DbBlob.h:141
pool::DbBlob::~DbBlob
virtual ~DbBlob()
Standard destructor.
pool::DbBlob::operator<<
DbBlob & operator<<(unsigned long data)
Output Streamer
Definition: DbBlob.h:185
pool::DbBlob::INVALID
@ INVALID
Definition: DbBlob.h:54
STREAM_ANALYSE
#define STREAM_ANALYSE(data, len)
Definition: DbBlob.h:137
pool::DbBlob::operator>>
DbBlob & operator>>(char &data)
Input Streamer
Definition: DbBlob.h:224
pool::DbBlob::operator<<
DbBlob & operator<<(int data)
Output Streamer
Definition: DbBlob.h:152
pool::DbBlob::extend
void extend(int len)
Extend the buffer.
Definition: DbBlob.h:117
pool::DbBlob::setMode
void setMode(Mode m)
Set mode of the stream and allocate buffer.
Definition: DbBlob.h:127
pool::DbBlob::reserve
void reserve(int len)
Reserve buffer space; Default: 16 k buffer size.
python.utils.AtlRunQueryMemUtil.memory
def memory(since=0.0)
Definition: AtlRunQueryMemUtil.py:30
pool::DbBlob::DbBlob
DbBlob(const DbBlob &)=delete
pool::DbBlob::READING
@ READING
Definition: DbBlob.h:50
lumiFormat.i
int i
Definition: lumiFormat.py:85
pool::DbBlob::VALID
@ VALID
Definition: DbBlob.h:54
pool::DbBlob::operator>>
DbBlob & operator>>(std::string &data)
Streamer to read strings in (std::string) format.
pool::DbBlob::m_mode
Mode m_mode
Boolean indicating wether the stream is in read or write mode.
Definition: DbBlob.h:60
pool::DbBlob::operator<<
DbBlob & operator<<(char data)
Output Streamer
Definition: DbBlob.h:218
pool::DbBlob::isReading
bool isReading() const
Get stream buffer state.
Definition: DbBlob.h:98
pool::DbBlob::data
char * data()
write access to data buffer
Definition: DbBlob.h:92
pool::DbBlob::swapBuffer
SwapAction swapBuffer(int siz) const
Check for byte swapping.
Definition: DbBlob.h:273
pool::DbBlob::writeBytes
DbBlob & writeBytes(const char *str, int len)
Write string to output stream.
pool::DbBlob::State
State
Link state defintions.
Definition: DbBlob.h:54
perfmonmt-printer.tar
tar
Definition: perfmonmt-printer.py:203
pool::DbBlob::operator>>
DbBlob & operator>>(char *data)
Streamer to read strings in (char*) format.
pool::DbBlob::operator=
DbBlob & operator=(const DbBlob &)=delete
pool::operator<<
DbBlob & operator<<(DbBlob &s, const std::vector< T > &v)
Definition: DbBlob.h:345
pool::DbBlob::operator<<
DbBlob & operator<<(unsigned int data)
Output Streamer
Definition: DbBlob.h:163
pool::DbBlob::data
const char * data() const
Read access to data buffer.
Definition: DbBlob.h:90
pool::DbBlob::operator>>
DbBlob & operator>>(float &data)
Input Streamer
Definition: DbBlob.h:246
pool::DbBlob::operator>>
DbBlob & operator>>(int &data)
Input Streamer
Definition: DbBlob.h:158
pool::DbBlob::WRITING
@ WRITING
Definition: DbBlob.h:50
python.PyAthena.v
v
Definition: PyAthena.py:154
pool::DbBlob::Analyzer
void(* Analyzer)(const void *data, int siz, const std::type_info &type)
Definition of the buffer analyzer.
Definition: DbBlob.h:56
pool::operator>>
DbBlob & operator>>(DbBlob &s, std::vector< T > &v)
Definition: DbBlob.h:356
pool::DbBlob::operator>>
DbBlob & operator>>(unsigned long &data)
Input Streamer
Definition: DbBlob.h:191
pool::DbBlob::SwapAction
SwapAction
Data Sawp actions.
Definition: DbBlob.h:52
copySelective.target
string target
Definition: copySelective.py:37
pool::DbBlob::operator<<
DbBlob & operator<<(float data)
Output Streamer
Definition: DbBlob.h:240
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
pool::DbBlob::operator>>
DbBlob & operator>>(unsigned char &data)
Input Streamer
Definition: DbBlob.h:235
pool::DbBlob::operator<<
DbBlob & operator<<(long data)
Output Streamer
Definition: DbBlob.h:174
pool::DbBlob::operator<<
DbBlob & operator<<(unsigned char data)
Output Streamer
Definition: DbBlob.h:229
copySelective.source
string source
Definition: copySelective.py:32
pool::DbBlob::size
int size() const
Total buffer size.
Definition: DbBlob.h:96
str
Definition: BTagTrackIpAccessor.cxx:11
pool::DbBlob::NOSWAP
@ NOSWAP
Definition: DbBlob.h:52
pool::DbBlob::m_length
int m_length
Total buffer length.
Definition: DbBlob.h:64
pool::DbBlob::SWAP
@ SWAP
Definition: DbBlob.h:52
_swab
#define _swab(source, target, radix)
Definition: DbBlob.h:19
pool::DbBlob::erase
void erase()
Reset the buffer.
Definition: DbBlob.h:94
pool::DbBlob
Definition: DbBlob.h:47
pool::DbBlob::operator>>
DbBlob & operator>>(unsigned short &data)
Input Streamer
Definition: DbBlob.h:213
pool::DbBlob::swapToBuffer
void swapToBuffer(const void *source, int siz)
Swap buffers: int, long, short, float and double.
Definition: DbBlob.h:297
pool::DbBlob::operator>>
DbBlob & operator>>(unsigned int &data)
Input Streamer
Definition: DbBlob.h:169
pool::DbBlob::m_pointer
int m_pointer
Current buffer pointer.
Definition: DbBlob.h:62
pool::DbBlob::UNINITIALIZED
@ UNINITIALIZED
Definition: DbBlob.h:50