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>
76 class ECL_PUBLIC PushAndPop
77 {
78 public:
79  typedef formatters::PushAndPopFormatter<Type,Size> Formatter;
81  PushAndPop()
82  :
83  size_fifo(Size+1),
84  leader(0),
85  follower(0)
86  {
87  }
88 
89  PushAndPop( const Type & d )
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_ */
ecl::PushAndPop::data
ecl::Array< Type, Size+1 > data
Definition: push_and_pop_fixed.hpp:189
ecl::PushAndPop::PushAndPop
PushAndPop()
Definition: push_and_pop_fixed.hpp:89
LOC
#define LOC
ecl::StandardException
ecl::PushAndPop::leader
int leader
Definition: push_and_pop_fixed.hpp:191
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
ecl::PushAndPop::follower
int follower
Definition: push_and_pop_fixed.hpp:192
standard_exception.hpp
ecl::PushAndPop
Surpport push and pack operation.
Definition: push_and_pop_fixed.hpp:82
ecl::Array
Fixed size container with a few bells and whistles.
Definition: array_no_mem_check.hpp:112
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