forked from marcdinkum/ringbuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer_main.cpp
80 lines (58 loc) · 2.68 KB
/
ringbuffer_main.cpp
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
#include <iostream>
#include "ringbuffer.h"
//this makes the whole thing need C++17, but the RingBuffer class
//on its own should also be able to work with C++14
template<typename...Args>
auto print(Args... args){
([](auto&& arg){ std::cout << arg << ' '; }(args), ...);
std::cout << '\n';
}
auto main() -> int
{
//could also be RingBuffer<double> or RingBuffer<long double> for more precision,
//To test this, change the using FloatType to double or long double
using FloatType = float;
RingBuffer<FloatType> buffer { 10, "Buffer" };
FloatType inputdata[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
FloatType anadata[8];
buffer.isLockFree() ? print("Lock free") : print("Not lock free");
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
buffer.push(inputdata, 1);
buffer.push(inputdata, 5);
buffer.pop(anadata, 5);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
buffer.push(inputdata, 5);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
buffer.push(inputdata, 1);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
buffer.push(inputdata, 1);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
if(buffer.numItemsAvailableForRead() >= 5)
{
buffer.pop(anadata, 5);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
for(int i = 0; i < 5; ++i) print("Anadata: i =", i, ":", anadata[i]);
}
else print("Not enough data");
buffer.push(inputdata, 1);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
buffer.push(inputdata, 1);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
if(buffer.numItemsAvailableForRead() >= 5)
{
buffer.pop(anadata, 5);
print("Available for write:", buffer.numItemsAvailableForWrite());
print("Available for read:", buffer.numItemsAvailableForRead());
for(int i = 0; i < 5; ++i) print("Anadata: i =", i, ":", anadata[i]);
}
else print("Not enough data\n");
return 0;
}