Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
5#include <ctti/name.hpp>
6#include <ctti/type_id.hpp>
7
8#include <concepts>
9#include <cstddef>
10#include <cstdint>
11#include <string_view>
12
13namespace helios::ecs {
14
15/**
16 * @brief Type ID for events.
17 */
19
20/**
21 * @brief Policy for event clearing behavior.
22 */
24 kAutomatic, ///< Events are automatically cleared after double buffer cycle
25 kManual ///< Events persist until manually cleared via ManualClear()
26};
27
28/**
29 * @brief Concept for valid event types.
30 * @details A valid event must be:
31 * - Move constructible
32 * - Move assignable
33 * - Trivially copyable (required for safe storage in byte buffers)
34 *
35 * @note Events are stored as raw bytes using memcpy, so they must be trivially copyable.
36 * This means events cannot contain:
37 * - std::string, std::vector, or other types with dynamic allocation
38 * - Virtual functions
39 * - User-defined copy/move constructors/assignment operators
40 *
41 * Use fixed-size arrays (e.g., char[N]) instead of dynamic containers.
42 */
43template <typename T>
44concept EventTrait = std::move_constructible<T> && std::is_move_assignable_v<T> && std::is_trivially_copyable_v<T>;
45
46/**
47 * @brief Concept for events with custom names.
48 * @details An event with name trait must satisfy EventTrait and provide:
49 * - `static constexpr std::string_view GetName() noexcept`
50 */
51template <typename T>
52concept EventWithNameTrait = EventTrait<T> && requires {
53 { T::GetName() } -> std::same_as<std::string_view>;
54};
55
56/**
57 * @brief Concept for events with custom clear policy.
58 * @details An event with clear policy trait must satisfy EventTrait and provide:
59 * - `static constexpr EventClearPolicy GetClearPolicy() noexcept`
60 */
61template <typename T>
62concept EventWithClearPolicy = EventTrait<T> && requires {
63 { T::GetClearPolicy() } -> std::same_as<EventClearPolicy>;
64};
65
66/**
67 * @brief Gets type ID for an event type.
68 * @tparam T Event type
69 * @return Unique type ID for the event
70 */
71template <EventTrait T>
73 return ctti::type_index_of<T>().hash();
74}
75
76/**
77 * @brief Gets the name of an event.
78 * @details Returns provided name or type name as fallback.
79 * @tparam T Event type
80 * @return Event name
81 */
82template <EventTrait T>
83[[nodiscard]] constexpr std::string_view EventNameOf() noexcept {
84 if constexpr (EventWithNameTrait<T>) {
85 return T::GetName();
86 } else {
87 return ctti::name_of<T>();
88 }
89}
90
91/**
92 * @brief Gets the clear policy of an event.
93 * @details Returns provided policy or kAutomatic as default.
94 * @tparam T Event type
95 * @return Event clear policy
96 */
97template <EventTrait T>
99 if constexpr (EventWithClearPolicy<T>) {
100 return T::GetClearPolicy();
101 } else {
103 }
104}
105
106} // namespace helios::ecs
Concept for valid event types.
Definition event.hpp:44
Concept for events with custom clear policy.
Definition event.hpp:62
Concept for events with custom names.
Definition event.hpp:52
constexpr EventTypeId EventTypeIdOf() noexcept
Gets type ID for an event type.
Definition event.hpp:72
constexpr EventClearPolicy EventClearPolicyOf() noexcept
Gets the clear policy of an event.
Definition event.hpp:98
EventClearPolicy
Policy for event clearing behavior.
Definition event.hpp:23
@ kAutomatic
Events are automatically cleared after double buffer cycle.
@ kManual
Events persist until manually cleared via ManualClear()
constexpr std::string_view EventNameOf() noexcept
Gets the name of an event.
Definition event.hpp:83
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481