Pothos  0.7.0-gf7fbae99
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) noexcept;
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 
149  template <typename ElementType>
150  ElementType as(void) const;
151 
161  template <typename ElementType>
162  operator ElementType(void) const;
163 
168  bool unique(void) const;
169 
174  size_t useCount(void) const;
175 
180  explicit operator bool(void) const;
181 
186  void clear(void);
187 
189  template<class Archive>
190  void serialize(Archive & ar, const unsigned int version);
191 
200  void append(const BufferChunk &other);
201 
210  BufferChunk convert(const DType &dtype, const size_t numElems = 0) const;
211 
220  std::pair<BufferChunk, BufferChunk> convertComplex(const DType &dtype, const size_t numElems = 0) const;
221 
231  size_t convert(const BufferChunk &outBuff, const size_t numElems = 0) const;
232 
243  size_t convertComplex(const BufferChunk &outBuffRe, const BufferChunk &outBuffIm, const size_t numElems = 0) const;
244 
245 private:
246  friend BufferAccumulator;
247  ManagedBuffer _managedBuffer;
248  void _incrNextBuffers(void);
249  void _decrNextBuffers(void);
250  size_t _nextBuffers;
251 };
252 
257 inline bool operator==(const BufferChunk &lhs, const BufferChunk &rhs);
258 
259 } //namespace Pothos
260 
261 inline bool Pothos::operator==(const Pothos::BufferChunk &lhs, const Pothos::BufferChunk &rhs)
262 {
263  return lhs.address == rhs.address and lhs.length == rhs.length and lhs.getBuffer() == rhs.getBuffer();
264 }
265 
267  address(0),
268  length(0),
269  _nextBuffers(0)
270 {
271  return;
272 }
273 
274 inline Pothos::BufferChunk::BufferChunk(const size_t numBytes):
275  address(0),
276  length(numBytes),
277  _managedBuffer(Pothos::SharedBuffer::make(numBytes)),
278  _nextBuffers(0)
279 {
281 }
282 
283 inline Pothos::BufferChunk::BufferChunk(const DType &dtype, const size_t numElems):
284  address(0),
285  length(dtype.size()*numElems),
286  dtype(dtype),
287  _managedBuffer(Pothos::SharedBuffer::make(length)),
288  _nextBuffers(0)
289 {
291 }
292 
294  address(buffer.getAddress()),
295  length(buffer.getLength()),
296  _managedBuffer(buffer),
297  _nextBuffers(0)
298 {
299  return;
300 }
301 
303  address(buffer.getBuffer().getAddress()),
304  length(buffer.getBuffer().getLength()),
305  _managedBuffer(buffer),
306  _nextBuffers(0)
307 {
308  return;
309 }
310 
312  address(other.address),
313  length(other.length),
314  dtype(other.dtype),
315  _managedBuffer(other._managedBuffer)
316 {
317  _incrNextBuffers();
318 }
319 
321  address(std::move(other.address)),
322  length(std::move(other.length)),
323  dtype(std::move(other.dtype)),
324  _managedBuffer(std::move(other._managedBuffer)),
325  _nextBuffers(std::move(other._nextBuffers))
326 {
327  other.address = 0;
328  other.length = 0;
329  other._nextBuffers = 0;
330 }
331 
333 {
334  _decrNextBuffers();
335 }
336 
338 {
339  _decrNextBuffers();
340  address = other.address;
341  length = other.length;
342  dtype = other.dtype;
343  _managedBuffer = other._managedBuffer;
344  _incrNextBuffers();
345  return *this;
346 }
347 
349 {
350  _decrNextBuffers();
351  address = std::move(other.address);
352  length = std::move(other.length);
353  dtype = std::move(other.dtype);
354  _managedBuffer = std::move(other._managedBuffer);
355  _nextBuffers = std::move(other._nextBuffers);
356  other.address = 0;
357  other.length = 0;
358  other._nextBuffers = 0;
359  return *this;
360 }
361 
362 inline void Pothos::BufferChunk::clear(void)
363 {
364  _decrNextBuffers();
365  address = 0;
366  length = 0;
367  dtype = Pothos::DType();
368  _managedBuffer.reset();
369  _nextBuffers = 0;
370 }
371 
372 inline size_t Pothos::BufferChunk::elements(void) const
373 {
374  return this->length/this->dtype.size();
375 }
376 
377 inline void Pothos::BufferChunk::setElements(const size_t numElements)
378 {
379  this->length = numElements*this->dtype.size();
380 }
381 
383 {
384  return _managedBuffer.getBuffer();
385 }
386 
388 {
389  return _managedBuffer;
390 }
391 
392 inline size_t Pothos::BufferChunk::getAlias(void) const
393 {
394  const auto &buffer = getBuffer();
395  if (buffer.getAlias() == 0) return 0;
396  if (address > buffer.getAlias()) return address - buffer.getLength();
397  else return address + buffer.getLength();
398 }
399 
400 inline size_t Pothos::BufferChunk::getEnd(void) const
401 {
402  return address + length;
403 }
404 
405 template <typename ElementType>
406 ElementType Pothos::BufferChunk::as(void) const
407 {
408  return reinterpret_cast<ElementType>(address);
409 }
410 
411 template <typename ElementType>
412 Pothos::BufferChunk::operator ElementType(void) const
413 {
414  return reinterpret_cast<ElementType>(address);
415 }
416 
417 inline Pothos::BufferChunk::operator bool(void) const
418 {
419  return bool(_managedBuffer);
420 }
421 
422 inline bool Pothos::BufferChunk::unique(void) const
423 {
424  return _managedBuffer.unique();
425 }
426 
427 inline size_t Pothos::BufferChunk::useCount(void) const
428 {
429  return _managedBuffer.useCount();
430 }
Definition: BufferAccumulator.hpp:31
bool unique(void) const
Definition: BufferChunk.hpp:422
bool unique(void) const
Definition: ManagedBuffer.hpp:241
#define POTHOS_API
Definition: Config.hpp:41
POTHOS_API bool operator==(const Callable &lhs, const Callable &rhs)
size_t elements(void) const
Definition: BufferChunk.hpp:372
const ManagedBuffer & getManagedBuffer(void) const
Definition: BufferChunk.hpp:387
size_t size(void) const
Get the size of this DType in bytes.
Definition: DType.hpp:198
const SharedBuffer & getBuffer(void) const
Definition: BufferChunk.hpp:382
ElementType as(void) const
Definition: BufferChunk.hpp:406
size_t getEnd(void) const
Definition: BufferChunk.hpp:400
Definition: ArchiveEntry.hpp:20
void reset(void)
Definition: ManagedBuffer.hpp:179
size_t getAddress(void) const
Get the address of the first byte of the buffer.
Definition: SharedBuffer.hpp:132
Definition: ManagedBuffer.hpp:31
BufferChunk(void)
Definition: BufferChunk.hpp:266
void serialize(Archive &ar, std::complex< T > &t, const unsigned int ver)
Definition: Complex.hpp:37
size_t useCount(void) const
Definition: ManagedBuffer.hpp:247
void setElements(const size_t numElements)
Definition: BufferChunk.hpp:377
void clear(void)
Definition: BufferChunk.hpp:362
BufferChunk & operator=(const BufferChunk &other)
BufferChunk copy assignment operator.
Definition: BufferChunk.hpp:337
~BufferChunk(void)
BufferChunk destructor.
Definition: BufferChunk.hpp:332
const SharedBuffer & getBuffer(void) const
Definition: ManagedBuffer.hpp:185
size_t getAlias(void) const
Definition: BufferChunk.hpp:392
Definition: SharedBuffer.hpp:21
DType dtype
Definition: BufferChunk.hpp:102
Definition: BufferChunk.hpp:30
size_t useCount(void) const
Definition: BufferChunk.hpp:427
size_t length
Definition: BufferChunk.hpp:97
Definition: DType.hpp:38
size_t address
Definition: BufferChunk.hpp:92