Toast Engine
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
15namespace toast {
16
17struct 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
32class TOAST_API EventRegistry {
33 static inline EventRegistry* instance = nullptr;
34 std::unordered_map<std::string_view, const EventInfo*> types;
35
36public:
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
49template<>
50struct 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
58void registerEventTypes() {
59 static const std::array<const toast::EventInfo*, 8> all_types = {
68 };
69 for (const auto* info : all_types) {
70 toast::EventRegistry::registerEvent(info);
71 }
72}
73
Definition reflect_event.hpp:32
Definition reflect.hpp:244
Definition event.hpp:36
Definition reflect_event.hpp:17