Toast Engine
Toggle main menu visibility
Loading...
Searching...
No Matches
reflect_event.hpp
Go to the documentation of this file.
1
8
9
#pragma once
10
#include <asio/serial_port_base.hpp>
11
#include <toast/events/event.hpp>
12
#include <
toast/export.hpp
>
13
#include <toast/reflect/reflect.hpp>
14
15
namespace
toast {
16
17
struct
TOAST_API
EventInfo
{
18
std::string_view name;
19
std::type_index type;
20
21
[[nodiscard]]
22
constexpr
auto
id()
const
-> uint32_t {
23
uint32_t hash = 2166136261u;
24
for
(
char
c : name) {
25
hash ^=
static_cast<
uint8_t
>
(c);
26
hash *= 16777619u;
27
}
28
return
hash;
29
}
30
};
31
32
class
TOAST_API
EventRegistry
{
33
static
inline
EventRegistry
* instance =
nullptr
;
34
std::unordered_map<std::string_view, const EventInfo*> types;
35
36
public
:
37
static
void
registerEvent(
const
EventInfo
* info) { (*instance).types[info->name] = info; }
38
39
[[nodiscard]]
40
static
auto
reflect(std::string_view name) ->
const
EventInfo
* {
41
auto
it = (*instance).types.find(name);
42
return
it != (*instance).types.end() ? it->second :
nullptr
;
43
}
44
};
45
}
46
48
49
template
<>
50
struct
toast::Reflect
<event::_detail::IEvent> {
51
static
constexpr
std::string_view name =
"event::_detail::IEvent"
;
52
inline
static
const
toast::EventInfo
type_info = {
53
.name = name,
54
.type =
typeid
(
event::_detail::IEvent
),
55
};
56
};
57
58
void
registerEventTypes() {
59
static
const
std::array<const toast::EventInfo*, 8> all_types = {
60
&
toast::Reflect<event::_detail::IEvent>::type_info
,
61
&
toast::Reflect<event::_detail::IEvent>::type_info
,
62
&
toast::Reflect<event::_detail::IEvent>::type_info
,
63
&
toast::Reflect<event::_detail::IEvent>::type_info
,
64
&
toast::Reflect<event::_detail::IEvent>::type_info
,
65
&
toast::Reflect<event::_detail::IEvent>::type_info
,
66
&
toast::Reflect<event::_detail::IEvent>::type_info
,
67
&
toast::Reflect<event::_detail::IEvent>::type_info
,
68
};
69
for
(
const
auto
* info : all_types) {
70
toast::EventRegistry::registerEvent(info);
71
}
72
}
73
toast::EventRegistry
Definition
reflect_event.hpp:32
toast::Reflect
Definition
reflect.hpp:244
export.hpp
event::_detail::IEvent
Definition
event.hpp:36
toast::EventInfo
Definition
reflect_event.hpp:17
engine
src
toast
reflect
reflect_event.hpp
Generated by
1.17.0