realtime_circular_buffer.h
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_H_
33 #define FILTERS_REALTIME_CIRCULAR_BUFFER_H_
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>
48 {
49 private:
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_H_
RealtimeCircularBuffer(int size, const T &default_val)
A realtime safe circular (ring) buffer.
void set_capacity(unsigned int order, const T &value)


filters
Author(s):
autogenerated on Mon Jun 10 2019 13:15:08