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>
44 class CircularBuffer
45 {
46 public:
47  /** The size_type of the buffer */
48  typedef 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  /** iterator is also const, we don't want to manipulate any elements */
52  typedef const_iterator iterator;
53 
54  /** Constructor.
55  * @param n the maximum size of the buffer */
57  {
58  }
59 
60  /** Copy constructor.
61  * @param other CircularBuffer to copy
62  */
64  : deque_(other.get_deque()), max_size_(other.get_max_size())
65  {
66  }
67 
68  /** Destructor. */
70  {
71  }
72 
73  /** Assignment operator.
74  * @param other CircularBuffer to copy
75  * @return reference to this instance
76  */
78  operator=(const CircularBuffer<Type> &other)
79  {
80  deque_ = other.get_deque();
81  max_size_ = other.get_max_size();
82  return *this;
83  }
84 
85  /** Insert an element at the end of the buffer
86  * and delete the first element if necessary
87  * @param val the value to insert
88  */
89  void
90  push_back(const Type &val)
91  {
92  if (deque_.size() >= max_size_) {
93  deque_.pop_front();
94  }
95  deque_.push_back(val);
96  }
97 
98  /** Delete the first element */
99  void
100  pop_front()
101  {
102  deque_.pop_front();
103  }
104 
105  /** Get the maximum size of the buffer
106  * @return the maximum size
107  */
108  size_type
109  get_max_size() const
110  {
111  return max_size_;
112  }
113 
114  /** Get the deque used to store the elements
115  * @return the deque
116  */
117  std::deque<Type>
118  get_deque() const
119  {
120  return deque_;
121  }
122 
123  /** Element access
124  * @param n position of the element
125  * @return reference to the n-th element
126  */
127  const Type &operator[](size_type n) const
128  {
129  return deque_[n];
130  }
131 
132  /** Element access
133  * @param n position of the element
134  * @return reference to the n-th element
135  */
136  const Type &
137  at(size_type n) const
138  {
139  return deque_.at(n);
140  }
141 
142  /** Access the first element in the buffer
143  * @return reference to the first element
144  */
145  const Type &
146  front() const
147  {
148  return deque_.front();
149  }
150 
151  /** Access the last element in the buffer
152  * @return reference to the last element
153  */
154  const Type &
155  back() const
156  {
157  return deque_.back();
158  }
159 
160  /** Get iterator to the beginning
161  * @return iterator
162  */
164  begin() const
165  {
166  return deque_.begin();
167  }
168 
169  /** Get iterator to the end
170  * @return iterator
171  */
173  end() const
174  {
175  return deque_.end();
176  }
177 
178  /** Get actual size of the buffer
179  * @return number of elements in the buffer
180  */
181  size_type
182  size() const
183  {
184  return deque_.size();
185  }
186 
187 protected:
188  /** The deque used to store the data */
189  std::deque<Type> deque_;
190  /** The maximum size of the circular buffer */
192 };
193 
194 } // end namespace fawkes
195 
196 #endif
fawkes::CircularBuffer::CircularBuffer
CircularBuffer(size_type n)
Constructor.
Definition: circular_buffer.h:62
fawkes::CircularBuffer::at
const Type & at(size_type n) const
Element access.
Definition: circular_buffer.h:143
fawkes::CircularBuffer::size_type
size_t size_type
The size_type of the buffer.
Definition: circular_buffer.h:54
fawkes::CircularBuffer::push_back
void push_back(const Type &val)
Insert an element at the end of the buffer and delete the first element if necessary.
Definition: circular_buffer.h:96
fawkes::CircularBuffer::iterator
const_iterator iterator
iterator is also const, we don't want to manipulate any elements
Definition: circular_buffer.h:58
fawkes::CircularBuffer::max_size_
size_type max_size_
The maximum size of the circular buffer.
Definition: circular_buffer.h:197
fawkes::CircularBuffer::operator=
CircularBuffer< Type > & operator=(const CircularBuffer< Type > &other)
Assignment operator.
Definition: circular_buffer.h:84
fawkes::CircularBuffer::front
const Type & front() const
Access the first element in the buffer.
Definition: circular_buffer.h:152
fawkes::CircularBuffer::const_iterator
std::deque< Type >::const_iterator const_iterator
The CircularBuffer's iterator is a std::deque iterator.
Definition: circular_buffer.h:56
fawkes::CircularBuffer::pop_front
void pop_front()
Delete the first element.
Definition: circular_buffer.h:106
fawkes::CircularBuffer::end
const_iterator end() const
Get iterator to the end.
Definition: circular_buffer.h:179
fawkes
fawkes::CircularBuffer::get_max_size
size_type get_max_size() const
Get the maximum size of the buffer.
Definition: circular_buffer.h:115
fawkes::CircularBuffer::get_deque
std::deque< Type > get_deque() const
Get the deque used to store the elements.
Definition: circular_buffer.h:124
fawkes::CircularBuffer::begin
const_iterator begin() const
Get iterator to the beginning.
Definition: circular_buffer.h:170
fawkes::CircularBuffer::~CircularBuffer
~CircularBuffer()
Destructor.
Definition: circular_buffer.h:75
fawkes::CircularBuffer::operator[]
const Type & operator[](size_type n) const
Element access.
Definition: circular_buffer.h:133
fawkes::CircularBuffer
Definition: circular_buffer.h:50
fawkes::CircularBuffer::size
size_type size() const
Get actual size of the buffer.
Definition: circular_buffer.h:188
fawkes::CircularBuffer::back
const Type & back() const
Access the last element in the buffer.
Definition: circular_buffer.h:161
fawkes::CircularBuffer::deque_
std::deque< Type > deque_
The deque used to store the data.
Definition: circular_buffer.h:195