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
26 size_t get_stored_item_count() const { return currentStoredEvents; }
27
32 bool enqueue(const T& element)
33 {
34 if (currentStoredEvents >= MaxElements)
35 return false;
36
37 Element e;
38 e._data = element;
39 _queueData[currentStoredEvents] = e;
40
41 currentStoredEvents++;
42 return true;
43 }
44
46 struct Optional
47 {
48 Optional() : _hasValue(false) {}
49 Optional(T val) : _hasValue(true), _value(val) {}
50 bool has_value() const { return _hasValue; }
51 T value() const { return _hasValue ? _value : T(); }
52
53 private:
54 bool _hasValue = false;
55 T _value;
56 };
57
62 {
63 if (currentStoredEvents <= 0)
64 return Optional();
65
66 // store first element
67 const Element toReturn = _queueData[0];
68
69 // move all other elements toward the front
70 for (size_t i = 1; i < currentStoredEvents; i++)
71 {
72 _queueData[i - 1] = _queueData[i];
73 }
74 // remove the element
75 currentStoredEvents -= 1;
76 return Optional(toReturn._data);
77 }
78
79private:
80 size_t currentStoredEvents = 0;
81 std::array<Element, MaxElements> _queueData;
82};
83
84} // namespace common
85} // 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:47
Definition: queue.h:15
bool enqueue(const T &element)
Add an element to the queue.
Definition: queue.h:32
Optional dequeue()
Remove and return the first element from the queue, or nothing.
Definition: queue.h:61