File: CircularBuffer.hpp

package info (click to toggle)
indi-bresserexos2 1.0%2B20221223130124-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 3,136 kB
  • sloc: cpp: 2,317; makefile: 2
file content (218 lines) | stat: -rw-r--r-- 4,860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * CircularBuffer.hpp
 *
 * Copyright 2020 Kevin Krüger <kkevin@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

#ifndef _CIRCULARBUFFER_H_INCLUDED_
#define _CIRCULARBUFFER_H_INCLUDED_

#include <cstdint>
#include <cstring>
#include <vector>
#include <iostream>
#include "config.h"

namespace SerialDeviceControl
{

template<typename T, size_t max_size>
class CircularBuffer
{
    public:
        CircularBuffer(T zeroElement) :
            mStart(0),
            mEnd(0),
            mSize(0),
            mZeroElement(zeroElement)
        {
            std::memset(mBuffer, zeroElement, max_size * sizeof(T));
        }

        virtual ~CircularBuffer()
        {

        }

        bool PushFront(T value)
        {
            if(!IsFull())
            {
                Decrement(mStart);
                mBuffer[mStart] = value;
                mSize++;
                return true;
            }

            return false;
        }

        bool PushBack(T value)
        {
            //std::cout << "value " << value << " size " << mSize << " start " << mStart << " end " << mEnd  << std::endl;
            if(!IsFull())
            {
                mBuffer[mEnd] = value;
                Increment(mEnd);
                mSize++;
                return true;
            }

            return false;
        }

        bool PopFront()
        {
            if(!IsEmpty())
            {
                mBuffer[mStart] = mZeroElement;
                Increment(mStart);
                mSize--;
                return true;
            }

            return false;
        }

        bool PopBack()
        {
            if(!IsEmpty())
            {
                Decrement(mEnd);
                mBuffer[mEnd] = mZeroElement;
                mSize--;
                return true;
            }

            return false;
        }

        bool Front(T &returnValue)
        {
            if(!IsEmpty())
            {
                returnValue = mBuffer[mStart];
                return true;
            }

            returnValue = mZeroElement;
            return false;
        }

        bool Back(T &returnValue)
        {
            if(!IsEmpty())
            {
                size_t elementIndex;

                if(mEnd != 0)
                {
                    elementIndex = mEnd - 1;
                }
                else
                {
                    elementIndex = max_size - 1;
                }

                returnValue = mBuffer[elementIndex];
                return true;
            }

            returnValue = mZeroElement;
            return false;
        }

        size_t Size()
        {
            return mSize;
        }

        bool IsEmpty()
        {
            return mSize == 0;
        }

        bool IsFull()
        {
            return mSize == max_size;
        }

        void CopyToVector(std::vector<T> &targetVector)
        {
            for(size_t logicalIndex = 0; logicalIndex < mSize; logicalIndex++)
            {
                size_t actIndex = ActualIndex(logicalIndex);

                targetVector.push_back(mBuffer[actIndex]);
            }
        }

        bool DiscardFront(size_t count)
        {
            bool returnval = false;

            for(size_t i = 0; i < count; i++)
            {
                returnval = PopFront();
            }

            return returnval;
        }

    private:
        size_t mStart;
        size_t mEnd;
        size_t mSize;
        
        T mZeroElement;
        T mBuffer[max_size];

        size_t ActualIndex(size_t logicalIndex)
        {
            if(logicalIndex < (max_size - mStart))
            {
                return mStart + logicalIndex;
            }
            else
            {
                return mStart + (logicalIndex - max_size);
            }
        }

        void Increment(size_t &value)
        {
            if(++value == max_size)
            {
                value = 0;
            }
        }

        void Decrement(size_t &value)
        {
            if(value == 0)
            {
                value = max_size;
            }
            value++;
        }
};
}

#endif