include
filters
realtime_circular_buffer.hpp
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2008, Willow Garage, Inc.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
*
8
* * Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* * Neither the name of the Willow Garage, Inc. nor the names of its
14
* contributors may be used to endorse or promote products derived from
15
* this software without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
* POSSIBILITY OF SUCH DAMAGE.
28
*/
29
31
32
#ifndef FILTERS_REALTIME_CIRCULAR_BUFFER_HPP_
33
#define FILTERS_REALTIME_CIRCULAR_BUFFER_HPP_
34
35
#include <stdint.h>
36
#include <vector>
37
38
#include <algorithm>
39
#include <boost/circular_buffer.hpp>
40
41
namespace
filters
42
{
43
46
template
<
typename
T>
47
class
RealtimeCircularBuffer
48
{
49
private
:
50
RealtimeCircularBuffer
();
51
52
public
:
53
RealtimeCircularBuffer
(
int
size
,
const
T& default_val):
54
counter_
(0),
cb_
(
size
)
55
{
56
for
(
unsigned
int
i = 0; i <
cb_
.capacity(); i++)
57
{
58
cb_
.push_back(default_val);
59
}
60
};
61
62
void
push_back
(
const
T& item)
63
{
64
if
(
cb_
.capacity() == 0)
return
;
65
66
if
(
counter_
<
cb_
.size())
67
{
68
cb_
[
counter_
] = item;
69
}
70
else
71
cb_
.push_back(item);
72
counter_
++;
73
};
74
75
void
push_front
(
const
T& item)
76
{
77
if
(
cb_
.capacity() == 0)
return
;
78
cb_
.push_front(item);
79
counter_
++;
80
};
81
82
void
clear
() {
counter_
= 0;};
83
84
void
set_capacity
(
unsigned
int
order,
const
T& value);
85
86
T&
front
(){
return
cb_
.front();};
87
T&
back
()
88
{
89
if
(
counter_
<
cb_
.size())
90
return
cb_
[
counter_
];
91
else
92
return
cb_
.back();
93
};
94
95
unsigned
int
size
(){
return
std::min(
counter_
, (
unsigned
int
)
cb_
.size());};
96
bool
empty
(){
return
cb_
.empty();};
97
T&
at
(
size_t
index){
return
cb_
.at(index);};
98
T&
operator[]
(
size_t
index){
return
cb_
[index];}
99
private
:
100
101
unsigned
int
counter_
;
//<! special counter to keep track of first N times through
102
103
boost::circular_buffer<T>
cb_
;
104
};
105
}
//namespace filters
106
#endif //#ifndef REALTIME_CIRCULAR_BUFFER_HPP_
filters
Definition:
filter_base.hpp:38
filters::RealtimeCircularBuffer
A realtime safe circular (ring) buffer.
Definition:
realtime_circular_buffer.hpp:47
filters::RealtimeCircularBuffer::back
T & back()
Definition:
realtime_circular_buffer.hpp:87
filters::RealtimeCircularBuffer::at
T & at(size_t index)
Definition:
realtime_circular_buffer.hpp:97
filters::RealtimeCircularBuffer::push_back
void push_back(const T &item)
Definition:
realtime_circular_buffer.hpp:62
filters::RealtimeCircularBuffer::set_capacity
void set_capacity(unsigned int order, const T &value)
filters::RealtimeCircularBuffer::clear
void clear()
Definition:
realtime_circular_buffer.hpp:82
filters::RealtimeCircularBuffer::RealtimeCircularBuffer
RealtimeCircularBuffer(int size, const T &default_val)
Definition:
realtime_circular_buffer.hpp:53
filters::RealtimeCircularBuffer::push_front
void push_front(const T &item)
Definition:
realtime_circular_buffer.hpp:75
filters::RealtimeCircularBuffer::RealtimeCircularBuffer
RealtimeCircularBuffer()
filters::RealtimeCircularBuffer::operator[]
T & operator[](size_t index)
Definition:
realtime_circular_buffer.hpp:98
filters::RealtimeCircularBuffer::cb_
boost::circular_buffer< T > cb_
Definition:
realtime_circular_buffer.hpp:103
filters::RealtimeCircularBuffer::empty
bool empty()
Definition:
realtime_circular_buffer.hpp:96
filters::RealtimeCircularBuffer::counter_
unsigned int counter_
Definition:
realtime_circular_buffer.hpp:101
filters::RealtimeCircularBuffer::front
T & front()
Definition:
realtime_circular_buffer.hpp:86
filters::RealtimeCircularBuffer::size
unsigned int size()
Definition:
realtime_circular_buffer.hpp:95
filters
Author(s):
autogenerated on Fri Nov 11 2022 03:09:05