fifo.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef ECL_CONTAINERS_FIFO_HPP_
16 #define ECL_CONTAINERS_FIFO_HPP_
17 
18 /*****************************************************************************
19 ** Includes
20 *****************************************************************************/
21 
22 #include <algorithm> // std::copy
23 #include <ecl/config/macros.hpp>
26 #include "array.hpp"
27 
28 /*****************************************************************************
29 ** Namespaces
30 *****************************************************************************/
31 
32 namespace ecl {
33 
34 /*****************************************************************************
35 ** Interface
36 *****************************************************************************/
81 template<typename T>
83 {
84 public:
89  FiFo( const unsigned int length = 0 ) :
90  size_fifo(length),
91  running_index(0)
92  {
93  if ( size_fifo > 0 ) {
94  data.resize( size_fifo );
95  }
96  }
97 
103  FiFo( const unsigned int length, const T &value ) :
104  size_fifo(length),
105  running_index(0)
106  {
107  if ( size_fifo > 0 ) {
108  data = Array<T>::Constant(size_fifo,value);
109  }
110  }
111  virtual ~FiFo() {}
122  T & operator[] (int idx) {
123  return data[ ((running_index+idx)%size_fifo) ];
124  }
125 
132  const T & operator[] (int idx) const {
133  return data[ ((running_index+idx)%size_fifo) ];
134  }
135 
143  void push_back( const T & datum ) {
144  data[ running_index++ ] = datum;
145  running_index %= size_fifo;
146  }
147 
152  void fill( const T & value ) {
153  for( unsigned int i=0; i<size_fifo; i++ ) data[i] = value;
154  }
155 
160  unsigned int get_idx() { return running_index;}
161 
167  void resize( unsigned int length ) {
168  size_fifo = length;
169  ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
170  data.resize( size_fifo );
171  }
172 
173 private:
175  unsigned int size_fifo;
176  unsigned int running_index;
177 };
178 
179 } // namespace ecl
180 
181 #endif /* ECL_CONTAINERS_FIFO_HPP_ */
void push_back(const T &datum)
Push back onto the fifo.
Definition: fifo.hpp:143
Embedded control libraries.
unsigned int running_index
Definition: fifo.hpp:176
FiFo(const unsigned int length=0)
Initialises the fifo, but does not fill it.
Definition: fifo.hpp:89
unsigned int size_fifo
Definition: fifo.hpp:175
void resize(unsigned int length)
Resize the fifo storage.
Definition: fifo.hpp:167
#define LOC
virtual ~FiFo()
Default destructor.
Definition: fifo.hpp:111
Really simple fifo implementation.
Definition: fifo.hpp:82
#define ecl_assert_throw(expression, exception)
OutOfRangeError
Fixed size containers with a few bells and whistles.
unsigned int get_idx()
Index of the oldest element in the fifo.
Definition: fifo.hpp:160
ecl::Array< T > data
Definition: fifo.hpp:174
void fill(const T &value)
One-shot fill method.
Definition: fifo.hpp:152
static ConstantArray< Type, Size > Constant(const Type &value)
FiFo(const unsigned int length, const T &value)
Initialise and fill the fifo.
Definition: fifo.hpp:103
#define ECL_PUBLIC


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:43