rtmlib
circularbuffer.h
Go to the documentation of this file.
1 /*
2  * rtmlib is a Real-Time Monitoring Library.
3  *
4  * Copyright (C) 2018-2020 AndrĂ© Pedro
5  *
6  * This file is part of rtmlib.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef _RTML_CIRCULARBUFFER_H_
23 #define _RTML_CIRCULARBUFFER_H_
24 
25 #include <stdio.h>
26 //#include <time.h>
27 
28 #include "debug_compat.h"
29 #include "event.h"
30 
43 template <typename T, size_t N> class RTML_buffer {
44 private:
51  T array[N + 1];
52 
56  size_t top;
57 
61  size_t bottom;
62 
66  bool writer;
67 
71  size_t &operator++() {
72  top = (size_t)(top + 1) % (N + 1);
73  return top;
74  };
75 
79  size_t operator++(int) {
80  size_t tmp(top);
81  operator++();
82  return tmp;
83  };
84 
88  size_t &operator--() {
89  bottom = (size_t)(bottom + 1) % (N + 1);
90  return bottom;
91  };
92 
96  size_t operator--(int) {
97  size_t tmp(bottom);
98  operator--();
99  return tmp;
100  };
101 
102 public:
103  const size_t size = N;
104 
105  typedef T event_t;
106 
107  typedef enum { OK = 0, EMPTY, OVERFLOW, OUT_OF_BOUND } error_t;
108 
112  RTML_buffer();
113 
117  error_t push(const event_t &);
118 
122  error_t pull(event_t &);
123 
127  error_t pop(event_t &);
128 
132  error_t read(event_t &, size_t) const;
133 
137  error_t state(size_t &, size_t &, timespanw &) const;
138 
144  size_t length() const;
145 
149  void debug() const;
150 };
151 
152 template <typename T, size_t N>
153 RTML_buffer<T, N>::RTML_buffer() : top(0), bottom(0), writer(false) {}
154 
155 template <typename T, size_t N>
158 
159  array[top] = node;
160 
161  ++(*this);
162 
163  bool p = top == bottom;
164 
165  if (p)
166  --(*this); // discard one element; the buffer is under a gap
167 
168  DEBUGV3("push-> %d (%d,%d) r:%d\n", length(), bottom, top, p);
169 
170  return (p) ? OVERFLOW : OK;
171 }
172 
173 template <typename T, size_t N>
175  bool c = length() > 0;
176  if (c) {
177  event = array[bottom];
178  --(*this);
179  }
180 
181  DEBUGV3("pull-> %d (%d,%d) r:%d e:%d\n", length(), bottom, top, c,
182  event.getTime());
183 
184  return c ? OK : EMPTY;
185 }
186 
187 template <typename T, size_t N>
189  bool c = length() > 0;
190  if (c) {
191  if (((int)top) - 1 >= 0) {
192  event = array[--top];
193  } else {
194  top = N;
195  event = array[top];
196  }
197  }
198 
199  DEBUGV3("pop-> %d (%d,%d) r:%d e:%d\n", length(), bottom, top, c,
200  event.getTime());
201 
202  return c ? OK : EMPTY;
203 }
204 
205 template <typename T, size_t N>
207 RTML_buffer<T, N>::read(event_t &event, size_t index) const {
208  event = array[index];
209 
210  return index < N + 1 ? ((length() > 0) ? OK : EMPTY) : OUT_OF_BOUND;
211 }
212 
213 template <typename T, size_t N>
215 RTML_buffer<T, N>::state(size_t &b, size_t &t, timespanw &ts) const {
216  b = bottom;
217  t = top;
218  ts = array[bottom].getTime();
219 
220  return OK;
221 }
222 
223 template <typename T, size_t N> size_t RTML_buffer<T, N>::length() const {
224  return (top >= bottom) ? top - bottom : (N + 1) - (bottom - top);
225 }
226 
227 template <typename T, size_t N> void RTML_buffer<T, N>::debug() const {
228  DEBUGV3(" ");
229  for (unsigned int idx = 0; idx < N + 1; idx++)
230  array[idx].debug();
231 
232  DEBUGV3_APPEND("\n");
233 }
234 
235 #endif //_RTML_CIRCULARBUFFER_H_
error_t state(size_t &, size_t &, timespanw &) const
Definition: circularbuffer.h:215
bool writer
Definition: circularbuffer.h:66
size_t & operator++()
Definition: circularbuffer.h:71
void debug() const
Definition: circularbuffer.h:227
Definition: circularbuffer.h:107
size_t operator++(int)
Definition: circularbuffer.h:79
timeabs timespanw
Definition: time_compat.h:94
RTML_buffer()
Definition: circularbuffer.h:153
Definition: circularbuffer.h:43
error_t
Definition: circularbuffer.h:107
Definition: circularbuffer.h:107
T event_t
Definition: circularbuffer.h:105
Definition: circularbuffer.h:107
size_t top
Definition: circularbuffer.h:56
size_t & operator--()
Definition: circularbuffer.h:88
#define DEBUGV3(...)
Definition: debug_compat.h:84
Definition: circularbuffer.h:107
T array[N+1]
Definition: circularbuffer.h:51
const size_t size
Definition: circularbuffer.h:103
error_t push(const event_t &)
Definition: circularbuffer.h:157
size_t operator--(int)
Definition: circularbuffer.h:96
error_t pop(event_t &)
Definition: circularbuffer.h:188
size_t length() const
Definition: circularbuffer.h:223
error_t read(event_t &, size_t) const
Definition: circularbuffer.h:207
#define DEBUGV3_APPEND(...)
Definition: debug_compat.h:86
size_t bottom
Definition: circularbuffer.h:61
error_t pull(event_t &)
Definition: circularbuffer.h:174