Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::ecs::AsyncMessageReader< T > Class Template Reference

Type-safe, move-based reader for async messages. More...

#include <async_reader.hpp>

Public Types

using size_type = AsyncMessageQueue::size_type

Public Member Functions

 AsyncMessageReader (MessageManager &manager) noexcept
 Constructs an AsyncMessageReader from the message manager.
 AsyncMessageReader (AsyncMessageQueue &async_queue)
 Constructs an AsyncMessageReader by draining messages from the async queue.
 AsyncMessageReader (const AsyncMessageReader &)=delete
 AsyncMessageReader (AsyncMessageReader &&) noexcept=default
 ~AsyncMessageReader () noexcept=default
AsyncMessageReaderoperator= (const AsyncMessageReader &)=delete
AsyncMessageReaderoperator= (AsyncMessageReader &&) noexcept=default
Dequeue () const
 Dequeues a single message from the queue.
bool Dequeue (T &dest) const
 Dequeues a single message from the queue.
template<std::output_iterator< T > It>
size_type Into (It out, size_type max_count=std::numeric_limits< size_type >::max()) const
 Moves messages into an output iterator (dequeues them).
template<typename Action>
requires std::invocable<Action, T&>
void ForEach (const Action &action) const
 Applies an action to every message in the queue (dequeues them).
bool Empty () const noexcept
 Checks if there are no messages.
size_type CountApprox () const noexcept
 Returns an approximate count of messages.

Detailed Description

template<AsyncMessageTrait T>
class helios::ecs::AsyncMessageReader< T >

Type-safe, move-based reader for async messages.

Drains all async messages of type T from the shared AsyncMessageQueue into a local buffer on construction. After draining, the messages are owned by the reader and can be iterated over, moved out, or queried.

Note
Thread-safe.
Template Parameters
TAsync message type satisfying AsyncMessageTrait
for (auto& message : reader) {
Process(std::move(message));
}
// Or move all messages out:
auto messages = reader.TakeAll();

Definition at line 34 of file async_reader.hpp.

Member Typedef Documentation

◆ size_type

template<AsyncMessageTrait T>
using helios::ecs::AsyncMessageReader< T >::size_type = AsyncMessageQueue::size_type

Definition at line 36 of file async_reader.hpp.

Constructor & Destructor Documentation

◆ AsyncMessageReader() [1/4]

template<AsyncMessageTrait T>
helios::ecs::AsyncMessageReader< T >::AsyncMessageReader ( MessageManager & manager)
inlineexplicitnoexcept

Constructs an AsyncMessageReader from the message manager.

Parameters
managerReference to the message manager

Definition at line 42 of file async_reader.hpp.

◆ AsyncMessageReader() [2/4]

template<AsyncMessageTrait T>
helios::ecs::AsyncMessageReader< T >::AsyncMessageReader ( AsyncMessageQueue & async_queue)
inlineexplicit

Constructs an AsyncMessageReader by draining messages from the async queue.

Warning
Triggers assertion if type 'T' is not registered.
Parameters
async_queueReference to the async message queue

Definition at line 51 of file async_reader.hpp.

◆ AsyncMessageReader() [3/4]

template<AsyncMessageTrait T>
helios::ecs::AsyncMessageReader< T >::AsyncMessageReader ( const AsyncMessageReader< T > & )
delete

◆ AsyncMessageReader() [4/4]

template<AsyncMessageTrait T>
helios::ecs::AsyncMessageReader< T >::AsyncMessageReader ( AsyncMessageReader< T > && )
defaultnoexcept

◆ ~AsyncMessageReader()

template<AsyncMessageTrait T>
helios::ecs::AsyncMessageReader< T >::~AsyncMessageReader ( )
defaultnoexcept

Member Function Documentation

◆ CountApprox()

template<AsyncMessageTrait T>
size_type helios::ecs::AsyncMessageReader< T >::CountApprox ( ) const
inlinenodiscardnoexcept

Returns an approximate count of messages.

Returns
Message count

Definition at line 110 of file async_reader.hpp.

◆ Dequeue() [1/2]

template<AsyncMessageTrait T>
T helios::ecs::AsyncMessageReader< T >::Dequeue ( ) const
inline

Dequeues a single message from the queue.

Returns
Dequeued message or default-constructed T if the queue is empty

Definition at line 65 of file async_reader.hpp.

◆ Dequeue() [2/2]

template<AsyncMessageTrait T>
bool helios::ecs::AsyncMessageReader< T >::Dequeue ( T & dest) const
inline

Dequeues a single message from the queue.

Parameters
destReference to store the dequeued message
Returns
True if an message was dequeued and stored in dest, false if the queue was empty (dest is unchanged)

Definition at line 73 of file async_reader.hpp.

◆ Empty()

template<AsyncMessageTrait T>
bool helios::ecs::AsyncMessageReader< T >::Empty ( ) const
inlinenodiscardnoexcept

Checks if there are no messages.

Returns
True if the reader holds no messages, false otherwise

Definition at line 104 of file async_reader.hpp.

◆ ForEach()

template<AsyncMessageTrait T>
requires std::invocable<Action, T&>
template<typename Action>
requires std::invocable<Action, T&>
void helios::ecs::AsyncMessageReader< T >::ForEach ( const Action & action) const
inline

Applies an action to every message in the queue (dequeues them).

Note
It is possible that the queue is modified by other writers while this method is executing, so the number of messages processed may be less than the total number of messages at the time of calling.
Template Parameters
ActionCallable type (const T&) -> void
Parameters
actionAction to apply

Definition at line 124 of file async_reader.hpp.

◆ Into()

template<AsyncMessageTrait T>
template<std::output_iterator< T > It>
size_type helios::ecs::AsyncMessageReader< T >::Into ( It out,
size_type max_count = std::numeric_limits<size_type>::max() ) const
inline

Moves messages into an output iterator (dequeues them).

Template Parameters
ItOutput iterator type
Parameters
outOutput iterator to receive messages
max_countMaximum number of messages to move (default: all messages)
Returns
Number of messages actually dequeued

Definition at line 83 of file async_reader.hpp.

◆ operator=() [1/2]

template<AsyncMessageTrait T>
AsyncMessageReader & helios::ecs::AsyncMessageReader< T >::operator= ( AsyncMessageReader< T > && )
defaultnoexcept

◆ operator=() [2/2]

template<AsyncMessageTrait T>
AsyncMessageReader & helios::ecs::AsyncMessageReader< T >::operator= ( const AsyncMessageReader< T > & )
delete