-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLIFObuf.h
88 lines (76 loc) · 1.5 KB
/
LIFObuf.h
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
/*
LIFObuf.h - It is a simple lightweight LIFO buffer library for the Arduino.
It is written in C++, and can easily be modified to work with other platforms.
It can buffer any fixed size object (ints, floats, structs, etc...).
Created by Pavel Pervushkin, March, 2024.
https://pervu.github.io/
MIT License
*/
#ifndef __LIFObuf__
#define __LIFObuf__
#include <Arduino.h>
template <typename T>
class LIFObuf {
private:
int _top;
size_t _bufferSize;
T* _buffer;
public:
LIFObuf(size_t bufferSize)
{
_top = 0;
_bufferSize = bufferSize;
_buffer = new T[bufferSize];
}
~LIFObuf()
{
if (_buffer != nullptr){
delete[] _buffer;
}
}
bool push(T data)
{
if (_top == _bufferSize) {
return false;
}
else
{
_buffer[_top] = data;
_top++;
return true;
}
}
T pop()
{
if (_top <= 0)
{
return T();
}
else
{
_top--;
T data = _buffer[_top];
return data;
}
}
T at(int index)
{
if (index < _top)
{
return _buffer[index];
}
else
{
return T();
}
}
size_t size()
{
return _top;
}
void clear()
{
_top = 0;
}
};
#endif