Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
writer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <concepts>
7#include <functional>
8#include <memory_resource>
9#include <ranges>
10#include <utility>
11
12namespace helios::ecs {
13
14/**
15 * @brief Type-safe writer for regular messages with a configurable queue
16 * allocator.
17 * @details Messages written through `BasicMessageWriter` are buffered in a
18 * message queue and merged into the global `MessageManager` at sync time when
19 * used from system local data.
20 * @note Not thread-safe.
21 * @tparam T Message type satisfying `MessageTrait`
22 * @tparam Allocator Allocator type for the underlying message queue (default:
23 * `std::allocator<std::byte>`)
24 */
25template <MessageTrait T, typename Allocator = std::allocator<std::byte>>
27public:
28 /**
29 * @brief Constructs a `BasicMessageWriter` that writes to a message queue.
30 * @param queue Reference to the message queue
31 */
32 explicit constexpr BasicMessageWriter(MessageQueue<Allocator>& queue) noexcept
33 : queue_(queue) {}
35 constexpr BasicMessageWriter(BasicMessageWriter&&) noexcept = default;
36 constexpr ~BasicMessageWriter() noexcept = default;
37
38 BasicMessageWriter& operator=(const BasicMessageWriter&) = delete;
39 constexpr BasicMessageWriter& operator=(BasicMessageWriter&&) noexcept =
40 default;
41
42 /**
43 * @brief Writes a single message to the local queue.
44 * @tparam U Message type, must be the same as `T`
45 * @param message Message to write
46 */
47 template <typename U = T>
48 requires std::same_as<std::remove_cvref_t<U>, T>
49 void Write(U&& message) const {
50 queue_.get().Enqueue(std::forward<U>(message));
51 }
52
53 /**
54 * @brief Writes multiple messages to the local queue in bulk.
55 * @tparam R Range of messages
56 * @param messages Range of messages to write
57 */
58 template <std::ranges::input_range R>
59 requires std::same_as<std::ranges::range_value_t<R>, T>
60 void WriteBulk(R&& messages) const {
61 queue_.get().EnqueueBulk(std::forward<R>(messages));
62 }
63
64 /**
65 * @brief Constructs an message in-place and writes it to the local queue.
66 * @tparam Args Constructor argument types
67 * @param args Arguments to forward to the message constructor
68 */
69 template <typename... Args>
70 requires std::constructible_from<T, Args...>
71 void Emplace(Args&&... args) const {
72 queue_.get().Enqueue(T{std::forward<Args>(args)...});
73 }
74
75private:
76 std::reference_wrapper<MessageQueue<Allocator>>
77 queue_; ///< Reference to the message queue
78};
79
80template <MessageTrait T>
83
84/**
85 * @brief Alias for `BasicMessageWriter` bound to a PMR allocator.
86 * @details The canonical message writer type for system parameters. Users who
87 * need a different allocator can use `BasicMessageWriter` directly.
88 * @tparam T Message type satisfying `MessageTrait`
89 *
90 * @code
91 * void MySystem(MessageWriter<DeathEvent> writer) {
92 * writer.Write(DeathEvent{...});
93 * }
94 * @endcode
95 */
96template <MessageTrait T>
98
99} // namespace helios::ecs
Type-safe writer for regular messages with a configurable queue allocator.
Definition writer.hpp:26
constexpr BasicMessageWriter(MessageQueue< Allocator > &queue) noexcept
Constructs a BasicMessageWriter that writes to a message queue.
Definition writer.hpp:32
constexpr BasicMessageWriter(BasicMessageWriter &&) noexcept=default
Queue for managing multiple message types using type-erased contiguous storage.
Definition queue.hpp:26
PmrBasicMessageWriter< T > MessageWriter
Alias for BasicMessageWriter bound to a PMR allocator.
Definition writer.hpp:97
BasicMessageWriter< T, std::pmr::polymorphic_allocator<> > PmrBasicMessageWriter
Definition writer.hpp:81
STL namespace.