Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1/***********************************************************************/
25#ifndef RINGBUFER_H
26#define RINGBUFER_H
27
29template <class datatype, unsigned size> class RingBuffer
30 {
31public:
33 {
34 pointer = 0;
35 for( unsigned i=0; i<size; ++i)
36 values[i] = initial_value;
37 }
38
40 {
41 return values[map(point)];
42 }
43
44 datatype getPreviousAt(unsigned position)
45 {
46 return values[mapback(position)];
47 }
48
49 inline datatype operator [] (unsigned position)
50 {
51 return getValueAt( position);
52 }
53
54 void pushValue(datatype value)
55 {
56 setValueAt(0, value);
57 ++pointer;
58 if (pointer >= size)
59 pointer = 0;
60 }
61
62void setAllValues(const datatype &value)
63 {
64 for( unsigned i=0; i < size; ++i)
65 setValueAt(i, value);
66 }
67
68 unsigned GetSize( void)
69 {
70 return size;
71 }
72private:
73 unsigned map(unsigned pos)
74 {
75 return (pos + pointer) % size;
76 }
77 unsigned mapback(unsigned position)
78 {
79 int index = pointer - position -1;
80 if( index < 0)
81 index += size;
82 return index;
83 }
84 void setValueAt(unsigned point, const datatype &value)
85 {
86 values[map(point)] = value;
87 }
88
89 unsigned pointer;
90 datatype values[size];
91 };
92
93#endif
ring buffer helper class (template)
Definition ringbuffer.h:30
datatype operator[](unsigned position)
Definition ringbuffer.h:49
datatype getValueAt(unsigned point)
Definition ringbuffer.h:39
void setAllValues(const datatype &value)
Definition ringbuffer.h:62
datatype getPreviousAt(unsigned position)
Definition ringbuffer.h:44
unsigned GetSize(void)
Definition ringbuffer.h:68
RingBuffer(datatype initial_value=datatype())
Definition ringbuffer.h:32
void pushValue(datatype value)
Definition ringbuffer.h:54
mathematical vector of arbitrary type and size
Definition vector.h:40