Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
queue.h
Go to the documentation of this file.
1
5#ifndef UTILS_QUEUE
6#define UTILS_QUEUE
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11
12namespace lampda {
13namespace utils {
14
15template<typename T, size_t MaxElements = 10> struct Queue
16{
17 static_assert(MaxElements > 1, "Queue: cannot define a one or less elements queue");
18
19 struct Element
20 {
21 uint32_t raisedTime;
22 T _data;
23 };
24
25 bool has_elements() const { return currentStoredEvents > 0; }
26
31 bool enqueue(const T& element)
32 {
33 if (currentStoredEvents >= MaxElements)
34 return false;
35
36 Element e;
37 e._data = element;
38 _queueData[currentStoredEvents] = e;
39
40 currentStoredEvents++;
41 return true;
42 }
43
45 struct Optional
46 {
47 Optional() : _hasValue(false) {}
48 Optional(T val) : _hasValue(true), _value(val) {}
49 bool has_value() const { return _hasValue; }
50 T value() const { return _hasValue ? _value : T(); }
51
52 private:
53 bool _hasValue = false;
54 T _value;
55 };
56
61 {
62 if (currentStoredEvents <= 0)
63 return Optional();
64
65 // store first element
66 const Element toReturn = _queueData[0];
67
68 // move all other elements toward the front
69 for (size_t i = 1; i < currentStoredEvents; i++)
70 {
71 _queueData[i - 1] = _queueData[i];
72 }
73 // remove the element
74 currentStoredEvents -= 1;
75 return Optional(toReturn._data);
76 }
77
78private:
79 size_t currentStoredEvents = 0;
80 std::array<Element, MaxElements> _queueData;
81};
82
83} // namespace utils
84} // namespace lampda
85
86#endif
Program scope.
Definition: control_fixed_modes.hpp:12
Definition: queue.h:20
We need a fake optional, has the core is compiled in C++11.
Definition: queue.h:46
Definition: queue.h:16
Optional dequeue()
Remove and return the first element from the queue, or nothing.
Definition: queue.h:60
bool enqueue(const T &element)
Add an element to the queue.
Definition: queue.h:31