blockfactory  0.8
A framework for wrapping algorithms for dataflow programming
Signal.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * GNU Lesser General Public License v2.1 or any later version.
7  */
8 
9 #ifndef BLOCKFACTORY_CORE_SIGNAL_H
10 #define BLOCKFACTORY_CORE_SIGNAL_H
11 
12 #include "BlockFactory/Core/Port.h"
13 
14 #include <memory>
15 
16 namespace blockfactory {
17  namespace core {
18  class Signal;
19  } // namespace core
20 } // namespace blockfactory
21 
37 {
38 public:
56  enum class DataFormat
57  {
65  NONCONTIGUOUS = 0,
66 
73  CONTIGUOUS = 1,
74 
84  };
85 
87  const Port::DataType& dataType = Port::DataType::DOUBLE);
88  ~Signal();
89 
90  Signal(const Signal& other);
91  Signal& operator=(const Signal& other) = delete;
92 
93  Signal(Signal&& other);
94  Signal& operator=(Signal&& other) = delete;
95 
108  bool initializeBufferFromContiguous(const void* buffer, size_t len);
109 
122  bool initializeBufferFromContiguousZeroCopy(const void* buffer, size_t len);
123 
137  bool initializeBufferFromNonContiguous(const void* const* bufferPtrs, size_t len);
138 
146  bool isValid() const;
147 
155  size_t getWidth() const;
156 
165 
173  DataFormat getDataFormat() const;
174 
190  template <typename T>
191  T* getBuffer();
192 
198  template <typename T>
199  const T* getBuffer() const;
200 
214  template <typename T>
215  T get(const size_t i) const;
216 
226  bool set(const size_t index, const double data);
227 
242  template <typename T>
243  bool setBuffer(const T* data, const size_t length);
244 
245 #ifndef DOXYGEN_SHOULD_SKIP_THIS
246 private:
247  size_t m_width = 0;
248  const Port::DataType m_portDataType;
249  const DataFormat m_dataFormat;
250  void* m_bufferPtr = nullptr;
251 
252  template <typename T>
253  T* getBufferImpl() const;
254 
255  void deleteBuffer();
256  void allocateBuffer(const void* const bufferInput, void*& bufferOutput, size_t length);
257 #endif
258 };
259 
260 // Explicit declaration of templates for all the supported types
261 // =============================================================
262 
263 // TODO: for the time being, only DOUBLE is allowed. The toolbox has an almost complete support to
264 // many other data types, but they need to be tested.
265 
266 namespace blockfactory {
267  namespace core {
268  // DataType::DOUBLE
269  extern template double* Signal::getBuffer<double>();
270  extern template const double* Signal::getBuffer<double>() const;
271  extern template double Signal::get<double>(const size_t i) const;
272  extern template bool Signal::setBuffer<double>(const double* data, const size_t length);
273  } // namespace core
274 } // namespace blockfactory
275 
276 #endif // BLOCKFACTORY_CORE_SIGNAL_H
bool initializeBufferFromNonContiguous(const void *const *bufferPtrs, size_t len)
Initialize the signal from a non-contiguous buffer.
DataFormat getDataFormat() const
Read the core::Signal::DataFormat of the signal.
Class to access data shared by blocks, represented as signals.
Definition: Signal.h:36
DataFormat
Definition: Signal.h:56
bool initializeBufferFromContiguous(const void *buffer, size_t len)
Initialize the signal from a contiguous buffer.
bool setBuffer(const T *data, const size_t length)
Set the pointer to the buffer storing signal&#39;s data.
Definition: Block.h:17
Port::DataType getPortDataType() const
Read the Port::DataType of the signal.
Signal & operator=(const Signal &other)=delete
bool isValid() const
Check if the signal is valid.
DataType
Defines allowed port data types.
Definition: Port.h:62
bool initializeBufferFromContiguousZeroCopy(const void *buffer, size_t len)
Initialize the signal from a contiguous buffer without copying data.
T * getBuffer()
Get the pointer to the buffer storing signal&#39;s data.
size_t getWidth() const
Read the width of the signal.
Signal(const DataFormat &dataFormat=DataFormat::CONTIGUOUS_ZEROCOPY, const Port::DataType &dataType=Port::DataType::DOUBLE)