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 #include <cstddef>
29 
30 namespace fawkes {
31 
32 /** @class CircularBuffer <core/utils/circular_buffer.h>
33  * Circular buffer with a fixed size.
34  * This class provides a a circular buffer.
35  * A circular buffer is a container with a fixed (maximum) size.
36  * It automatically maintains its size by removing elements from the front,
37  * if necessary. This implementation does not allow any element manipulation
38  * other than push_back() and pop_front(). All returned references to elements
39  * are constant.
40  *
41  * @ingroup FCL
42  * @author Till Hofmann
43  */
44 template <typename Type>
46 {
47 public:
48  /** The size_type of the buffer */
49  typedef size_t size_type;
50  /** The CircularBuffer's iterator is a std::deque iterator */
51  typedef typename std::deque<Type>::const_iterator const_iterator;
52  /** iterator is also const, we don't want to manipulate any elements */
54 
55  /** Constructor.
56  * @param n the maximum size of the buffer */
58  {
59  }
60 
61  /** Copy constructor.
62  * @param other CircularBuffer to copy
63  */
65  : deque_(other.get_deque()), max_size_(other.get_max_size())
66  {
67  }
68 
69  /** Destructor. */
71  {
72  }
73 
74  /** Assignment operator.
75  * @param other CircularBuffer to copy
76  * @return reference to this instance
77  */
80  {
81  deque_ = other.get_deque();
82  max_size_ = other.get_max_size();
83  return *this;
84  }
85 
86  /** Insert an element at the end of the buffer
87  * and delete the first element if necessary
88  * @param val the value to insert
89  */
90  void
91  push_back(const Type &val)
92  {
93  if (deque_.size() >= max_size_) {
94  deque_.pop_front();
95  }
96  deque_.push_back(val);
97  }
98 
99  /** Delete the first element */
100  void
102  {
103  deque_.pop_front();
104  }
105 
106  /** Get the maximum size of the buffer
107  * @return the maximum size
108  */
109  size_type
110  get_max_size() const
111  {
112  return max_size_;
113  }
114 
115  /** Get the deque used to store the elements
116  * @return the deque
117  */
118  std::deque<Type>
119  get_deque() const
120  {
121  return deque_;
122  }
123 
124  /** Element access
125  * @param n position of the element
126  * @return reference to the n-th element
127  */
128  const Type &
130  {
131  return deque_[n];
132  }
133 
134  /** Element access
135  * @param n position of the element
136  * @return reference to the n-th element
137  */
138  const Type &
139  at(size_type n) const
140  {
141  return deque_.at(n);
142  }
143 
144  /** Access the first element in the buffer
145  * @return reference to the first element
146  */
147  const Type &
148  front() const
149  {
150  return deque_.front();
151  }
152 
153  /** Access the last element in the buffer
154  * @return reference to the last element
155  */
156  const Type &
157  back() const
158  {
159  return deque_.back();
160  }
161 
162  /** Get iterator to the beginning
163  * @return iterator
164  */
166  begin() const
167  {
168  return deque_.begin();
169  }
170 
171  /** Get iterator to the end
172  * @return iterator
173  */
175  end() const
176  {
177  return deque_.end();
178  }
179 
180  /** Get actual size of the buffer
181  * @return number of elements in the buffer
182  */
183  size_type
184  size() const
185  {
186  return deque_.size();
187  }
188 
189 protected:
190  /** The deque used to store the data */
191  std::deque<Type> deque_;
192  /** The maximum size of the circular buffer */
194 };
195 
196 } // end namespace fawkes
197 
198 #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 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.
size_type get_max_size() const
Get the maximum size of the buffer.
const_iterator begin() const
Get iterator to the beginning.
const_iterator iterator
iterator is also const, we don't want to manipulate any elements
size_t size_type
The size_type of the buffer.
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.