-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton_storage.hpp
103 lines (93 loc) · 2.88 KB
/
button_storage.hpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include <array>
#include <cstddef>
namespace Storage {
/**
* @brief Represents a button's binding to the event manger.
*/
struct Binding {
bool valid; ///< Set true if the binding was valid. i.e enough space in container.
size_t button_index; ///< The index the button is assigned.
size_t event_group_index; ///< The event group index the button should receive.
};
/// @brief The storage container for button handlers. Stores handles and issues bindings on request.
/// @tparam ptr_type The type of pointer for a butotn.
/// @tparam max_button_count The maximum number of buttons to store.
/// @tparam buttons_per_group The number of buttons on a single event group.
template<typename ptr_type, size_t max_button_count, size_t buttons_per_group>
class ButtonHandler {
public:
using storage = std::array<ptr_type, max_button_count>;
ButtonHandler() : _storage{{nullptr}} {}
/**
* @brief Add an handler to be managed by the container.
*
* @param item The handler to add.
* @return Binding A binding describing the handler position and characteristics in the container.
*/
Binding add(ptr_type item) {
size_t index = 0;
for(auto& entry: _storage) {
if(entry == nullptr) {
entry = item;
return {true, index, index / buttons_per_group};
}
index++;
}
return {false, 0, 0};
}
/**
* @brief Remove a hander by the container.
*
* @param item A previosly added handler
* @return true The handler was removed.
* @return false The hander wasn't found in the container, and not removed.
*/
bool remove(ptr_type item) {
for(auto& entry: _storage) {
if(entry == item) {
entry = nullptr;
return true;
}
}
return false;
}
/**
* @brief Query if the container is empty.
*
* @return true The container is empty.
* @return false The container is not empty
*/
bool empty() const { return size() == 0; }
/**
* @brief Query if the container is full.
*
* @return true The container is full
* @return false The container is not full.
*/
bool full() const { return size() == max_button_count; }
/**
* @brief Query the number of handlers added to the container.
*
* @return size_t The number of handlers
*/
size_t size() const {
size_t count = 0;
for(auto& entry: _storage) {
if(entry != nullptr) {
count++;
}
}
return count;
}
/**
* @brief Overload operator to access items by index
*
* @param index The index to access
* @return ptr_type The handler at that index
*/
ptr_type operator[](const size_t index) { return _storage[index]; }
private:
storage _storage;
};
} // namespace Storage