Pothos  0.1.1
The Pothos dataflow programming software suite
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
BufferChunk.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #include <Pothos/Config.hpp>
17 
18 namespace Pothos {
19 
27 {
28 public:
29 
31  static const BufferChunk &null(void);
32 
37  BufferChunk(void);
38 
46  BufferChunk(const size_t numBytes);
47 
56  BufferChunk(const DType &dtype, const size_t numElems);
57 
62  BufferChunk(const SharedBuffer &buffer);
63 
68  BufferChunk(const ManagedBuffer &buffer);
69 
73  size_t address;
74 
78  size_t length;
79 
84 
89  size_t elements(void) const;
90 
94  const SharedBuffer &getBuffer(void) const;
95 
99  const ManagedBuffer &getManagedBuffer(void) const;
100 
106  size_t getAlias(void) const;
107 
113  size_t getEnd(void) const;
114 
119  template <typename ElementType>
120  ElementType as(void) const;
121 
126  bool unique(void) const;
127 
132  size_t useCount(void) const;
133 
138  pothos_explicit operator bool(void) const;
139 
141  template<class Archive>
142  void serialize(Archive & ar, const unsigned int version);
143 
152  void append(const BufferChunk &other);
153 
162  BufferChunk convert(const DType &dtype, const size_t numElems = 0) const;
163 
172  std::pair<BufferChunk, BufferChunk> convertComplex(const DType &dtype, const size_t numElems = 0) const;
173 
174 private:
175  SharedBuffer _buffer;
176  ManagedBuffer _managedBuffer;
177 };
178 
183 inline bool operator==(const BufferChunk &lhs, const BufferChunk &rhs);
184 
185 } //namespace Pothos
186 
187 #include <cassert>
188 
189 inline bool Pothos::operator==(const Pothos::BufferChunk &lhs, const Pothos::BufferChunk &rhs)
190 {
191  return lhs.address == rhs.address and lhs.length == rhs.length and lhs.getBuffer() == rhs.getBuffer();
192 }
193 
195  address(0),
196  length(0)
197 {
198  return;
199 }
200 
201 inline size_t Pothos::BufferChunk::elements(void) const
202 {
203  return this->length/this->dtype.size();
204 }
205 
207 {
208  return _buffer;
209 }
210 
212 {
213  return _managedBuffer;
214 }
215 
216 inline size_t Pothos::BufferChunk::getAlias(void) const
217 {
218  if (_buffer.getAlias() == 0) return 0;
219  if (address > _buffer.getAlias()) return address - _buffer.getLength();
220  else return address + _buffer.getLength();
221 }
222 
223 inline size_t Pothos::BufferChunk::getEnd(void) const
224 {
225  return address + length;
226 }
227 
228 template <typename ElementType>
229 ElementType Pothos::BufferChunk::as(void) const
230 {
231  return reinterpret_cast<ElementType>(address);
232 }
233 
234 inline Pothos::BufferChunk::operator bool(void) const
235 {
236  return (address != 0) or bool(_buffer);
237 }
238 
239 inline bool Pothos::BufferChunk::unique(void) const
240 {
241  return this->useCount() == 1;
242 }
243 
244 inline size_t Pothos::BufferChunk::useCount(void) const
245 {
246  //dont count the copy held by the managed buffer
247  if (_managedBuffer)
248  {
249  assert(_buffer.useCount() >= 2);
250  return _buffer.useCount() - 1;
251  }
252  return _buffer.useCount();
253 }
size_t elements(void) const
Definition: BufferChunk.hpp:201
const SharedBuffer & getBuffer(void) const
Definition: BufferChunk.hpp:206
#define pothos_explicit
Definition: Config.hpp:85
#define POTHOS_API
Definition: Config.hpp:41
ElementType as(void) const
Definition: BufferChunk.hpp:229
size_t getEnd(void) const
Definition: BufferChunk.hpp:223
size_t getAlias(void) const
Definition: BufferChunk.hpp:216
bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs)
Definition: ManagedBuffer.hpp:132
bool unique(void) const
Definition: BufferChunk.hpp:239
Definition: ManagedBuffer.hpp:27
size_t useCount(void) const
Definition: BufferChunk.hpp:244
BufferChunk(void)
Definition: BufferChunk.hpp:194
void serialize(Archive &, Pothos::Detail::ObjectContainer &, const unsigned int)
Definition: Serialize.hpp:46
Definition: SharedBuffer.hpp:21
const ManagedBuffer & getManagedBuffer(void) const
Definition: BufferChunk.hpp:211
DType dtype
Definition: BufferChunk.hpp:83
Definition: BufferChunk.hpp:26
size_t length
Definition: BufferChunk.hpp:78
Definition: DType.hpp:38
size_t address
Definition: BufferChunk.hpp:73