Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
message.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <concepts>
6#include <cstddef>
7#include <cstdint>
8#include <string_view>
9#include <type_traits>
10
11namespace helios::ecs {
12
13/// @brief Type index for messages.
15
16/// @brief Policy for message clearing behavior.
17enum class MessageClearPolicy : uint8_t {
18 kAutomatic, ///< Messages are automatically cleared after double buffer cycle
19 kManual ///< Messages persist until manually cleared
20};
21
22/**
23 * @brief Concept for valid message types.
24 * @details A message must be destructible, move or copy constructible, not
25 * polymorphic, be an object, and have a `static constexpr bool kAsync`
26 * variable set to false or not defined.
27 */
28template <typename T>
29concept MessageTrait =
30 std::destructible<T> &&
31 (std::move_constructible<T> || std::copy_constructible<T>) &&
32 std::is_object_v<std::remove_cvref_t<T>> &&
33 !std::is_polymorphic_v<std::remove_cvref_t<T>> && requires {
34 requires !requires { requires std::remove_cvref_t<T>::kAsync; };
35 };
36
37/**
38 * @brief Concept for valid async message types.
39 * @details An async message trait must be destructible, default initializable,
40 * move constructible and be an object and have a `static constexpr bool kAsync`
41 * variable set to true.
42 */
43template <typename T>
45 std::destructible<T> && std::default_initializable<T> &&
46 std::move_constructible<T> && std::is_object_v<std::remove_cvref_t<T>> &&
47 requires { requires std::remove_cvref_t<T>::kAsync; };
48
49/**
50 * @brief Concept for any valid message type.
51 * @details A message type must satisfy either `MessageTrait` or
52 * `AsyncMessageTrait`.
53 */
54template <typename T>
56
57/**
58 * @brief Concept for messages with custom names.
59 * @details A message with name trait must satisfy `MessageTrait` and provide:
60 * - `static constexpr std::string_view kName` variable
61 */
62template <typename T>
64 { std::remove_cvref_t<T>::kName } -> std::convertible_to<std::string_view>;
65};
66
67/**
68 * @brief Concept for messages with custom clear policy.
69 * @details A message with clear policy trait must satisfy `MessageTrait` and
70 * provide:
71 * - `static constexpr MessageClearPolicy kClearPolicy` variable
72 */
73template <typename T>
75 {
76 std::remove_cvref_t<T>::kClearPolicy
77 } -> std::convertible_to<MessageClearPolicy>;
78};
79
80/**
81 * @brief Concept for consumable message types.
82 * @details A consumable message trait must satisfy `MessageTrait` and have a
83 * `static constexpr bool kConsumable` variable set to true.
84 */
85template <typename T>
87 requires std::remove_cvref_t<T>::kConsumable;
88};
89
90/**
91 * @brief Gets the name of an message.
92 * @details Returns provided name or type name as fallback.
93 * @tparam T Message type
94 * @return Message name
95 */
96template <AnyMessageTrait T>
97[[nodiscard]] constexpr std::string_view MessageNameOf() noexcept {
98 if constexpr (MessageWithNameTrait<T>) {
99 return T::kName;
100 } else {
102 }
103}
104
105/**
106 * @brief Gets the name of an message.
107 * @details Returns provided name or type name as fallback.
108 * @tparam T Message type
109 * @param instance Message instance
110 * @return Message name
111 */
112template <AnyMessageTrait T>
113[[nodiscard]] constexpr std::string_view MessageNameOf(
114 const T& /*instance*/) noexcept {
116}
117
118/**
119 * @brief Gets the clear policy of an message.
120 * @details Returns provided policy or `MessageClearPolicy::kAutomatic` as
121 * default.
122 * @tparam T Message type
123 * @return Message clear policy
124 */
125template <AnyMessageTrait T>
126[[nodiscard]] consteval MessageClearPolicy MessageClearPolicyOf() noexcept {
127 if constexpr (MessageWithClearPolicy<T>) {
128 return T::kClearPolicy;
129 } else {
131 }
132}
133
134/**
135 * @brief Gets the clear policy of an message.
136 * @details Returns provided policy or `MessageClearPolicy::kAutomatic` as
137 * default.
138 * @tparam T Message type
139 * @param instance Message instance
140 * @return Message clear policy
141 */
142template <AnyMessageTrait T>
144 const T& /*instance*/) noexcept {
146}
147
148} // namespace helios::ecs
Concept for any valid message type.
Definition message.hpp:55
Concept for valid async message types.
Definition message.hpp:44
Concept for consumable message types.
Definition message.hpp:86
Concept for valid message types.
Definition message.hpp:29
Concept for messages with custom clear policy.
Definition message.hpp:74
Concept for messages with custom names.
Definition message.hpp:63
consteval MessageClearPolicy MessageClearPolicyOf() noexcept
Gets the clear policy of an message.
Definition message.hpp:126
MessageClearPolicy
Policy for message clearing behavior.
Definition message.hpp:17
@ kAutomatic
Messages are automatically cleared after double buffer cycle.
Definition message.hpp:18
@ kManual
Messages persist until manually cleared.
Definition message.hpp:19
utils::TypeIndex MessageTypeIndex
Type index for messages.
Definition message.hpp:14
constexpr std::string_view MessageNameOf() noexcept
Gets the name of an message.
Definition message.hpp:97
constexpr std::string_view QualifiedTypeNameOf() noexcept
Retrieves the fully qualified type name of T.