Helios++
Helios software for LiDAR simulations
BufferedReadingStrategy.h
1 #pragma once
2 
3 #include <filems/read/strategies/ReadingStrategy.h>
4 
5 #include <queue>
6 
7 namespace helios { namespace filems{
8 
9 using std::queue;
10 
19 template <typename ReadArg>
20 class BufferedReadingStrategy : public ReadingStrategy<ReadArg>{
21 protected:
22  // *** ATTRIBUTES *** //
23  // ******************** //
28  size_t bufferSize;
32  ReadArg *buffer = nullptr;
37 
41  size_t cachedIndex = 0;
46  size_t cachedMaxIndex = 0;
47 
48 public:
49  // *** CONSTRUCTION / DESTRUCTION *** //
50  // ************************************ //
57  size_t const bufferSize = 256
58  ) :
61  {
62  buffer = new ReadArg[bufferSize];
63  }
64  virtual ~BufferedReadingStrategy(){
65  delete[] buffer;
66  }
67 
68  // *** READING STRATEGY METHODS *** //
69  // ********************************** //
76  ReadArg read() override{
77  // When buffer is not empty : read next element (FIFO)
78  if(!isBufferEmpty()){
79  size_t const bufferIndex = cachedIndex;
80  ++cachedIndex;
82  return buffer[bufferIndex];
83  }
84  // When buffer is empty : populate it again by reading strategy
85  try{
86  while(cachedMaxIndex < bufferSize){
89  }
90  }
91  catch(EndOfReadingException &eorex){} // Handle EndOfReadingExceptions
92  // If buffer stills empty after trying to populate it
93  if(isBufferEmpty()){ // Throw EndOfReadingException
94  throw EndOfReadingException();
95  }
96  // If not, return next from buffer
97  cachedIndex = 1;
99  return buffer[0];
100  }
101 
102 protected:
103  // *** BUFFER HANDLING METHODS *** //
104  // ********************************* //
109  virtual bool isBufferEmpty() const {return cachedMaxIndex==0;}
110 
111 public:
112  // *** GETTERs and SETTERs *** //
113  // ***************************** //
120  virtual inline size_t getBufferSize() {return bufferSize;}
127  virtual inline void setBufferSize(size_t const bufferSize){
128  this->bufferSize = bufferSize;
129  delete[] this->buffer;
130  this->buffer = new ReadArg[bufferSize];
131  }
132 
133 
134 };
135 
136 }}
Class defining a strategy that provides support for buffering of consecutive readings.
Definition: BufferedReadingStrategy.h:20
size_t bufferSize
The maximum number of ReadArg type elements that a buffer is allowed to hold.
Definition: BufferedReadingStrategy.h:28
size_t cachedMaxIndex
The cached index defining the next to the greatest admissible index for the current state of the buff...
Definition: BufferedReadingStrategy.h:46
virtual void setBufferSize(size_t const bufferSize)
Set the buffer size and update the buffer to fit.
Definition: BufferedReadingStrategy.h:127
size_t cachedIndex
The cached index defining the state of the buffer.
Definition: BufferedReadingStrategy.h:41
ReadArg * buffer
The buffer where multiple consecutive reads are stored.
Definition: BufferedReadingStrategy.h:32
ReadingStrategy< ReadArg > & readingStrategy
The reading strategy that is being extended with buffer support.
Definition: BufferedReadingStrategy.h:36
ReadArg read() override
Take until buffer is empty, read always that buffer needs to be fulfilled.
Definition: BufferedReadingStrategy.h:76
virtual bool isBufferEmpty() const
Check whether the buffer is empty or not.
Definition: BufferedReadingStrategy.h:109
BufferedReadingStrategy(ReadingStrategy< ReadArg > &readingStrategy, size_t const bufferSize=256)
Default constructor for buffered reading strategy.
Definition: BufferedReadingStrategy.h:55
virtual size_t getBufferSize()
Obtain the buffer size.
Definition: BufferedReadingStrategy.h:120
Class representing the end of reading exception for FMS readers.
Definition: EndOfReadingException.h:15
Abstract class defining the fundamentals of any file reading strategy.
Definition: ReadingStrategy.h:12
virtual ReadType read()=0
Read from file.