push_and_pop_fixed.hpp
Go to the documentation of this file.
00001 
00011 /*****************************************************************************
00012  ** Ifdefs
00013  *****************************************************************************/
00014 
00015 #ifndef ECL_CONTAINERS_PUSH_AND_POP_FIXED_HPP_
00016 #define ECL_CONTAINERS_PUSH_AND_POP_FIXED_HPP_
00017 
00018 /*****************************************************************************
00019  ** Includes
00020  *****************************************************************************/
00021 
00022 #include <iostream> //showMe
00023 #include <algorithm> // std::copy
00024 #include <ecl/config/macros.hpp>
00025 #include <ecl/errors/compile_time_assert.hpp>
00026 #include <ecl/exceptions/standard_exception.hpp>
00027 #include "../array.hpp"
00028 
00029 /*****************************************************************************
00030  ** Namespaces
00031  *****************************************************************************/
00032 
00033 namespace ecl
00034 {
00035 
00036 
00037 /*****************************************************************************
00038 ** Forward Declarations
00039 *****************************************************************************/
00040 
00041 namespace formatters {
00042 
00043 template <typename Type, size_t N> class PushAndPopFormatter;
00044 
00045 } // namespace formatters
00046 
00047 /*****************************************************************************
00048  ** Interface
00049  *****************************************************************************/
00075 template<typename Type, std::size_t Size=DynamicStorage>
00076 class ECL_PUBLIC PushAndPop
00077 {
00078 public:
00079   typedef formatters::PushAndPopFormatter<Type,Size> Formatter; 
00081   PushAndPop()
00082   :
00083   size_fifo(Size+1),
00084   leader(0),
00085   follower(0)
00086   {
00087   }
00088 
00089   PushAndPop( const Type & d ) ecl_assert_throw_decl(StandardException)
00090   :
00091   size_fifo(Size+1),
00092   leader(0),
00093   follower(0)
00094   {
00095     ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
00096     fill( d );
00097   }
00098   virtual ~PushAndPop()
00099   {}
00100 
00101   Type & operator[] (int idx)
00102   {
00103     return data[ ((follower+idx)%size_fifo) ];
00104   }
00105 
00106   const Type & operator[] (int idx) const
00107   {
00108     return data[ ((follower+idx)%size_fifo) ];
00109   }
00110 
00111   void operator() (const PushAndPop<Type,Size> & otherOne )
00112   {
00113     leader = otherOne.leader;
00114     follower = otherOne.follower;
00115     for( int i=0; i<size_fifo; i++ )
00116     {
00117       data[i] = otherOne.data[i];
00118     }
00119   }
00120 
00127   void push_back( const Type & datum )
00128   {
00129     data[ leader++ ] = datum;
00130     leader %= size_fifo;
00131     if( leader == follower )
00132     {
00133       follower++;
00134       follower %= size_fifo;
00135       //std::cout << "PushAndPop::push_back : exceeds maximum size, follower catch the leader " << std::endl;
00136     }
00137   }
00138 
00139   Type pop_front()
00140   {
00141     ecl_assert_throw( (follower != leader), StandardException(LOC, OutOfRangeError, "PushAndPop follower==leader"));
00142     Type value = data[follower++];
00143     follower %= size_fifo;
00144     return value;
00145   }
00146 
00147   void fill( const Type & d )
00148   {
00149     for( unsigned int i=0; i<size_fifo; i++ ) data[i] = d;
00150   }
00151 
00157   unsigned int asize()
00158   {
00159     return size_fifo;
00160   }
00161 
00162   unsigned int size() const
00163   {
00164     if( leader > follower ) return leader - follower;
00165     else if( leader < follower ) return size_fifo-follower+leader;
00166     return 0;
00167   }
00168 
00169   void clear()
00170   {
00171     follower = 0;
00172     leader = 0;
00173   }
00174 
00175   void showMe()
00176   {
00177     std::cout << "[Size|Leader|Follower]: " << size_fifo << "|" << leader << "|" << follower << std::endl;
00178   }
00179 
00180 private:
00181   ecl::Array <Type,Size+1>data;
00182   unsigned int size_fifo;
00183   int leader;
00184   int follower;
00185 };
00186 
00187 } // namespace ecl
00188 
00189 #endif /* ECL_CONTAINERS_PUSH_AND_POP_HPP_ */


ecl_containers
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:43