Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
async_writer.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <concepts>
8#include <ranges>
9#include <utility>
10
11namespace helios::ecs {
12
13/**
14 * @brief Type-safe writer for async messages.
15 * @details Async messages use a lock-free concurrent queue, making writes
16 * thread-safe. This is suitable for messages produced from multiple threads
17 * concurrently (e.g., I/O callbacks, background workers). `AsyncMessageWriter`
18 * is intended to be short-lived (function-scoped).
19 * @note Thread-safe.
20 * @tparam T Async message type satisfying `AsyncMessageTrait`
21 */
22template <AsyncMessageTrait T>
24public:
25 /**
26 * @brief Constructs an `AsyncMessageWriter` from the message manager.
27 * @param manager Mutable reference to the message manager
28 */
29 explicit constexpr AsyncMessageWriter(MessageManager& manager) noexcept
30 : AsyncMessageWriter(manager.AsyncQueue()) {}
31
32 /**
33 * @brief Constructs an `AsyncMessageWriter` that writes to the shared async
34 * message queue.
35 * @param async_queue Reference to the async message queue
36 */
38 : async_queue_(async_queue), token_(async_queue.MakeProducerToken<T>()) {}
41 ~AsyncMessageWriter() noexcept = default;
42
43 AsyncMessageWriter& operator=(const AsyncMessageWriter&) = delete;
44 AsyncMessageWriter& operator=(AsyncMessageWriter&&) noexcept = default;
45
46 /**
47 * @brief Writes a single message to the async queue (move).
48 * @param message Message to write
49 */
50 void Write(T&& message) {
51 async_queue_.get().template Enqueue<T>(token_, std::move(message));
52 }
53
54 /**
55 * @brief Writes a single message to the async queue (copy).
56 * @param message Message to write
57 */
58 void Write(const T& message)
59 requires std::copy_constructible<T>
60 {
61 async_queue_.get().template Enqueue<T>(token_, T{message});
62 }
63
64 /**
65 * @brief Writes multiple messages to the async queue in bulk.
66 * @tparam R Range of messages
67 * @param messages Range of messages to write
68 */
69 template <std::ranges::input_range R>
70 requires std::same_as<std::ranges::range_value_t<R>, T>
71 void WriteBulk(R&& messages) {
72 async_queue_.get().EnqueueBulk(token_, std::forward<R>(messages));
73 }
74
75 /**
76 * @brief Constructs an message in-place and writes it to the async queue.
77 * @tparam Args Constructor argument types
78 * @param args Arguments to forward to the message constructor
79 */
80 template <typename... Args>
81 requires std::constructible_from<T, Args...>
82 void Emplace(Args&&... args) {
83 async_queue_.get().template Enqueue<T>(token_,
84 T{std::forward<Args>(args)...});
85 }
86
87private:
88 std::reference_wrapper<AsyncMessageQueue>
89 async_queue_; ///< Reference to the shared async message queue
91 token_; ///< Per-writer producer token for improved throughput
92};
93
94} // namespace helios::ecs
Async queue for managing multiple message types via lock-free concurrent queues.
constexpr AsyncMessageWriter(MessageManager &manager) noexcept
Constructs an AsyncMessageWriter from the message manager.
AsyncMessageWriter(const AsyncMessageWriter &)=delete
void WriteBulk(R &&messages)
Writes multiple messages to the async queue in bulk.
void Write(const T &message)
Writes a single message to the async queue (copy).
AsyncMessageWriter(AsyncMessageQueue &async_queue)
Constructs an AsyncMessageWriter that writes to the shared async message queue.
void Emplace(Args &&... args)
Constructs an message in-place and writes it to the async queue.
AsyncMessageWriter(AsyncMessageWriter &&) noexcept=default
void Write(T &&message)
Writes a single message to the async queue (move).
Central coordinator for message lifecycle with double buffering and consumed message removal.
Definition manager.hpp:54
moodycamel::ProducerToken AsyncMessageQueueProducerToken