push_and_pop_fixed.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12  ** Ifdefs
13  *****************************************************************************/
14 
15 #ifndef ECL_CONTAINERS_PUSH_AND_POP_FIXED_HPP_
16 #define ECL_CONTAINERS_PUSH_AND_POP_FIXED_HPP_
17 
18 /*****************************************************************************
19  ** Includes
20  *****************************************************************************/
21 
22 #include <iostream> //showMe
23 #include <algorithm> // std::copy
24 #include <ecl/config/macros.hpp>
27 #include "../array.hpp"
28 
29 /*****************************************************************************
30  ** Namespaces
31  *****************************************************************************/
32 
33 namespace ecl
34 {
35 
36 
37 /*****************************************************************************
38 ** Forward Declarations
39 *****************************************************************************/
40 
41 namespace formatters {
42 
43 template <typename Type, size_t N> class PushAndPopFormatter;
44 
45 } // namespace formatters
46 
47 /*****************************************************************************
48  ** Interface
49  *****************************************************************************/
75 template<typename Type, std::size_t Size=DynamicStorage>
77 {
78 public:
82  :
83  size_fifo(Size+1),
84  leader(0),
85  follower(0)
86  {
87  }
88 
90  :
91  size_fifo(Size+1),
92  leader(0),
93  follower(0)
94  {
95  ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
96  fill( d );
97  }
98  virtual ~PushAndPop()
99  {}
100 
101  Type & operator[] (int idx)
102  {
103  return data[ ((follower+idx)%size_fifo) ];
104  }
105 
106  const Type & operator[] (int idx) const
107  {
108  return data[ ((follower+idx)%size_fifo) ];
109  }
110 
111  void operator() (const PushAndPop<Type,Size> & otherOne )
112  {
113  leader = otherOne.leader;
114  follower = otherOne.follower;
115  for( int i=0; i<size_fifo; i++ )
116  {
117  data[i] = otherOne.data[i];
118  }
119  }
120 
127  void push_back( const Type & datum )
128  {
129  data[ leader++ ] = datum;
130  leader %= size_fifo;
131  if( leader == follower )
132  {
133  follower++;
134  follower %= size_fifo;
135  //std::cout << "PushAndPop::push_back : exceeds maximum size, follower catch the leader " << std::endl;
136  }
137  }
138 
139  Type pop_front()
140  {
141  ecl_assert_throw( (follower != leader), StandardException(LOC, OutOfRangeError, "PushAndPop follower==leader"));
142  Type value = data[follower++];
143  follower %= size_fifo;
144  return value;
145  }
146 
147  void fill( const Type & d )
148  {
149  for( unsigned int i=0; i<size_fifo; i++ ) data[i] = d;
150  }
151 
157  unsigned int asize()
158  {
159  return size_fifo;
160  }
161 
162  unsigned int size() const
163  {
164  if( leader > follower ) return leader - follower;
165  else if( leader < follower ) return size_fifo-follower+leader;
166  return 0;
167  }
168 
169  void clear()
170  {
171  follower = 0;
172  leader = 0;
173  }
174 
175  void showMe()
176  {
177  std::cout << "[Size|Leader|Follower]: " << size_fifo << "|" << leader << "|" << follower << std::endl;
178  }
179 
180 private:
182  unsigned int size_fifo;
183  int leader;
184  int follower;
185 };
186 
187 } // namespace ecl
188 
189 #endif /* ECL_CONTAINERS_PUSH_AND_POP_HPP_ */
formatters::PushAndPopFormatter< Type, Size > Formatter
Formatter for this class.
Pseudo formatter for integral type arrays.
Embedded control libraries.
Surpport push and pack operation.
unsigned int size() const
#define LOC
#define ecl_assert_throw(expression, exception)
PushAndPop(const Type &d) ecl_assert_throw_decl(StandardException)
void fill(const Type &d)
void push_back(const Type &datum)
Pushes an element onto the back of the container.
OutOfRangeError
unsigned int asize()
#define ecl_assert_throw_decl(exception)
ecl::Array< Type, Size+1 > data
#define ECL_PUBLIC


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:30