Main MRPT website > C++ reference
MRPT logo
circular_buffer.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef circular_buffer_H
10 #define circular_buffer_H
11 
12 #include <vector>
13 #include <stdexcept>
14 
15 namespace mrpt
16 {
17  namespace utils
18  {
19  /** A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the underlying storage.
20  * \ingroup stlext_grp
21  */
22  template <typename T>
24  {
25  private:
26  std::vector<T> m_data;
27  const size_t m_size;
29 
30  public:
31  circular_buffer(const size_t size) :
32  m_data(size),
33  m_size(size),
34  m_next_read(0),
35  m_next_write(0)
36  {
37  if (m_size<=2) throw std::invalid_argument("size must be >2");
38  }
39  //virtual ~circular_buffer() { }
40 
41  /** Insert a copy of the given element in the buffer.
42  * \exception std::out_of_range If the buffer run out of space.
43  */
44  void push(T d) {
45  m_data[m_next_write++]=d;
46  if (m_next_write==m_size) m_next_write=0;
47 
48  if (m_next_write==m_next_read)
49  throw std::out_of_range("push: circular_buffer is full");
50  }
51 
52  /** Insert a reference of the given element in the buffer.
53  * \exception std::out_of_range If the buffer run out of space.
54  */
55  void push_ref(const T &d) {
56  m_data[m_next_write++]=d;
57  if (m_next_write==m_size) m_next_write=0;
58 
59  if (m_next_write==m_next_read)
60  throw std::out_of_range("push: circular_buffer is full");
61  }
62 
63  /** Insert an array of elements in the buffer.
64  * \exception std::out_of_range If the buffer run out of space.
65  */
66  void push_many(T *array_elements, size_t count) {
67  while (count--)
68  push(*array_elements++);
69  }
70 
71  /** Retrieve an element from the buffer.
72  * \exception std::out_of_range If the buffer is empty.
73  */
74  T pop() {
75  if (m_next_read==m_next_write)
76  throw std::out_of_range("pop: circular_buffer is empty");
77 
78  const size_t i = m_next_read++;
79  if (m_next_read==m_size) m_next_read=0;
80  return m_data[i];
81  }
82 
83  /** Retrieve an element from the buffer.
84  * \exception std::out_of_range If the buffer is empty.
85  */
86  void pop(T &out_val) {
87  if (m_next_read==m_next_write)
88  throw std::out_of_range("pop: circular_buffer is empty");
89 
90  out_val=m_data[m_next_read++];
91  if (m_next_read==m_size) m_next_read=0;
92  }
93 
94  /** Pop a number of elements into a user-provided array.
95  * \exception std::out_of_range If the buffer has less elements than requested.
96  */
97  void pop_many(T *out_array, size_t count) {
98  while (count--)
99  pop(*out_array++);
100  }
101 
102  /** Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size of the internal buffer)
103  * \sa capacity
104  */
105  size_t size() const {
106  if (m_next_write>=m_next_read)
107  return m_next_write-m_next_read;
108  else return m_next_write + (m_size-m_next_read);
109  }
110 
111  /** Return the maximum capacity of the buffer.
112  * \sa size
113  */
114  size_t capacity() const {
115  return m_size;
116  }
117 
118  /** The maximum number of elements that can be written ("push") without rising an overflow error.
119  */
120  size_t available() const {
121  return (capacity()-size())-1;
122  }
123 
124  /** Delete all the stored data, if any. */
125  void clear() {
126  m_next_write = m_next_read = 0;
127  }
128 
129  }; // end class circular_buffer
130 
131  } // End of namespace
132 } // End of namespace
133 #endif
void clear()
Delete all the stored data, if any.
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
circular_buffer(const size_t size)
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
void pop(T &out_val)
Retrieve an element from the buffer.
size_t available() const
The maximum number of elements that can be written ("push") without rising an overflow error...
T pop()
Retrieve an element from the buffer.
A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void push(T d)
Insert a copy of the given element in the buffer.
size_t capacity() const
Return the maximum capacity of the buffer.
void push_many(T *array_elements, size_t count)
Insert an array of elements in the buffer.
void push_ref(const T &d)
Insert a reference of the given element in the buffer.



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015