NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
RingBuffer.h
Go to the documentation of this file.
1/*
2 * RingBuffer.h
3 *
4 * Copyright (c) 2020-2024 Timo Lappalainen, Kave Oy, www.kave.fi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22*/
23
24/*************************************************************************/
93#ifndef _RING_BUFFER_H_
94#define _RING_BUFFER_H_
95
96#include <cstdint>
97/************************************************************************/
113template <typename T> class tRingBuffer {
114
115protected:
119 uint16_t head;
121 uint16_t tail;
123 uint16_t size;
124
125public:
126 /************************************************************************/
133 tRingBuffer(uint16_t _size);
134 /************************************************************************/
139 virtual ~tRingBuffer();
140 /************************************************************************/
144 uint16_t getSize() const { return size; }
145 /************************************************************************/
151 bool isEmpty() const;
152 /************************************************************************/
154 void clear();
155 /************************************************************************/
159 uint16_t count();
160
161 /************************************************************************/
177
178 /************************************************************************/
188 bool add(const T &val);
189
190 /************************************************************************/
205 const T *getReadRef();
206
207 /************************************************************************/
215 T *peek();
216
217 /************************************************************************/
229 bool read(T &val);
230};
231
232/************************************************************************/
255template <typename T> class tPriorityRingBuffer {
256protected:
258 #define INVALID_RING_REF (uint16_t)(-1)
260 #define INVALID_PRIORITY_REF (uint8_t)(-1)
261
262 /************************************************************************/
270 struct tValueSlot {
274 uint16_t next;
276 uint8_t priority;
277 };
278 /************************************************************************/
284 uint16_t next;
286 uint16_t last;
291 };
292protected:
298 uint16_t head;
300 uint16_t tail;
302 uint16_t size;
305
306public:
307 /************************************************************************/
315 tPriorityRingBuffer(uint16_t _size, uint8_t _maxPriorities=1);
316 /************************************************************************/
321
322 /************************************************************************/
326 uint16_t getSize() const { return size; }
327 /************************************************************************/
337 static uint32_t getMemSize(uint16_t _size, uint8_t _maxPriorities=1);
338 /************************************************************************/
348 bool isEmpty(uint8_t _priority=0xff) const;
349 /************************************************************************/
351 void clear();
352 /************************************************************************/
354 void clean();
355 /************************************************************************/
363 uint16_t count();
364 /************************************************************************/
375 bool add(const T &val, uint8_t _priority=0);
376 /************************************************************************/
388 bool read(T &val);
389
390 /************************************************************************/
406 T *getAddRef(uint8_t _priority=0);
407
408 /************************************************************************/
424 const T *getReadRef(uint8_t _priority);
425
426 /************************************************************************/
443 const T *getReadRef(uint8_t *_priority=0);
444};
445
446
447#include "RingBuffer.tpp"
448
449#endif
#define INVALID_RING_REF
Constant for an invalid index reference for the buffer.
Definition: RingBuffer.h:258
Template Class that holds values with given priorities in a ring buffer.
Definition: RingBuffer.h:255
bool isEmpty(uint8_t _priority=0xff) const
Check is ring buffer empty.
tPriorityRef * priorityReferencies
Pointer to priority references in buffer.
Definition: RingBuffer.h:296
uint16_t size
Number of values that can be stored in the ring buffer.
Definition: RingBuffer.h:302
void clean()
This function does nothing at the moment!
uint16_t getSize() const
Get the size of the ring buffer.
Definition: RingBuffer.h:326
uint8_t maxPriorities
highest priority possible
Definition: RingBuffer.h:304
void clear()
Clears the whole ring buffer.
tValueSlot * buffer
Pointer to the ring puffer in memory.
Definition: RingBuffer.h:294
virtual ~tPriorityRingBuffer()
Destroy the priority ring buffer object Frees all memory.
uint16_t count()
Returns the number of values in the ring buffer.
bool add(const T &val, uint8_t _priority=0)
Add a value to the ring buffer with a given priority.
T * getAddRef(uint8_t _priority=0)
Get the pointer to new value added to the ring buffer.
const T * getReadRef(uint8_t _priority)
Get the pointer to value with given priority read out from the ring buffer.
uint16_t tail
Index of the last occupied index in the ring buffer.
Definition: RingBuffer.h:300
static uint32_t getMemSize(uint16_t _size, uint8_t _maxPriorities=1)
Get the size of memory required by ring buffer.
uint16_t head
Index of the first free index in the ring buffer.
Definition: RingBuffer.h:298
bool read(T &val)
Reads highest priority value out from the ring buffer.
const T * getReadRef(uint8_t *_priority=0)
Get pointer to highest priority value read out from the the ring buffer.
tPriorityRingBuffer(uint16_t _size, uint8_t _maxPriorities=1)
Construct a new Priority ring buffer object.
Template Class that holds values in a ring buffer.
Definition: RingBuffer.h:113
uint16_t getSize() const
Get the size of the ring buffer.
Definition: RingBuffer.h:144
bool add(const T &val)
Adds a new value to the ring buffer.
bool read(T &val)
Reads a value out from the ring buffer.
T * getAddRef()
Get the pointer to new value added to the ring buffer.
uint16_t tail
Index of the last value in the ring buffer.
Definition: RingBuffer.h:121
bool isEmpty() const
Checks if the ring buffer is empty.
uint16_t count()
Returns the number of values in the ring buffer.
uint16_t size
Number of values that can be stored in the ring buffer.
Definition: RingBuffer.h:123
virtual ~tRingBuffer()
Destroy the ring buffer object.
T * buffer
Pointer to the ring buffer of values in memory.
Definition: RingBuffer.h:117
uint16_t head
Index of the first value in the ring buffer.
Definition: RingBuffer.h:119
T * peek()
Get pointer to next value to be read out from the ring buffer. Function does not read value out from ...
const T * getReadRef()
Get the pointer to value read out from the ring buffer.
void clear()
Clears the whole ring buffer.
tRingBuffer(uint16_t _size)
Construct a new ring buffer object.
Structure handles meta data for each priority.
Definition: RingBuffer.h:282
void clear()
Initialize all attributes to default.
Definition: RingBuffer.h:290
uint16_t next
Index of the next value for this priority in the ring buffer.
Definition: RingBuffer.h:284
uint16_t last
Index of the last value for this priority in the ring buffer.
Definition: RingBuffer.h:286
tPriorityRef()
Initialize all attributes to default.
Definition: RingBuffer.h:288
Structure that holds the actual value and meta data.
Definition: RingBuffer.h:270
uint16_t next
Index of the next value in the ring buffer (???)
Definition: RingBuffer.h:274
T Value
Value stored in ring buffer.
Definition: RingBuffer.h:272
uint8_t priority
Priority of this value.
Definition: RingBuffer.h:276