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

Type-erased storage for events using a byte vector. More...

#include <event_storage.hpp>

Public Member Functions

 EventStorage (size_t event_size)
 Constructs an EventStorage.
 
 EventStorage (const EventStorage &)=delete
 
 EventStorage (EventStorage &&) noexcept=default
 
 ~EventStorage ()=default
 
EventStorageoperator= (const EventStorage &)=delete
 
EventStorageoperator= (EventStorage &&) noexcept=default
 
void Clear () noexcept
 Clears all stored events.
 
void Reserve (size_t capacity_bytes)
 Reserves space for events.
 
template<EventTrait T>
void Write (const T &event)
 Writes a single event to storage.
 
template<std::ranges::sized_range R>
requires EventTrait<std::ranges::range_value_t<R>>
void WriteBulk (const R &events)
 Writes multiple events to storage in bulk.
 
template<EventTrait T>
auto ReadAll () const noexcept -> std::span< const T >
 Reads all events from storage.
 
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.
 
void AppendRawBytes (std::span< const std::byte > data)
 Appends raw bytes from another EventStorage.
 
bool Empty () const noexcept
 Checks if storage is empty.
 
size_t SizeBytes () const noexcept
 Gets the size of stored data in bytes.
 
auto Data () const noexcept -> std::span< const std::byte >
 Gets raw data (const).
 
size_t EventSize () const noexcept
 Gets the size of each event in bytes.
 

Static Public Member Functions

template<EventTrait T>
static EventStorage FromEvent ()
 Creates an EventStorage for a specific event type.
 

Detailed Description

Type-erased storage for events using a byte vector.

Stores events as raw bytes and provides methods to write/read events with proper type safety through. Events are stored sequentially with their size information for proper deserialization.

Note
Not thread-safe.

Definition at line 25 of file event_storage.hpp.

Constructor & Destructor Documentation

◆ EventStorage() [1/3]

helios::ecs::details::EventStorage::EventStorage ( size_t  event_size)
inlineexplicit

Constructs an EventStorage.

Parameters
event_sizeSize of each event in bytes

Definition at line 31 of file event_storage.hpp.

31: event_size_(event_size) {};
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ EventStorage() [2/3]

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

◆ EventStorage() [3/3]

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

◆ ~EventStorage()

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

Member Function Documentation

◆ AppendRawBytes()

void helios::ecs::details::EventStorage::AppendRawBytes ( std::span< const std::byte >  data)
inline

Appends raw bytes from another EventStorage.

Parameters
dataPointer to raw byte data

Definition at line 197 of file event_storage.hpp.

197 {
198 if (data.empty()) [[unlikely]] {
199 return;
200 }
201
202 const size_t old_size = data_.size();
203 data_.resize(old_size + data.size());
204 std::memcpy(data_.data() + old_size, data.data(), data.size());
205}

◆ Clear()

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

Clears all stored events.

Definition at line 52 of file event_storage.hpp.

52{ data_.clear(); }

◆ Data()

auto helios::ecs::details::EventStorage::Data ( ) const -> std::span<const std::byte>
inlinenoexcept

Gets raw data (const).

Returns
Pointer to raw byte data

Definition at line 123 of file event_storage.hpp.

123{ return {data_.data(), data_.size()}; }

◆ Empty()

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

Checks if storage is empty.

Returns
True if no events are stored, false otherwise

Definition at line 111 of file event_storage.hpp.

111{ return data_.empty(); }

◆ EventSize()

size_t helios::ecs::details::EventStorage::EventSize ( ) const
inlinenoexcept

Gets the size of each event in bytes.

Returns
Event size in bytes

Definition at line 129 of file event_storage.hpp.

129{ return event_size_; }

◆ FromEvent()

template<EventTrait T>
static EventStorage helios::ecs::details::EventStorage::FromEvent ( )
inlinestatic

Creates an EventStorage for a specific event type.

Template Parameters
TEvent type
Returns
EventStorage configured for the event type

Definition at line 42 of file event_storage.hpp.

42 {
43 return EventStorage(sizeof(T));
44 }
EventStorage(size_t event_size)
Constructs an EventStorage.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ ReadAll()

template<EventTrait T>
auto helios::ecs::details::EventStorage::ReadAll ( ) const -> std::span<const T>
inlinenoexcept

Reads all events from storage.

Warning
Triggers assertion if event size doesn't match storage event size. Span gets invalidated if storage is modified.
Template Parameters
TEvent type
Returns
Span containing all events

Definition at line 169 of file event_storage.hpp.

169 {
170 constexpr size_t event_size = sizeof(T);
171 HELIOS_ASSERT(event_size == event_size_, "Failed to write event: Event size misatch, expected '{}', got '{}'!",
172 event_size_, event_size);
173
174 const auto* begin = std::bit_cast<const T*>(data_.data());
175 const auto* end = std::bit_cast<const T*>(data_.data() + data_.size());
176 return {begin, end};
177}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140

◆ ReadInto()

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

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

Note
Thread-safe for read operations.
Warning
Triggers assertion if event size doesn't match storage event size.
Template Parameters
TEvent type
OutItOutput iterator type
Parameters
outOutput iterator to write events into

Definition at line 181 of file event_storage.hpp.

181 {
182 constexpr size_t event_size = sizeof(T);
183 HELIOS_ASSERT(event_size == event_size_, "Failed to write event: Event size misatch, expected '{}', got '{}'!",
184 event_size_, event_size);
185
186 size_t offset = 0;
187 while (offset < data_.size()) {
188 // Read event data using memcpy (safe for trivially copyable types)
189 T event;
190 std::memcpy(&event, data_.data() + offset, event_size);
191 *out++ = std::move(event);
192
194 }
195}

◆ Reserve()

void helios::ecs::details::EventStorage::Reserve ( size_t  capacity_bytes)
inline

Reserves space for events.

Parameters
capacity_bytesCapacity to reserve in bytes

Definition at line 58 of file event_storage.hpp.

58{ data_.reserve(capacity_bytes); }

◆ SizeBytes()

size_t helios::ecs::details::EventStorage::SizeBytes ( ) const
inlinenoexcept

Gets the size of stored data in bytes.

Returns
Size in bytes

Definition at line 117 of file event_storage.hpp.

117{ return data_.size(); }

◆ Write()

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

Writes a single event to storage.

Warning
Triggers assertion if event size doesn't match storage event size.
Template Parameters
TEvent type
Parameters
eventEvent to store

Definition at line 137 of file event_storage.hpp.

137 {
138 constexpr size_t event_size = sizeof(T);
139 HELIOS_ASSERT(event_size == event_size_, "Failed to write event: Event size misatch, expected '{}', got '{}'!",
140 event_size_, event_size);
141
142 const size_t old_size = data_.size();
143 data_.resize(old_size + event_size);
144 std::memcpy(data_.data() + old_size, &event, event_size);
145}

◆ WriteBulk()

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

Writes multiple events to storage in bulk.

Warning
Triggers assertion if event size doesn't match storage event size.
Template Parameters
RRange of events
Parameters
eventsRange of events to store

Definition at line 149 of file event_storage.hpp.

149 {
150 using EventType = std::ranges::range_value_t<R>;
151 constexpr size_t event_size = sizeof(EventType);
152 HELIOS_ASSERT(event_size == event_size_, "Failed to write events bulk: Event size misatch, expected '{}', got '{}'!",
153 event_size_, event_size);
154
155 const size_t old_size = data_.size();
156 const size_t total_bytes = events.size() * event_size;
157
158 data_.resize(old_size + total_bytes);
159
160 size_t offset = old_size;
161 for (const auto& event : events) {
162 // Write event data using memcpy (safe for trivially copyable types)
163 std::memcpy(data_.data() + offset, &event, event_size);
165 }
166}