Pothos  0.4.1-gb758ed46
The Pothos dataflow programming software suite
BufferChunk.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #include <Pothos/Config.hpp>
17 #include <utility> //move
18 
19 namespace Pothos {
20 
22 class BufferAccumulator;
23 
31 {
32 public:
33 
35  static const BufferChunk &null(void);
36 
41  BufferChunk(void);
42 
50  BufferChunk(const size_t numBytes);
51 
60  BufferChunk(const DType &dtype, const size_t numElems);
61 
66  BufferChunk(const SharedBuffer &buffer);
67 
72  BufferChunk(const ManagedBuffer &buffer);
73 
75  BufferChunk(const BufferChunk &other);
76 
78  BufferChunk(BufferChunk &&other);
79 
81  ~BufferChunk(void);
82 
84  BufferChunk &operator=(const BufferChunk &other);
85 
87  BufferChunk &operator=(BufferChunk &&other);
88 
92  size_t address;
93 
97  size_t length;
98 
103 
108  size_t elements(void) const;
109 
115  void setElements(const size_t numElements);
116 
120  const SharedBuffer &getBuffer(void) const;
121 
125  const ManagedBuffer &getManagedBuffer(void) const;
126 
132  size_t getAlias(void) const;
133 
139  size_t getEnd(void) const;
140 
145  template <typename ElementType>
146  ElementType as(void) const;
147 
152  bool unique(void) const;
153 
158  size_t useCount(void) const;
159 
164  explicit operator bool(void) const;
165 
170  void clear(void);
171 
173  template<class Archive>
174  void serialize(Archive & ar, const unsigned int version);
175 
184  void append(const BufferChunk &other);
185 
194  BufferChunk convert(const DType &dtype, const size_t numElems = 0) const;
195 
204  std::pair<BufferChunk, BufferChunk> convertComplex(const DType &dtype, const size_t numElems = 0) const;
205 
215  size_t convert(const BufferChunk &outBuff, const size_t numElems = 0) const;
216 
227  size_t convertComplex(const BufferChunk &outBuffRe, const BufferChunk &outBuffIm, const size_t numElems = 0) const;
228 
229 private:
230  friend BufferAccumulator;
231  SharedBuffer _buffer;
232  ManagedBuffer _managedBuffer;
233  void _incrNextBuffers(void);
234  void _decrNextBuffers(void);
235  size_t _nextBuffers;
236 };
237 
242 inline bool operator==(const BufferChunk &lhs, const BufferChunk &rhs);
243 
244 } //namespace Pothos
245 
246 #include <cassert>
247 
248 inline bool Pothos::operator==(const Pothos::BufferChunk &lhs, const Pothos::BufferChunk &rhs)
249 {
250  return lhs.address == rhs.address and lhs.length == rhs.length and lhs.getBuffer() == rhs.getBuffer();
251 }
252 
254  address(0),
255  length(0),
256  _nextBuffers(0)
257 {
258  return;
259 }
260 
261 inline Pothos::BufferChunk::BufferChunk(const size_t numBytes):
262  address(0),
263  length(numBytes),
264  _buffer(Pothos::SharedBuffer::make(numBytes)),
265  _nextBuffers(0)
266 {
267  address = _buffer.getAddress();
268 }
269 
270 inline Pothos::BufferChunk::BufferChunk(const DType &dtype, const size_t numElems):
271  address(0),
272  length(dtype.size()*numElems),
273  dtype(dtype),
274  _buffer(Pothos::SharedBuffer::make(length)),
275  _nextBuffers(0)
276 {
277  address = _buffer.getAddress();
278 }
279 
281  address(buffer.getAddress()),
282  length(buffer.getLength()),
283  _buffer(buffer),
284  _nextBuffers(0)
285 {
286  return;
287 }
288 
290  address(buffer.getBuffer().getAddress()),
291  length(buffer.getBuffer().getLength()),
292  _buffer(buffer.getBuffer()),
293  _managedBuffer(buffer),
294  _nextBuffers(0)
295 {
296  return;
297 }
298 
300  address(other.address),
301  length(other.length),
302  dtype(other.dtype),
303  _buffer(other._buffer),
304  _managedBuffer(other._managedBuffer)
305 {
306  _incrNextBuffers();
307 }
308 
310  address(std::move(other.address)),
311  length(std::move(other.length)),
312  dtype(std::move(other.dtype)),
313  _buffer(std::move(other._buffer)),
314  _managedBuffer(std::move(other._managedBuffer)),
315  _nextBuffers(std::move(other._nextBuffers))
316 {
317  other._nextBuffers = 0;
318 }
319 
321 {
322  _decrNextBuffers();
323 }
324 
326 {
327  _decrNextBuffers();
328  address = other.address;
329  length = other.length;
330  dtype = other.dtype;
331  _buffer = other._buffer;
332  _managedBuffer = other._managedBuffer;
333  _incrNextBuffers();
334  return *this;
335 }
336 
338 {
339  _decrNextBuffers();
340  address = std::move(other.address);
341  length = std::move(other.length);
342  dtype = std::move(other.dtype);
343  _buffer = std::move(other._buffer);
344  _managedBuffer = std::move(other._managedBuffer);
345  _nextBuffers = std::move(other._nextBuffers);
346  other._nextBuffers = 0;
347  return *this;
348 }
349 
350 inline void Pothos::BufferChunk::clear(void)
351 {
352  _decrNextBuffers();
353  address = 0;
354  length = 0;
355  dtype = Pothos::DType();
356  _buffer = Pothos::SharedBuffer();
357  _managedBuffer.reset();
358  _nextBuffers = 0;
359 }
360 
361 inline size_t Pothos::BufferChunk::elements(void) const
362 {
363  return this->length/this->dtype.size();
364 }
365 
366 inline void Pothos::BufferChunk::setElements(const size_t numElements)
367 {
368  this->length = numElements*this->dtype.size();
369 }
370 
372 {
373  return _buffer;
374 }
375 
377 {
378  return _managedBuffer;
379 }
380 
381 inline size_t Pothos::BufferChunk::getAlias(void) const
382 {
383  if (_buffer.getAlias() == 0) return 0;
384  if (address > _buffer.getAlias()) return address - _buffer.getLength();
385  else return address + _buffer.getLength();
386 }
387 
388 inline size_t Pothos::BufferChunk::getEnd(void) const
389 {
390  return address + length;
391 }
392 
393 template <typename ElementType>
394 ElementType Pothos::BufferChunk::as(void) const
395 {
396  return reinterpret_cast<ElementType>(address);
397 }
398 
399 inline Pothos::BufferChunk::operator bool(void) const
400 {
401  return (address != 0) or bool(_buffer);
402 }
403 
404 inline bool Pothos::BufferChunk::unique(void) const
405 {
406  return this->useCount() == 1;
407 }
408 
409 inline size_t Pothos::BufferChunk::useCount(void) const
410 {
411  if (_managedBuffer) return _managedBuffer.useCount();
412  return _buffer.useCount();
413 }
Definition: BufferAccumulator.hpp:31
size_t elements(void) const
Definition: BufferChunk.hpp:361
const SharedBuffer & getBuffer(void) const
Definition: BufferChunk.hpp:371
#define POTHOS_API
Definition: Config.hpp:41
ElementType as(void) const
Definition: BufferChunk.hpp:394
size_t getEnd(void) const
Definition: BufferChunk.hpp:388
size_t useCount(void) const
Definition: SharedBuffer.hpp:154
size_t getAlias(void) const
Definition: BufferChunk.hpp:381
Definition: Testing.hpp:134
void reset(void)
Definition: ManagedBuffer.hpp:170
bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs)
Definition: ManagedBuffer.hpp:261
size_t getLength(void) const
Get the length of the buffer in bytes.
Definition: SharedBuffer.hpp:134
bool unique(void) const
Definition: BufferChunk.hpp:404
Definition: ManagedBuffer.hpp:31
size_t getAddress(void) const
Get the address of the first byte of the buffer.
Definition: SharedBuffer.hpp:129
size_t useCount(void) const
Definition: BufferChunk.hpp:409
BufferChunk(void)
Definition: BufferChunk.hpp:253
void setElements(const size_t numElements)
Definition: BufferChunk.hpp:366
void clear(void)
Definition: BufferChunk.hpp:350
BufferChunk & operator=(const BufferChunk &other)
BufferChunk copy assignment operator.
Definition: BufferChunk.hpp:325
void serialize(Archive &, Pothos::Detail::ObjectContainer &, const unsigned int)
Definition: Serialize.hpp:46
~BufferChunk(void)
BufferChunk destructor.
Definition: BufferChunk.hpp:320
size_t useCount(void) const
Definition: ManagedBuffer.hpp:237
size_t getAlias(void) const
Definition: SharedBuffer.hpp:139
Definition: SharedBuffer.hpp:21
const ManagedBuffer & getManagedBuffer(void) const
Definition: BufferChunk.hpp:376
DType dtype
Definition: BufferChunk.hpp:102
Definition: BufferChunk.hpp:30
size_t length
Definition: BufferChunk.hpp:97
size_t size(void) const
Get the size of this DType in bytes.
Definition: DType.hpp:198
Definition: DType.hpp:38
size_t address
Definition: BufferChunk.hpp:92