DABC (Data Acquisition Backbone Core)  2.9.9
Pointer.h
Go to the documentation of this file.
1 // $Id: Pointer.h 4506 2020-05-26 07:55:57Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Pointer
17 #define DABC_Pointer
18 
19 #ifndef DABC_Buffer
20 #include "dabc/Buffer.h"
21 #endif
22 
23 namespace dabc {
24 
34  class Pointer {
35 
36  friend class Buffer;
37 
38  protected:
40  unsigned fSegm;
41  unsigned char* fPtr;
44 
45  void long_shift(BufferSize_t sz);
46 
47  public:
48  inline Pointer() :
49  fBuf(),
50  fSegm(0),
51  fPtr(nullptr),
52  fRawSize(0),
53  fFullSize(0)
54  {
55  }
56 
57  inline Pointer(const void* buf, BufferSize_t sz) :
58  fBuf(),
59  fSegm(0),
60  fPtr((unsigned char*) buf),
61  fRawSize(sz),
62  fFullSize(sz)
63  {
64  }
65 
66  inline Pointer(const Pointer& src, BufferSize_t pos = 0, BufferSize_t fullsz = 0) :
67  fBuf(src.fBuf),
68  fSegm(src.fSegm),
69  fPtr(src.fPtr),
70  fRawSize(src.fRawSize),
71  fFullSize(src.fFullSize)
72  {
73  if (pos>0) shift(pos);
74  if (fullsz>0) setfullsize(fullsz);
75  }
76 
77  inline Pointer(const Buffer& buf, unsigned pos = 0, unsigned len = 0) :
78  fBuf(),
79  fSegm(0),
80  fPtr(0),
81  fRawSize(0),
82  fFullSize(0)
83  {
84  reset(buf, pos, len);
85  }
86 
87  inline Pointer& operator=(const Pointer& src)
88  {
89  reset(src);
90  return *this;
91  }
92 
93  inline Pointer& operator=(const Buffer& src)
94  {
95  reset(src);
96  return *this;
97  }
98 
99  inline void reset(void* buf, BufferSize_t sz)
100  {
101  fBuf.Release();
102  fSegm = 0;
103  fPtr = (unsigned char*) buf;
104  fRawSize = sz;
105  fFullSize = sz;
106  }
107 
108  inline void reset(const Pointer& src, BufferSize_t pos = 0, BufferSize_t fullsz = 0)
109  {
110  fBuf = src.fBuf;
111  fSegm = src.fSegm;
112  fPtr = src.fPtr;
113  fRawSize = src.fRawSize;
114  fFullSize = src.fFullSize;
115  if (pos>0) shift(pos);
116  if (fullsz>0) setfullsize(fullsz);
117  }
118 
119  inline void reset(const Buffer& src, BufferSize_t pos = 0, BufferSize_t fullsz = 0)
120  {
121  fSegm = 0;
122  fBuf = src;
124 
125  if (fFullSize>0) {
126  fPtr = (unsigned char*) fBuf.SegmentPtr(0);
128  } else {
129  fPtr = 0;
130  fRawSize = 0;
131  }
132  if (pos>0) shift(pos);
133  if (fullsz>0) setfullsize(fullsz);
134  }
135 
136  inline void reset()
137  {
138  fBuf.Release();
139  fSegm = 0;
140  fPtr = 0;
141  fRawSize = 0;
142  fFullSize = 0;
143  }
144 
145  inline void* operator()() const { return fPtr; }
146  inline void* ptr() const { return fPtr; }
147  inline bool null() const { return (fPtr==0) || (fullsize()==0); }
148  inline BufferSize_t rawsize() const { return fRawSize; }
149  inline BufferSize_t fullsize() const { return fFullSize; }
150 
151  inline Buffer buf() { return fBuf; }
152 
153  inline BufferSize_t shift(BufferSize_t sz) throw()
154  {
155  if (sz<fRawSize) {
156  fPtr += sz;
157  fRawSize -= sz;
158  fFullSize -= sz;
159  } else
160  if (sz>fFullSize) {
161  sz = fFullSize;
162  reset();
163  } else
164  long_shift(sz);
165  return sz;
166  }
167 
169  {
170  if (fFullSize>sz) {
171  fFullSize = sz;
172  if (fRawSize>sz) fRawSize = sz;
173  }
174  }
175 
176  inline void operator+=(BufferSize_t sz) throw() { shift(sz); }
177 
178  BufferSize_t copyto(void* tgt, BufferSize_t sz) const throw();
179 
180  inline BufferSize_t copyto_shift(void* tgt, BufferSize_t sz) throw()
181  { return shift(copyto(tgt,sz)); }
182 
183  BufferSize_t copyto(Pointer& src, BufferSize_t sz) const throw();
184 
186  { return shift(copyto(src, sz)); }
187 
189  BufferSize_t copyfrom(const Pointer& src, BufferSize_t sz = 0) throw();
190 
192  inline BufferSize_t copyfrom_shift(const Pointer& src, BufferSize_t sz = 0) throw()
193  { return shift(copyfrom(src, sz)); }
194 
195  BufferSize_t copyfrom(const void *src, BufferSize_t sz);
196 
197  inline BufferSize_t copyfrom_shift(const void* src, BufferSize_t sz) throw()
198  { return shift(copyfrom(src, sz)); }
199 
200  BufferSize_t copyfromstr(const char* str, unsigned len = 0) throw();
201 
202  int distance_to(const Pointer& child) const;
203 
204  int distance_to_ownbuf() const { return fBuf.null() ? 0 : Pointer(fBuf).distance_to(*this); }
205 
207  float consumed_size() const;
208 
209  bool is_same_buf(const Pointer& ptr) const { return fBuf == ptr.fBuf; }
210 
211  unsigned segmid() const { return fSegm; }
212  };
213 }
214 
215 
216 #endif
Reference on memory from memory pool.
Definition: Buffer.h:135
unsigned SegmentSize(unsigned n=0) const
Returns size on the segment, no any boundary checks.
Definition: Buffer.h:174
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Definition: Buffer.h:171
Manipulator with dabc::Buffer class.
Definition: Pointer.h:34
BufferSize_t shift(BufferSize_t sz)
Definition: Pointer.h:153
unsigned segmid() const
Definition: Pointer.h:211
Pointer(const Pointer &src, BufferSize_t pos=0, BufferSize_t fullsz=0)
Definition: Pointer.h:66
void reset(void *buf, BufferSize_t sz)
Definition: Pointer.h:99
BufferSize_t fRawSize
size of contiguous memory, pointed by fPtr
Definition: Pointer.h:42
Pointer & operator=(const Pointer &src)
Definition: Pointer.h:87
int distance_to(const Pointer &child) const
Definition: Pointer.cxx:134
bool is_same_buf(const Pointer &ptr) const
Definition: Pointer.h:209
void setfullsize(BufferSize_t sz)
Definition: Pointer.h:168
unsigned char * fPtr
pointer on the raw buffer
Definition: Pointer.h:41
BufferSize_t copyfrom(const Pointer &src, BufferSize_t sz=0)
Returns actual size copied.
Definition: Pointer.cxx:54
BufferSize_t copyto_shift(Pointer &src, BufferSize_t sz)
Definition: Pointer.h:185
BufferSize_t copyto(void *tgt, BufferSize_t sz) const
Definition: Pointer.cxx:98
unsigned fSegm
segment id
Definition: Pointer.h:40
void operator+=(BufferSize_t sz)
Definition: Pointer.h:176
Pointer(const Buffer &buf, unsigned pos=0, unsigned len=0)
Definition: Pointer.h:77
int distance_to_ownbuf() const
Definition: Pointer.h:204
Pointer & operator=(const Buffer &src)
Definition: Pointer.h:93
BufferSize_t copyfrom_shift(const void *src, BufferSize_t sz)
Definition: Pointer.h:197
void long_shift(BufferSize_t sz)
Definition: Pointer.cxx:25
BufferSize_t fullsize() const
Definition: Pointer.h:149
BufferSize_t copyfromstr(const char *str, unsigned len=0)
Definition: Pointer.cxx:129
void reset(const Buffer &src, BufferSize_t pos=0, BufferSize_t fullsz=0)
Definition: Pointer.h:119
void * ptr() const
Definition: Pointer.h:146
float consumed_size() const
Returns ratio between filled size and buffer size, 0 when exmpy.
Definition: Pointer.cxx:155
BufferSize_t rawsize() const
Definition: Pointer.h:148
void reset()
Definition: Pointer.h:136
Buffer buf()
Definition: Pointer.h:151
void reset(const Pointer &src, BufferSize_t pos=0, BufferSize_t fullsz=0)
Definition: Pointer.h:108
Buffer fBuf
we keep reference on the buffer, could be only used in same thread
Definition: Pointer.h:39
BufferSize_t copyto_shift(void *tgt, BufferSize_t sz)
Definition: Pointer.h:180
void * operator()() const
Definition: Pointer.h:145
BufferSize_t copyfrom_shift(const Pointer &src, BufferSize_t sz=0)
Returns actual size copied and shifted.
Definition: Pointer.h:192
Pointer(const void *buf, BufferSize_t sz)
Definition: Pointer.h:57
BufferSize_t fFullSize
full size of memory from pointer till the end
Definition: Pointer.h:43
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
Event manipulation API.
Definition: api.h:23
uint32_t BufferSize_t
Definition: Buffer.h:32