Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::details::EventQueue Class Reference

Queue for managing multiple event types. More...

#include <event_queue.hpp>

Public Member Functions

 EventQueue ()=default
 
 EventQueue (const EventQueue &)=delete
 
 EventQueue (EventQueue &&) noexcept=default
 
 ~EventQueue ()=default
 
EventQueueoperator= (const EventQueue &)=delete
 
EventQueueoperator= (EventQueue &&) noexcept=default
 
template<EventTrait T>
void Register ()
 Registers an event type with the queue.
 
void Clear () noexcept
 Clears all events from the queue and removes registrations.
 
void ClearData () noexcept
 Clears all event data but preserves registrations.
 
template<EventTrait T>
void Clear ()
 Clears events of a specific type.
 
void ClearByTypeId (EventTypeId type_id)
 Clears events of a specific type by type ID (runtime).
 
void Merge (EventQueue &other)
 Merges events from another EventQueue into this one.
 
template<EventTrait T>
void Write (const T &event)
 Writes a single event to the queue.
 
template<std::ranges::sized_range R>
requires EventTrait<std::ranges::range_value_t<R>>
void WriteBulk (const R &events)
 Writes multiple events to the queue in bulk.
 
template<EventTrait T>
auto Read () const -> std::span< const T >
 Reads all events of a specific type from the queue.
 
template<EventTrait T, typename OutIt >
requires std::output_iterator<OutIt, T>
void ReadInto (OutIt out) const
 Reads events of a specific type into a provided output iterator.
 
template<EventTrait T>
bool IsRegistered () const
 Checks if an event type is registered.
 
template<EventTrait T>
bool HasEvents () const
 Checks if events of a specific type exist in the queue.
 
bool Empty () const noexcept
 Checks if the queue is empty.
 
size_t TypeCount () const noexcept
 Gets the number of event types stored.
 
size_t TotalSizeBytes () const noexcept
 Gets the total size of all stored events in bytes.
 

Detailed Description

Queue for managing multiple event types.

Uses an unordered_map to store EventStorage for each event type, allowing efficient type-based event management. Events can be written and read in batches for better performance.

Note
Not thread-safe.

Definition at line 27 of file event_queue.hpp.

Constructor & Destructor Documentation

◆ EventQueue() [1/3]

helios::ecs::details::EventQueue::EventQueue ( )
default

◆ EventQueue() [2/3]

helios::ecs::details::EventQueue::EventQueue ( const EventQueue )
delete

◆ EventQueue() [3/3]

helios::ecs::details::EventQueue::EventQueue ( EventQueue &&  )
defaultnoexcept

◆ ~EventQueue()

helios::ecs::details::EventQueue::~EventQueue ( )
default

Member Function Documentation

◆ Clear() [1/2]

template<EventTrait T>
void helios::ecs::details::EventQueue::Clear ( )
inline

Clears events of a specific type.

Warning
Triggers assertion if event type is not registered.
Template Parameters
TEvent type

Definition at line 164 of file event_queue.hpp.

164 {
165 constexpr EventTypeId type_id = EventTypeIdOf<T>();
166 HELIOS_ASSERT(IsRegistered<T>(), "Failed to clear events: Event type '{}' is not registered!", EventNameOf<T>());
167 storages_.at(type_id).Clear();
168}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140
size_t EventTypeId
Type ID for events.
Definition event.hpp:18
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ Clear() [2/2]

void helios::ecs::details::EventQueue::Clear ( )
inlinenoexcept

Clears all events from the queue and removes registrations.

Definition at line 47 of file event_queue.hpp.

47{ storages_.clear(); }

◆ ClearByTypeId()

void helios::ecs::details::EventQueue::ClearByTypeId ( EventTypeId  type_id)
inline

Clears events of a specific type by type ID (runtime).

Warning
Triggers assertion if event type does not exist.
Parameters
type_idType identifier of events to clear

Definition at line 176 of file event_queue.hpp.

176 {
177 if (const auto it = storages_.find(type_id); it != storages_.end()) {
178 it->second.Clear();
179 }
180}

◆ ClearData()

void helios::ecs::details::EventQueue::ClearData ( )
inlinenoexcept

Clears all event data but preserves registrations.

Use this when you want to clear events without losing type registrations.

Definition at line 170 of file event_queue.hpp.

170 {
171 for (auto& [_, storage] : storages_) {
172 storage.Clear();
173 }
174}

◆ Empty()

bool helios::ecs::details::EventQueue::Empty ( ) const
inlinenoexcept

Checks if the queue is empty.

Returns
True if no events are stored, false otherwise

Definition at line 135 of file event_queue.hpp.

135 {
136 return std::ranges::all_of(storages_, [](const auto& pair) { return pair.second.Empty(); });
137 }

◆ HasEvents()

template<EventTrait T>
bool helios::ecs::details::EventQueue::HasEvents ( ) const
inline

Checks if events of a specific type exist in the queue.

Template Parameters
TEvent type
Returns
True if events of type T exist, false otherwise

Definition at line 245 of file event_queue.hpp.

245 {
246 constexpr EventTypeId type_id = EventTypeIdOf<T>();
247 const auto it = storages_.find(type_id);
248 return it != storages_.end() && !it->second.Empty();
249}

◆ IsRegistered()

template<EventTrait T>
bool helios::ecs::details::EventQueue::IsRegistered ( ) const
inline

Checks if an event type is registered.

Template Parameters
TEvent type
Returns
True if the event type is registered, false otherwise

Definition at line 252 of file event_queue.hpp.

252 {
253 constexpr EventTypeId type_id = EventTypeIdOf<T>();
254 return storages_.contains(type_id);
255}

◆ Merge()

void helios::ecs::details::EventQueue::Merge ( EventQueue other)
inline

Merges events from another EventQueue into this one.

Parameters
otherEventQueue to merge from

Definition at line 182 of file event_queue.hpp.

182 {
183 for (auto& [type_id, other_storage] : other.storages_) {
184 if (other_storage.Empty()) {
185 continue;
186 }
187
188 if (const auto it = storages_.find(type_id); it == storages_.end()) {
189 // If we don't have this type registered, move the entire storage
190 storages_.emplace(type_id, std::move(other_storage));
191 } else {
192 auto& this_storage = it->second;
193 if (this_storage.Empty()) {
194 // If our storage is empty, just move the other storage
195 this_storage = std::move(other_storage);
196 } else {
197 // Append raw bytes from other storage to this storage
198 this_storage.AppendRawBytes(other_storage.Data());
199 }
200 }
201 }
202}

◆ operator=() [1/2]

EventQueue & helios::ecs::details::EventQueue::operator= ( const EventQueue )
delete

◆ operator=() [2/2]

EventQueue & helios::ecs::details::EventQueue::operator= ( EventQueue &&  )
defaultnoexcept

◆ Read()

template<EventTrait T>
auto helios::ecs::details::EventQueue::Read ( ) const -> std::span<const T>
inline

Reads all events of a specific type from the queue.

Warning
Span gets invalidated if storage is modified.
Template Parameters
TEvent type
Returns
Span containing all events of type T

Definition at line 222 of file event_queue.hpp.

222 {
223 constexpr EventTypeId type_id = EventTypeIdOf<T>();
224 const auto it = storages_.find(type_id);
225 if (it == storages_.end()) {
226 return {};
227 }
228
229 return it->second.ReadAll<T>();
230}

◆ ReadInto()

template<EventTrait T, typename OutIt >
requires std::output_iterator<OutIt, T>
void helios::ecs::details::EventQueue::ReadInto ( OutIt  out) const
inline

Reads events of a specific type into a provided output iterator.

Warning
Triggers assertion if event type is not registered.
Template Parameters
TEvent type
OutItOutput iterator type
Parameters
outOutput iterator to write events into

Definition at line 234 of file event_queue.hpp.

234 {
235 constexpr EventTypeId type_id = EventTypeIdOf<T>();
236 const auto it = storages_.find(type_id);
237 if (it == storages_.end()) {
238 return;
239 }
240
241 it->second.ReadInto<T>(std::move(out));
242}

◆ Register()

template<EventTrait T>
void helios::ecs::details::EventQueue::Register ( )
inline

Registers an event type with the queue.

Template Parameters
TEvent type

Definition at line 156 of file event_queue.hpp.

156 {
157 constexpr EventTypeId type_id = EventTypeIdOf<T>();
158 if (!storages_.contains(type_id)) {
159 storages_.emplace(type_id, EventStorage::FromEvent<T>());
160 }
161}

◆ TotalSizeBytes()

size_t helios::ecs::details::EventQueue::TotalSizeBytes ( ) const
inlinenoexcept

Gets the total size of all stored events in bytes.

Returns
Total size in bytes

Definition at line 257 of file event_queue.hpp.

257 {
258 size_t total = 0;
259 for (const auto& [_, storage] : storages_) {
260 total += storage.SizeBytes();
261 }
262 return total;
263}

◆ TypeCount()

size_t helios::ecs::details::EventQueue::TypeCount ( ) const
inlinenoexcept

Gets the number of event types stored.

Returns
Number of distinct event types

Definition at line 143 of file event_queue.hpp.

143{ return storages_.size(); }

◆ Write()

template<EventTrait T>
void helios::ecs::details::EventQueue::Write ( const T &  event)
inline

Writes a single event to the queue.

Warning
Triggers assertion if event type is not registered.
Template Parameters
TEvent type
Parameters
eventEvent to store

Definition at line 205 of file event_queue.hpp.

205 {
206 constexpr EventTypeId type_id = EventTypeIdOf<T>();
207 HELIOS_ASSERT(IsRegistered<T>(), "Failed to write event: Event type '{}' is not registered!", EventNameOf<T>());
208 storages_.at(type_id).Write(event);
209}

◆ WriteBulk()

template<std::ranges::sized_range R>
requires EventTrait<std::ranges::range_value_t<R>>
void helios::ecs::details::EventQueue::WriteBulk ( const R events)
inline

Writes multiple events to the queue in bulk.

Warning
Triggers assertion if event type is not registered.
Template Parameters
RRange of events
Parameters
eventsRange of events to store

Definition at line 213 of file event_queue.hpp.

213 {
214 using EventType = std::ranges::range_value_t<R>;
215 constexpr EventTypeId type_id = EventTypeIdOf<EventType>();
216 HELIOS_ASSERT(IsRegistered<EventType>(), "Failed to write events bulk: Event type '{}' is not registered!",
218 storages_.at(type_id).WriteBulk(events);
219}