Helios++
Helios software for LiDAR simulations
Yielder.h
1 #pragma once
2 
3 #include <filems/facade/FMSWriteFacade.h>
4 
5 #include <mutex>
6 #include <cstdlib>
7 #include <vector>
8 
17 template <typename T>
18 class Yielder{
19 protected:
20  // *** ATTRIBUTES *** //
21  // ******************** //
26  std::mutex mtx;
31  std::size_t bufferSize;
35  std::vector<T> buffer;
36 
37 public:
38  // *** CONSTRUCTION / DESTRUCTION *** //
39  // ************************************ //
44  Yielder(std::size_t bufferSize=256){
46  }
47  virtual ~Yielder() = default;
48 
49  // *** YIELD METHODS *** //
50  // *********************** //
55  inline void yield(){
56  // Copy and clear in critical region
57  mtx.lock();
58  vector<T> copy = copyBuffer();
59  buffer.clear();
60  mtx.unlock();
61  // Digest temporal copy
62  digest(copy);
63  }
70  inline void push(T const &elem){
71  mtx.lock();
72  buffer.push_back(elem);
73  mtx.unlock();
74  if(buffer.size() >= bufferSize) yield();
75  }
76 
81  virtual void digest(vector<T> &copy) = 0;
86  virtual vector<T> copyBuffer() const {return vector<T>(buffer);}
87 
88 
89  // *** GETTERs and SETTERs *** //
90  // ***************************** //
95  inline void setBufferSize(size_t const bufferSize){
96  this->bufferSize = bufferSize;
97  }
102  inline size_t getBufferSize() const {return bufferSize;}
103 };
Abstract class representing a yielder. It is, an object which can be used to accumulate inputs until ...
Definition: Yielder.h:18
virtual void digest(vector< T > &copy)=0
Abstract method that must be overridden by any concrete Yielder implementation to specify how a yield...
std::size_t bufferSize
The number of elements that can be buffered before forcing the yield operation.
Definition: Yielder.h:31
std::vector< T > buffer
Where the elements are stored.
Definition: Yielder.h:35
void push(T const &elem)
Push the element into the yielder. The element could be simply accumulated or either it could be dire...
Definition: Yielder.h:70
std::mutex mtx
The mutex to handle concurrent push backs to the buffer vector and the yielding operation itself.
Definition: Yielder.h:26
size_t getBufferSize() const
Obtain the current buffer size of the yielder.
Definition: Yielder.h:102
virtual vector< T > copyBuffer() const
Make a copy of the buffer with its current state.
Definition: Yielder.h:86
Yielder(std::size_t bufferSize=256)
Default constructor for the abstract yielder.
Definition: Yielder.h:44
void setBufferSize(size_t const bufferSize)
Set the buffer size of the yielder, effectively.
Definition: Yielder.h:95
void yield()
Make the yielder flush its elements so the output is performed.
Definition: Yielder.h:55