RingBuffer.hpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_RING_BUFFER_HPP_INCLUDED
24 #define ICL_CORE_RING_BUFFER_HPP_INCLUDED
25 
26 namespace icl_core {
27 
28 template <typename T>
30 {
31  if (m_write >= m_read)
32  {
33  return array_range(&m_buffer[m_read], m_write-m_read);
34  }
35  else
36  {
37  return array_range(&m_buffer[m_read], m_buffer.size()-m_read);
38  }
39 }
40 
41 template <typename T>
43 {
44  if (m_write >= m_read)
45  {
46  return const_array_range(&m_buffer[m_read], m_write-m_read);
47  }
48  else
49  {
50  return const_array_range(&m_buffer[m_read], m_buffer.size()-m_read);
51  }
52 }
53 
54 template <typename T>
56 {
57  if (m_write >= m_read)
58  {
59  return array_range(&m_buffer[m_write], 0);
60  }
61  else
62  {
63  return array_range(&m_buffer[0], m_write);
64  }
65 }
66 
67 template <typename T>
69 {
70  if (m_write >= m_read)
71  {
72  return const_array_range(&m_buffer[m_write], 0);
73  }
74  else
75  {
76  return const_array_range(&m_buffer[0], m_write);
77  }
78 }
79 
80 template <typename T>
82 {
83  if (m_write >= m_read)
84  {
85  if (m_read == 0)
86  {
87  return array_range(&m_buffer[m_write], m_buffer.size()-m_write-1);
88  }
89  else
90  {
91  return array_range(&m_buffer[m_write], m_buffer.size()-m_write);
92  }
93  }
94  else
95  {
96  return array_range(&m_buffer[m_write], m_read-m_write-1);
97  }
98 }
99 
100 template <typename T>
102 {
103  if (m_write >= m_read)
104  {
105  if (m_read == 0)
106  {
107  return const_array_range(&m_buffer[m_write], m_buffer.size()-m_write-1);
108  }
109  else
110  {
111  return const_array_range(&m_buffer[m_write], m_buffer.size()-m_write);
112  }
113  }
114  else
115  {
116  return const_array_range(&m_buffer[m_write], m_read-m_write-1);
117  }
118 }
119 
120 template <typename T>
122 {
123  if (m_write >= m_read)
124  {
125  return array_range(&m_buffer[0], (m_read>0?m_read:1) - 1);
126  }
127  else
128  {
129  return array_range(&m_buffer[m_read], 0);
130  }
131 }
132 
133 template <typename T>
135 {
136  if (m_write >= m_read)
137  {
138  return const_array_range(&m_buffer[0], (m_read>0?m_read:1) - 1);
139  }
140  else
141  {
142  return const_array_range(&m_buffer[m_read], 0);
143  }
144 }
145 
146 template <typename T>
148 {
149  if (count >= size())
150  {
151  clear();
152  }
153  else
154  {
155  m_read = (m_read+count) % m_buffer.size();
156  }
157 }
158 
159 template <typename T>
160 void RingBuffer<T>::fakeWrite(size_t count)
161 {
162  if (count > reserve())
163  {
164  count = reserve();
165  }
166  m_write = (m_write + count) % m_buffer.size();
167 }
168 
170 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
171 
175 template <typename T>
177 {
178  clear();
179 }
180 
182 
187 template <typename T>
188 void RingBuffer<T>::Write(const T& val, bool overwrite)
189 {
190  write(val, overwrite);
191 }
192 
194 
199 template <typename T>
200 const T& RingBuffer<T>::At(size_type pos) const
201 {
202  return at(pos);
203 }
204 
206 
211 template <typename T>
213 {
214  return at(pos);
215 }
216 
220 template <typename T>
221 void RingBuffer<T>::Skip()
222 {
223  skip();
224 }
225 
227 
230 template <typename T>
232 {
233  return read();
234 }
235 
239 template <typename T>
241 {
242  return size();
243 }
244 
248 template <typename T>
250 {
251  return capacity();
252 }
253 
255 
260 template <typename T>
262 {
263  setCapacity(capacity);
264 }
265 
266 #endif
267 
269 }
270 
271 #endif
void fakeWrite(size_t count)
Definition: RingBuffer.hpp:160
void skip()
Removes an element from the ring buffer without returning it.
Definition: RingBuffer.h:312
std::vector< T >::size_type size_type
Definition: RingBuffer.h:49
std::pair< const T *, size_type > const_array_range
Array range as in boost::circular_buffer::const_array_range.
Definition: RingBuffer.h:54
array_range arrayOne()
Definition: RingBuffer.hpp:29
array_range emptyArrayOne()
Definition: RingBuffer.hpp:81
array_range arrayTwo()
Definition: RingBuffer.hpp:55
std::pair< T *, size_type > array_range
Array range as in boost::circular_buffer::array_range.
Definition: RingBuffer.h:52
array_range emptyArrayTwo()
Definition: RingBuffer.hpp:121
A simple ring buffer implementation based on std::vector.
Definition: RingBuffer.h:45


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58