Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
async_wrapper.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <string_view>
6#include <type_traits>
7#include <utility>
8
9namespace helios::ecs {
10
11/**
12 * @brief A wrapper around an async message that provides convenient access to
13 * the message's data and type information.
14 * @details Unlike `MessageWrapper` for regular messages, `AsyncMessageWrapper`
15 * owns the message.
16 * @note Thread-safe.
17 * @tparam T The type of the message being wrapped. Must satisfy
18 * `AsyncMessageTrait`
19 */
20template <AsyncMessageTrait T>
22public:
23 /**
24 * @brief Constructs an AsyncMessageWrapper by moving in an message.
25 * @param message Message to wrap (moved)
26 */
27 explicit constexpr AsyncMessageWrapper(T&& message) noexcept(
28 std::is_nothrow_move_constructible_v<T>)
29 : message_(std::move(message)) {}
30 constexpr AsyncMessageWrapper(const AsyncMessageWrapper&) noexcept(
31 std::is_nothrow_copy_constructible_v<T>) = default;
33 std::is_nothrow_move_constructible_v<T>) = default;
34 constexpr ~AsyncMessageWrapper() noexcept(std::is_nothrow_destructible_v<T>) =
35 default;
36
37 constexpr AsyncMessageWrapper& operator=(const AsyncMessageWrapper&) noexcept(
38 std::is_nothrow_copy_assignable_v<T>) = default;
39 constexpr AsyncMessageWrapper& operator=(AsyncMessageWrapper&&) noexcept(
40 std::is_nothrow_move_assignable_v<T>) = default;
41
42 /**
43 * @brief Provides member access to the underlying message (mutable).
44 * @return A pointer to the underlying message data
45 */
46 constexpr T* operator->() noexcept { return &message_; }
47
48 /**
49 * @brief Provides member access to the underlying message (const).
50 * @return A const pointer to the underlying message data
51 */
52 constexpr const T* operator->() const noexcept { return &message_; }
53
54 /**
55 * @brief Dereferences to the underlying message (mutable).
56 * @return A reference to the underlying message data
57 */
58 [[nodiscard]] constexpr T& operator*() noexcept { return message_; }
59
60 /**
61 * @brief Dereferences to the underlying message (const).
62 * @return A const reference to the underlying message data
63 */
64 [[nodiscard]] constexpr const T& operator*() const noexcept {
65 return message_;
66 }
67
68 /**
69 * @brief Gets the name of the message type.
70 * @return A string view containing the name of the message type
71 */
72 [[nodiscard]] constexpr std::string_view Name() const noexcept {
73 return MessageNameOf<T>();
74 }
75
76 /**
77 * @brief Gets the type index of the message.
78 * @return An `MessageTypeIndex` representing the type index of the message
79 */
80 [[nodiscard]] constexpr MessageTypeIndex TypeIndex() const noexcept {
82 }
83
84 /**
85 * @brief Unwraps the message wrapper, returning a mutable reference.
86 * @return A mutable reference to the underlying message data
87 */
88 [[nodiscard]] constexpr T& Unwrap() noexcept { return message_; }
89
90 /**
91 * @brief Unwraps the message wrapper, returning a const reference.
92 * @return A const reference to the underlying message data
93 */
94 [[nodiscard]] constexpr const T& Unwrap() const noexcept { return message_; }
95
96 /**
97 * @brief Moves the message out of the wrapper.
98 * @details After calling this, the wrapper's message is in a moved-from
99 * state.
100 * @return The message, moved out of the wrapper
101 */
102 [[nodiscard]] constexpr T Take() noexcept(
103 std::is_nothrow_move_constructible_v<T>) {
104 return std::move(message_);
105 }
106
107private:
108 T message_; ///< The owned message
109};
110
111} // namespace helios::ecs
constexpr T Take() noexcept(std::is_nothrow_move_constructible_v< T >)
Moves the message out of the wrapper.
constexpr AsyncMessageWrapper(const AsyncMessageWrapper &) noexcept(std::is_nothrow_copy_constructible_v< T >)=default
constexpr AsyncMessageWrapper(T &&message) noexcept(std::is_nothrow_move_constructible_v< T >)
Constructs an AsyncMessageWrapper by moving in an message.
constexpr AsyncMessageWrapper(AsyncMessageWrapper &&) noexcept(std::is_nothrow_move_constructible_v< T >)=default
constexpr std::string_view Name() const noexcept
Gets the name of the message type.
constexpr const T & operator*() const noexcept
Dereferences to the underlying message (const).
constexpr T & Unwrap() noexcept
Unwraps the message wrapper, returning a mutable reference.
constexpr T & operator*() noexcept
Dereferences to the underlying message (mutable).
constexpr const T * operator->() const noexcept
Provides member access to the underlying message (const).
constexpr const T & Unwrap() const noexcept
Unwraps the message wrapper, returning a const reference.
constexpr MessageTypeIndex TypeIndex() const noexcept
Gets the type index of the message.
static constexpr TypeIndex From() noexcept
Constructs a TypeIndex from a type T.
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
STL namespace.