Debian Codegraph @

CircularBuffer.hpp

raw
1 /*
2 * CircularBuffer.hpp
3 *
4 * Copyright 2020 Kevin Krüger <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24 #ifndef _CIRCULARBUFFER_H_INCLUDED_
25 #define _CIRCULARBUFFER_H_INCLUDED_
26
27 #include <cstdint>
28 #include <cstring>
29 #include <vector>
30 #include <iostream>
31 #include "config.h"
32
33 namespace SerialDeviceControl
34 {
35
36 template<typename T, size_t max_size>
37 class CircularBuffer
38 {
39 public:
40 CircularBuffer(T zeroElement) :
41 mStart(0),
42 mEnd(0),
43 mSize(0),
44 mZeroElement(zeroElement)
45 {
46 std::memset(mBuffer, zeroElement, max_size * sizeof(T));
47 }
48
49 virtual ~CircularBuffer()
50 {
51
52 }
53
54 bool PushFront(T value)
55 {
56 if(!IsFull())
57 {
58 Decrement(mStart);
59 mBuffer[mStart] = value;
60 mSize++;
61 return true;
62 }
63
64 return false;
65 }
66
67 bool PushBack(T value)
68 {
69 //std::cout << "value " << value << " size " << mSize << " start " << mStart << " end " << mEnd << std::endl;
70 if(!IsFull())
71 {
72 mBuffer[mEnd] = value;
73 Increment(mEnd);
74 mSize++;
75 return true;
76 }
77
78 return false;
79 }
80
81 bool PopFront()
82 {
83 if(!IsEmpty())
84 {
85 mBuffer[mStart] = mZeroElement;
86 Increment(mStart);
87 mSize--;
88 return true;
89 }
90
91 return false;
92 }
93
94 bool PopBack()
95 {
96 if(!IsEmpty())
97 {
98 Decrement(mEnd);
99 mBuffer[mEnd] = mZeroElement;
100 mSize--;
101 return true;
102 }
103
104 return false;
105 }
106
107 bool Front(T &returnValue)
108 {
109 if(!IsEmpty())
110 {
111 returnValue = mBuffer[mStart];
112 return true;
113 }
114
115 returnValue = mZeroElement;
116 return false;
117 }
118
119 bool Back(T &returnValue)
120 {
121 if(!IsEmpty())
122 {
123 size_t elementIndex;
124
125 if(mEnd != 0)
126 {
127 elementIndex = mEnd - 1;
128 }
129 else
130 {
131 elementIndex = max_size - 1;
132 }
133
134 returnValue = mBuffer[elementIndex];
135 return true;
136 }
137
138 returnValue = mZeroElement;
139 return false;
140 }
141
142 size_t Size()
143 {
144 return mSize;
145 }
146
147 bool IsEmpty()
148 {
149 return mSize == 0;
150 }
151
152 bool IsFull()
153 {
154 return mSize == max_size;
155 }
156
157 void CopyToVector(std::vector<T> &targetVector)
158 {
159 for(size_t logicalIndex = 0; logicalIndex < mSize; logicalIndex++)
160 {
161 size_t actIndex = ActualIndex(logicalIndex);
162
163 targetVector.push_back(mBuffer[actIndex]);
164 }
165 }
166
167 bool DiscardFront(size_t count)
168 {
169 bool returnval = false;
170
171 for(size_t i = 0; i < count; i++)
172 {
173 returnval = PopFront();
174 }
175
176 return returnval;
177 }
178
179 private:
180 size_t mStart;
181 size_t mEnd;
182 size_t mSize;
183
184 T mZeroElement;
185 T mBuffer[max_size];
186
187 size_t ActualIndex(size_t logicalIndex)
188 {
189 if(logicalIndex < (max_size - mStart))
190 {
191 return mStart + logicalIndex;
192 }
193 else
194 {
195 return mStart + (logicalIndex - max_size);
196 }
197 }
198
199 void Increment(size_t &value)
200 {
201 if(++value == max_size)
202 {
203 value = 0;
204 }
205 }
206
207 void Decrement(size_t &value)
208 {
209 if(value == 0)
210 {
211 value = max_size;
212 }
213 value++;
214 }
215 };
216 }
217
218 #endif