Fawkes API  Fawkes Development Version
circular_buffer.h
1 
2 /***************************************************************************
3  * circual_buffer.h - Circular buffer
4  *
5  * Created: Fri Aug 15 12:00:42 2014
6  * Copyright 2014 Till Hofmann
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _CORE_UTILS_CIRCULAR_BUFFER_H_
25 #define _CORE_UTILS_CIRCULAR_BUFFER_H_
26 
27 #include <deque>
28 
29 namespace fawkes {
30 
31 /** @class CircularBuffer <core/utils/circular_buffer.h>
32  * Circular buffer with a fixed size.
33  * This class provides a a circular buffer.
34  * A circular buffer is a container with a fixed (maximum) size.
35  * It automatically maintains its size by removing elements from the front,
36  * if necessary. This implementation does not allow any element manipulation
37  * other than push_back() and pop_front(). All returned references to elements
38  * are constant.
39  *
40  * @ingroup FCL
41  * @author Till Hofmann
42  */
43 template <typename Type>
45 {
46 public:
47  /** The size_type of the buffer */
48  typedef std::size_t size_type;
49  /** The CircularBuffer's iterator is a std::deque iterator */
50  typedef typename std::deque<Type>::const_iterator const_iterator;
51  /** The CircularBuffer's reverse iterator is a std::deque reverse iterator */
52  typedef typename std::deque<Type>::const_reverse_iterator const_reverse_iterator;
53  /** iterator is also const, we don't want to manipulate any elements */
55 
56  /** Constructor.
57  * @param n the maximum size of the buffer */
59  {
60  }
61 
62  /** Copy constructor.
63  * @param other CircularBuffer to copy
64  */
66  : deque_(other.get_deque()), max_size_(other.get_max_size())
67  {
68  }
69 
70  /** Destructor. */
72  {
73  }
74 
75  /** Assignment operator.
76  * @param other CircularBuffer to copy
77  * @return reference to this instance
78  */
81  {
82  deque_ = other.get_deque();
83  max_size_ = other.get_max_size();
84  return *this;
85  }
86 
87  /** Insert an element at the end of the buffer
88  * and delete the first element if necessary
89  * @param val the value to insert
90  */
91  void
92  push_back(const Type &val)
93  {
94  if (deque_.size() >= max_size_) {
95  deque_.pop_front();
96  }
97  deque_.push_back(val);
98  }
99 
100  /** Delete the first element */
101  void
103  {
104  deque_.pop_front();
105  }
106 
107  /** Get the maximum size of the buffer
108  * @return the maximum size
109  */
110  size_type
111  get_max_size() const
112  {
113  return max_size_;
114  }
115 
116  /** Get the deque used to store the elements
117  * @return the deque
118  */
119  std::deque<Type>
120  get_deque() const
121  {
122  return deque_;
123  }
124 
125  /** Element access
126  * @param n position of the element
127  * @return reference to the n-th element
128  */
129  const Type &
131  {
132  return deque_[n];
133  }
134 
135  /** Element access
136  * @param n position of the element
137  * @return reference to the n-th element
138  */
139  const Type &
140  at(size_type n) const
141  {
142  return deque_.at(n);
143  }
144 
145  /** Access the first element in the buffer
146  * @return reference to the first element
147  */
148  const Type &
149  front() const
150  {
151  return deque_.front();
152  }
153 
154  /** Access the last element in the buffer
155  * @return reference to the last element
156  */
157  const Type &
158  back() const
159  {
160  return deque_.back();
161  }
162 
163  /** Get iterator to the beginning
164  * @return iterator
165  */
167  begin() const
168  {
169  return deque_.begin();
170  }
171 
172  /** Get iterator to the end
173  * @return iterator
174  */
176  end() const
177  {
178  return deque_.end();
179  }
180 
181  /** Get reverse iterator to the beginning
182  * @return iterator
183  */
185  rbegin() const
186  {
187  return deque_.rbegin();
188  }
189 
190  /** Get reverse iterator to the end
191  * @return iterator
192  */
194  rend() const
195  {
196  return deque_.rend();
197  }
198 
199  /** Get actual size of the buffer
200  * @return number of elements in the buffer
201  */
202  size_type
203  size() const
204  {
205  return deque_.size();
206  }
207 
208 protected:
209  /** The deque used to store the data */
210  std::deque<Type> deque_;
211  /** The maximum size of the circular buffer */
213 };
214 
215 } // end namespace fawkes
216 
217 #endif
Circular buffer with a fixed size.
~CircularBuffer()
Destructor.
CircularBuffer(size_type n)
Constructor.
std::deque< Type >::const_iterator const_iterator
The CircularBuffer's iterator is a std::deque iterator.
const_reverse_iterator rend() const
Get reverse iterator to the end.
const Type & front() const
Access the first element in the buffer.
const_iterator end() const
Get iterator to the end.
const Type & at(size_type n) const
Element access.
size_type max_size_
The maximum size of the circular buffer.
const Type & operator[](size_type n) const
Element access.
const Type & back() const
Access the last element in the buffer.
std::size_t size_type
The size_type of the buffer.
size_type get_max_size() const
Get the maximum size of the buffer.
const_reverse_iterator rbegin() const
Get reverse iterator to the beginning.
const_iterator begin() const
Get iterator to the beginning.
const_iterator iterator
iterator is also const, we don't want to manipulate any elements
std::deque< Type >::const_reverse_iterator const_reverse_iterator
The CircularBuffer's reverse iterator is a std::deque reverse iterator.
size_type size() const
Get actual size of the buffer.
CircularBuffer(const CircularBuffer< Type > &other)
Copy constructor.
std::deque< Type > get_deque() const
Get the deque used to store the elements.
void pop_front()
Delete the first element.
void push_back(const Type &val)
Insert an element at the end of the buffer and delete the first element if necessary.
std::deque< Type > deque_
The deque used to store the data.
CircularBuffer< Type > & operator=(const CircularBuffer< Type > &other)
Assignment operator.
Fawkes library namespace.