-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhw_iface.h
84 lines (67 loc) · 2.39 KB
/
hw_iface.h
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
#pragma once
#include <cstddef>
#include <array>
#include <bitset>
#include <memory>
#include <type_traits>
#include <utility>
template <class Impl, std::size_t N_INSTANCES>
class HardwarePeripheralWrapper
{
using weak_handle_t = std::weak_ptr<Impl>;
public:
using handle_t = std::shared_ptr<Impl>;
template <class... Args>
static handle_t construct_instance(std::size_t idx, Args&&... args)
{
if (not is_valid_index(idx))
return nullptr;
if (is_constructed(idx))
return nullptr;
auto p = p_instance_from_idx(idx);
if (not p)
return nullptr;
auto ret = handle_t{ new (p) Impl(std::forward<Args>(args)...),
[](Impl* _p){std::destroy_at(_p);} };
instance_handles[idx] = ret;
return ret;
}
[[nodiscard]] static handle_t get_handle(std::size_t idx)
{
return handle_t{get_weak_handle(idx)};
}
[[nodiscard]] static bool is_constructed(std::size_t idx)
{
return is_valid_index(idx) ?
(not get_weak_handle(idx).expired()) : false;
}
[[nodiscard]] static auto is_constructed(void)
{
std::bitset<N_INSTANCES> ret;
for (std::size_t i = 0 ; i < N_INSTANCES ; ++i)
ret[i] = is_constructed(i);
return ret;
}
[[nodiscard]] static constexpr auto is_valid_index(std::size_t idx)
{
return idx < N_INSTANCES;
}
private:
[[nodiscard]] static constexpr std::byte* p_instance_from_idx(std::size_t idx)
{
if (not is_valid_index(idx))
return nullptr;
const auto offset = idx * sizeof(Impl);
return instance_memory + offset;
}
[[nodiscard]] static constexpr weak_handle_t get_weak_handle(std::size_t idx)
{
return instance_handles.at(idx);
}
alignas(std::alignment_of_v<Impl>) static std::byte instance_memory[N_INSTANCES * sizeof(Impl)];
static std::array<weak_handle_t, N_INSTANCES> instance_handles;
};
template <class Impl, std::size_t N_INSTANCES>
alignas(std::alignment_of_v<Impl>) inline std::byte HardwarePeripheralWrapper<Impl, N_INSTANCES>::instance_memory[N_INSTANCES * sizeof(Impl)]{};
template <class Impl, std::size_t N_INSTANCES>
inline std::array<typename HardwarePeripheralWrapper<Impl, N_INSTANCES>::weak_handle_t, N_INSTANCES> HardwarePeripheralWrapper<Impl, N_INSTANCES>::instance_handles{};