stream  0.10.0
stream analysis framework
Buffer.h
1 #ifndef BASE_BUFFER_H
2 #define BASE_BUFFER_H
3 
4 #include "base/TimeStamp.h"
5 
6 namespace base {
7 
10  struct RawDataRec {
11  int refcnt;
12 
13  unsigned kind;
14  unsigned boardid;
15  unsigned format;
16 
17  GlobalTime_t local_tm;
18  GlobalTime_t global_tm;
19 
20  void* buf;
21  unsigned datalen;
22 
23  unsigned user_tag;
24 
26  RawDataRec() : refcnt(0), kind(0), boardid(0), format(0), local_tm(0), global_tm(0), buf(0), datalen(0), user_tag(0) {}
27 
29  void reset()
30  {
31  refcnt = 0;
32  kind = 0;
33  boardid = 0;
34  format = 0;
35  local_tm = 0;
36  global_tm = 0;
37  buf = 0;
38  datalen = 0;
39  user_tag = 0;
40  }
41  };
42 
49  class Buffer {
50  protected:
52  public:
54  Buffer() : fRec(nullptr) {}
56  Buffer(const Buffer& src) : fRec(src.fRec) { if (fRec) fRec->refcnt++; }
58  ~Buffer() { reset(); }
59 
61  Buffer& operator=(const Buffer& src)
62  {
63  reset();
64  fRec = src.fRec;
65  if (fRec) fRec->refcnt++;
66  return *this;
67  }
68 
70  bool null() const { return fRec==nullptr; }
71 
72  void reset();
73 
75  RawDataRec& operator()(void) const { return *fRec; }
76 
78  RawDataRec& rec() const { return *fRec; }
79 
81  unsigned datalen() const { return fRec ? fRec->datalen : 0; }
82 
84  bool setdatalen(unsigned newlen);
85 
87  void* ptr(unsigned shift = 0) const { return fRec ? (shift <= fRec->datalen ? (char*)fRec->buf + shift : 0) : 0; }
88 
90  uint32_t getuint32(unsigned indx) const { return ((uint32_t*) ptr())[indx]; }
91 
94  void makenew(unsigned datalen);
95 
98  void makecopyof(void* buf, unsigned datalen);
99 
103  void makereferenceof(void* buf, unsigned datalen);
104 
105  };
106 
107 }
108 
109 #endif
Memory management class.
Definition: Buffer.h:49
uint32_t getuint32(unsigned indx) const
get uint32_t at given index
Definition: Buffer.h:90
Buffer & operator=(const Buffer &src)
assign operator
Definition: Buffer.h:61
Buffer(const Buffer &src)
constructor
Definition: Buffer.h:56
unsigned datalen() const
Returns length of memory buffer.
Definition: Buffer.h:81
void makecopyof(void *buf, unsigned datalen)
Method produces buffer instance with deep copy of provided raw data Means extra memory will be alloca...
Definition: Buffer.cxx:61
Buffer()
constructor
Definition: Buffer.h:54
RawDataRec & operator()(void) const
access operator
Definition: Buffer.h:75
void reset()
reset buffer
Definition: Buffer.cxx:10
bool setdatalen(unsigned newlen)
Change length of memory buffer - only can be reduced.
Definition: Buffer.cxx:22
~Buffer()
destructor
Definition: Buffer.h:58
RawDataRec & rec() const
access operator
Definition: Buffer.h:78
RawDataRec * fRec
data
Definition: Buffer.h:51
void makereferenceof(void *buf, unsigned datalen)
Method produces buffer instance with reference to provided raw data Means buffer will only contain po...
Definition: Buffer.cxx:88
void makenew(unsigned datalen)
Method produces empty buffer with specified amount of memory.
Definition: Buffer.cxx:37
void * ptr(unsigned shift=0) const
return pointer with shift
Definition: Buffer.h:87
Internal raw data for base::Buffer.
Definition: Buffer.h:10
GlobalTime_t global_tm
buffer head time in global time
Definition: Buffer.h:18
unsigned kind
like ROC event, SPADIC or MBS or ..
Definition: Buffer.h:13
int refcnt
number of references
Definition: Buffer.h:11
unsigned boardid
board id
Definition: Buffer.h:14
unsigned format
raw data format like optic, Ethernet for ROC and so on
Definition: Buffer.h:15
unsigned user_tag
arbitrary data, can be used for any additional data
Definition: Buffer.h:23
unsigned datalen
length of raw data
Definition: Buffer.h:21
RawDataRec()
constructor
Definition: Buffer.h:26
GlobalTime_t local_tm
buffer head time in local scale,
Definition: Buffer.h:17
void reset()
reset
Definition: Buffer.h:29
void * buf
raw data
Definition: Buffer.h:20