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