Main MRPT website
>
C++ reference
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
mrpt
utils
circular_buffer.h
Go to the documentation of this file.
1
/* +---------------------------------------------------------------------------+
2
| The Mobile Robot Programming Toolkit (MRPT) C++ library |
3
| |
4
| http://www.mrpt.org/ |
5
| |
6
| Copyright (C) 2005-2012 University of Malaga |
7
| |
8
| This software was written by the Machine Perception and Intelligent |
9
| Robotics Lab, University of Malaga (Spain). |
10
| Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11
| |
12
| This file is part of the MRPT project. |
13
| |
14
| MRPT is free software: you can redistribute it and/or modify |
15
| it under the terms of the GNU General Public License as published by |
16
| the Free Software Foundation, either version 3 of the License, or |
17
| (at your option) any later version. |
18
| |
19
| MRPT is distributed in the hope that it will be useful, |
20
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
| GNU General Public License for more details. |
23
| |
24
| You should have received a copy of the GNU General Public License |
25
| along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26
| |
27
+---------------------------------------------------------------------------+ */
28
#ifndef circular_buffer_H
29
#define circular_buffer_H
30
31
// Note: This file is included from "stl_extensions.h"
32
33
#include <
mrpt/utils/utils_defs.h
>
34
#include <vector>
35
36
namespace
mrpt
37
{
38
namespace
utils
39
{
40
/** A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the underlying storage.
41
* \ingroup stlext_grp
42
*/
43
template
<
typename
T>
44
class
circular_buffer
45
{
46
private
:
47
std::vector<T>
m_data
;
48
const
size_t
m_size
;
49
size_t
m_next_read
,
m_next_write
;
50
51
public
:
52
circular_buffer
(
const
size_t
size
) :
53
m_data
(size),
54
m_size
(size),
55
m_next_read
(0),
56
m_next_write
(0)
57
{
58
if
(
m_size
<=2)
throw
std::invalid_argument(
"size must be >2"
);
59
}
60
//virtual ~circular_buffer() { }
61
62
/** Insert a copy of the given element in the buffer.
63
* \exception std::out_of_range If the buffer run out of space.
64
*/
65
void
push
(T d) {
66
m_data
[
m_next_write
++]=d;
67
if
(
m_next_write
==
m_size
)
m_next_write
=0;
68
69
if
(
m_next_write
==
m_next_read
)
70
throw
std::out_of_range(
"push: circular_buffer is full"
);
71
}
72
73
/** Insert a reference of the given element in the buffer.
74
* \exception std::out_of_range If the buffer run out of space.
75
*/
76
void
push_ref
(
const
T &d) {
77
m_data
[
m_next_write
++]=d;
78
if
(
m_next_write
==
m_size
)
m_next_write
=0;
79
80
if
(
m_next_write
==
m_next_read
)
81
throw
std::out_of_range(
"push: circular_buffer is full"
);
82
}
83
84
/** Insert an array of elements in the buffer.
85
* \exception std::out_of_range If the buffer run out of space.
86
*/
87
void
push_many
(T *array_elements,
size_t
count) {
88
while
(count--)
89
push
(*array_elements++);
90
}
91
92
/** Retrieve an element from the buffer.
93
* \exception std::out_of_range If the buffer is empty.
94
*/
95
T
pop
() {
96
if
(
m_next_read
==
m_next_write
)
97
throw
std::out_of_range(
"pop: circular_buffer is empty"
);
98
99
const
size_t
i =
m_next_read
++;
100
if
(
m_next_read
==
m_size
)
m_next_read
=0;
101
return
m_data
[i];
102
}
103
104
/** Retrieve an element from the buffer.
105
* \exception std::out_of_range If the buffer is empty.
106
*/
107
void
pop
(T &out_val) {
108
if
(
m_next_read
==
m_next_write
)
109
throw
std::out_of_range(
"pop: circular_buffer is empty"
);
110
111
out_val=
m_data
[
m_next_read
++];
112
if
(
m_next_read
==
m_size
)
m_next_read
=0;
113
}
114
115
/** Pop a number of elements into a user-provided array.
116
* \exception std::out_of_range If the buffer has less elements than requested.
117
*/
118
void
pop_many
(T *out_array,
size_t
count) {
119
while
(count--)
120
pop
(*out_array++);
121
}
122
123
/** Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size of the internal buffer)
124
* \sa capacity
125
*/
126
size_t
size
()
const
{
127
if
(
m_next_write
>=
m_next_read
)
128
return
m_next_write
-
m_next_read
;
129
else
return
m_next_write
+ (
m_size
-
m_next_read
);
130
}
131
132
/** Return the maximum capacity of the buffer.
133
* \sa size
134
*/
135
size_t
capacity
()
const
{
136
return
m_size
;
137
}
138
139
/** The maximum number of elements that can be written ("push") without rising an overflow error.
140
*/
141
size_t
available
()
const
{
142
return
(
capacity
()-
size
())-1;
143
}
144
145
/** Delete all the stored data, if any. */
146
void
clear
() {
147
m_next_write
=
m_next_read
= 0;
148
}
149
150
};
// end class circular_buffer
151
152
}
// End of namespace
153
}
// End of namespace
154
#endif
Page generated by
Doxygen 1.8.3
for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013