CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
SlotTable.h
Go to the documentation of this file.
1
11
12#pragma once
13
14#include <array>
15#include <cstddef>
16
17namespace cdc::plugin_manager {
18
19template <typename T, std::size_t N>
20struct SlotTable {
21 std::array<T, N> slots{};
22
23 static constexpr std::size_t capacity() { return N; }
24
26 T* lookup(int id)
27 {
28 if (id <= 0 || static_cast<std::size_t>(id) > N) return nullptr;
29 T* s = &slots[static_cast<std::size_t>(id) - 1];
30 return s->used ? s : nullptr;
31 }
32
37 T* allocate(int& out_id)
38 {
39 for (std::size_t i = 0; i < N; ++i) {
40 if (!slots[i].used) {
41 out_id = static_cast<int>(i + 1);
42 return &slots[i];
43 }
44 }
45 out_id = 0;
46 return nullptr;
47 }
48};
49
50} // namespace cdc::plugin_manager
static constexpr std::size_t capacity()
Definition SlotTable.h:23
T * lookup(int id)
1-based lookup. Returns nullptr if id is out of range or slot unused.
Definition SlotTable.h:26
std::array< T, N > slots
Definition SlotTable.h:21
T * allocate(int &out_id)
Definition SlotTable.h:37