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>
82 class ECL_PUBLIC FiFo
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_ */
ecl::FiFo::FiFo
FiFo(const unsigned int length=0)
Initialises the fifo, but does not fill it.
Definition: fifo.hpp:103
array.hpp
Fixed size containers with a few bells and whistles.
ecl::blueprints::ArrayFactory< Type, DynamicStorage >::Constant
static ConstantArray< Type, Size > Constant(const Type &value)
Definition: array_no_mem_check.hpp:529
ecl::FiFo
Really simple fifo implementation.
Definition: fifo.hpp:88
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
standard_exception.hpp
ecl::Array< T >
macros.hpp
ecl::OutOfRangeError
OutOfRangeError
ecl
Embedded control libraries.
ECL_PUBLIC
#define ECL_PUBLIC
compile_time_assert.hpp


ecl_containers
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:34