fifo.hpp
Go to the documentation of this file.
00001 
00011 /*****************************************************************************
00012 ** Ifdefs
00013 *****************************************************************************/
00014 
00015 #ifndef ECL_CONTAINERS_FIFO_HPP_
00016 #define ECL_CONTAINERS_FIFO_HPP_
00017 
00018 /*****************************************************************************
00019 ** Includes
00020 *****************************************************************************/
00021 
00022 #include <algorithm> // std::copy
00023 #include <ecl/config/macros.hpp>
00024 #include <ecl/errors/compile_time_assert.hpp>
00025 #include <ecl/exceptions/standard_exception.hpp>
00026 #include "array.hpp"
00027 
00028 /*****************************************************************************
00029 ** Namespaces
00030 *****************************************************************************/
00031 
00032 namespace ecl {
00033 
00034 /*****************************************************************************
00035 ** Interface
00036 *****************************************************************************/
00081 template<typename T>
00082 class ECL_PUBLIC FiFo
00083 {
00084 public:
00089         FiFo( const unsigned int length = 0 ) :
00090                 size_fifo(length),
00091                 running_index(0)
00092         {
00093                 if ( size_fifo > 0 ) {
00094                         data.resize( size_fifo );
00095                 }
00096         }
00097 
00103         FiFo( const unsigned int length, const T &value ) :
00104                 size_fifo(length),
00105                 running_index(0)
00106         {
00107                 if ( size_fifo > 0 ) {
00108                         data = Array<T>::Constant(size_fifo,value);
00109                 }
00110         }
00111         virtual ~FiFo() {} 
00122         T & operator[] (int idx) {
00123                 return data[ ((running_index+idx)%size_fifo) ];
00124         }
00125 
00132         const T & operator[] (int idx) const {
00133                 return data[ ((running_index+idx)%size_fifo) ];
00134         }
00135 
00143         void push_back( const T & datum ) {
00144                 data[ running_index++ ] = datum;
00145                 running_index %= size_fifo;
00146         }
00147 
00152         void fill( const T & value ) {
00153                 for( unsigned int i=0; i<size_fifo; i++ ) data[i] = value;
00154         }
00155 
00160         unsigned int get_idx() { return running_index;}
00161 
00167         void resize( unsigned int length ) ecl_assert_throw_decl(StandardException) {
00168                 size_fifo = length;
00169                 ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
00170                 data.resize( size_fifo );
00171         }
00172 
00173 private:
00174         ecl::Array <T>data;
00175         unsigned int size_fifo;
00176         unsigned int running_index;
00177 };
00178 
00179 } // namespace ecl
00180 
00181 #endif /* ECL_CONTAINERS_FIFO_HPP_ */


ecl_containers
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:12:25