static_circular_buffer.h
Go to the documentation of this file.
1 
26 #ifndef STATICCIRCULARBUFFER_H
27 #define STATICCIRCULARBUFFER_H
28 
29 #include <memory>
30 #include <vector>
40 template <typename T, typename Allocator = std::allocator<T>>
42 {
43 public:
44  typedef std::vector<T, Allocator> VectorType;
45 
46  StaticCircularBuffer(size_t max_size, const T& val, const Allocator& alloc = Allocator())
47  : buffer_(alloc)
48  {
49  set_capacity(max_size, val);
50  }
51 
53  {
54  begin_iterator_ = buffer_.begin();
56  }
57 
62  void clear()
63  {
64  begin_iterator_ = buffer_.begin();
66  full_ = false;
67  }
68 
74  void set_capacity(size_t max_size, const T& val)
75  {
76  buffer_.assign(max_size, val);
77  clear();
78  }
79 
80 
81  size_t capacity() const
82  {
83  return buffer_.size();
84  }
85 
86  size_t size() const
87  {
88  if (full_)
89  return capacity();
90  else if (begin_iterator_ <= end_iterator_)
91  return std::distance(begin_iterator_, end_iterator_);
92  else
93  return buffer_.size() - std::distance(end_iterator_, begin_iterator_);
94  }
95 
96  T& front()
97  {
98  if (!full_ && (begin_iterator_ == end_iterator_))
99  throw std::runtime_error("Buffer is empty");
100  return *begin_iterator_;
101  }
102 
110  {
111 
112  auto old_it = end_iterator_;
113  if (full_)
116 
117  // Buffer at max capacity
119  {
120  full_ = true;
121  }
122  return *old_it;
123  }
124 
128  void pop_front()
129  {
130  if (!full_ && (begin_iterator_ == end_iterator_))
131  throw std::runtime_error("Buffer is empty");
133  full_ = false;
134  }
135 
141  VectorType &getBuffer()
142  {
143  return buffer_;
144  }
145 
146 private:
147  void advance(typename VectorType::iterator& it, size_t distance = 1)
148  {
149  for (size_t i = 0; i < distance; ++i)
150  {
151  it++;
152  if (it == buffer_.end())
153  it = buffer_.begin();
154  }
155  }
156  VectorType buffer_;
157  typename VectorType::iterator begin_iterator_;
158  typename VectorType::iterator end_iterator_;
159  bool full_; //Disambiguates when end == begin because it's empty, or it's full
160 };
161 
162 #endif // STATICCIRCULARBUFFER_H
void advance(typename VectorType::iterator &it, size_t distance=1)
VectorType::iterator begin_iterator_
StaticCircularBuffer(size_t max_size, const T &val, const Allocator &alloc=Allocator())
VectorType & getBuffer()
getBuffer Provides a reference to the internal data structure, use at your own risk.
std::vector< T, Allocator > VectorType
CircularBuffer implementation that does not perform allocations/deallocations outside of the construc...
void pop_front()
pop_front Reduces buffer size by one, advancing the begin iterator
T & push_back()
push_back Increases the buffer size (not capacity) by one, and returns a reference to the last item i...
VectorType::iterator end_iterator_
void set_capacity(size_t max_size, const T &val)
set_capacity Allocates memory for max_size copies of val
void clear()
clear Change the size of the buffer to 0 (not capacity) Only modifies internal iterators ...


pal_statistics
Author(s):
autogenerated on Mon Feb 28 2022 23:04:05