Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
system_local_data.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <helios/ecs/details/profile.hpp>
10
11#include <cstddef>
12#include <memory>
13
14namespace helios::ecs {
15
16/// @brief Options for system local data.
18 static constexpr size_t kDefaultPreallocatedSize = 1024 * 1; // 1 KB
20};
21
22/// @brief Local data for a system.
24 mem::ArenaAllocator allocator; ///< local arena allocator
25
26 PmrCmdQueue cmd_queue{&allocator}; ///< local command queue
27 PmrMessageQueue message_queue{&allocator}; ///< local message queue
28
29 /// local consumed messages registry
31 ResourceManager resource_manager; ///< local resource manager
32
33 /**
34 * @brief Creates system local data from system local data options.
35 * @param options System local data options
36 * @return System local data
37 */
38 [[nodiscard]] static SystemLocalData From(
39 SystemLocalDataOptions options = {}) {
40 return SystemLocalData(options);
41 }
42
43 SystemLocalData() = default;
45 : allocator(options.preallocated_size),
49
52 : allocator(std::move(other.allocator)),
56 resource_manager(std::move(other.resource_manager)) {
57 cmd_queue.Merge(std::move(other.cmd_queue));
58 message_queue.Merge(std::move(other.message_queue));
59 consumed_messages.MergeFrom(std::move(other.consumed_messages));
60 }
61 ~SystemLocalData() = default;
62
64 if (this == &other) [[unlikely]] {
65 return *this;
66 }
67 cmd_queue.Clear();
68 message_queue.ClearAll();
69 consumed_messages.Clear();
70 resource_manager.Clear();
71
72 allocator = std::move(other.allocator);
73
74 cmd_queue.Merge(std::move(other.cmd_queue));
75 message_queue.Merge(std::move(other.message_queue));
76 consumed_messages.MergeFrom(std::move(other.consumed_messages));
77 resource_manager = std::move(other.resource_manager);
78
79 return *this;
80 }
82
83 /**
84 * @brief Updates the system local data by executing commands and merging
85 * messages.
86 * @details Call after `World::Flush()` so reserved entities exist before
87 * command execution.
88 * @param world World to update
89 */
90 void Update(World& world) {
91 HELIOS_ECS_PROFILE_SCOPE_N("helios::ecs::SystemLocalData::Update");
92
93 ExecuteCommands(world);
94 MergeMessages(world);
95 ResetArena();
96 }
97
98 /// @brief Clears the system local data.
99 void Clear() { ResetArena(); }
100
101 /**
102 * @brief Checks whether commands or messages are still pending application.
103 * @return True if local commands, messages, or consumed-message bookkeeping
104 * remain unapplied
105 */
106 [[nodiscard]] bool HasPendingWork() const noexcept {
107 return !cmd_queue.Empty() || !consumed_messages.Empty() ||
108 message_queue.MessageCount() > 0;
109 }
110
111 /**
112 * @brief Executes all commands in the local command queue.
113 * @param world World to execute commands on
114 */
115 void ExecuteCommands(World& world) { cmd_queue.ExecuteAll(world); }
116
117 /**
118 * @brief Merges messages from the local message queue into the world message
119 * manager.
120 * @param world World to merge messages into
121 */
122 void MergeMessages(World& world) {
123 auto& message_manager = world.Messages();
124
125 // Remove consumed messages and clear the consumed registry
126 message_manager.Update(consumed_messages);
127 consumed_messages.Clear();
128
129 // Merge local messages and clear the local message queue
130 message_manager.MergeLocalMessages(std::move(message_queue));
131 message_queue.ClearAll();
132 }
133
134 /// @brief Resets the arena allocator and clears all local data.
135 /// @details Polymorphic allocator types are move-constructible but not
136 /// move-assignable, so fresh objects are constructed in place after
137 /// destroying the stale ones whose internal storage was invalidated by
138 /// the arena reset.
139 void ResetArena() noexcept {
140 resource_manager.Clear();
141 allocator.Reset();
142
143 Reconstruct(cmd_queue, &allocator);
144 Reconstruct(message_queue, &allocator);
145 Reconstruct(consumed_messages, &allocator);
146 }
147
148private:
149 template <typename T, typename... Args>
150 static void Reconstruct(T& obj, Args&&... args) {
151 std::destroy_at(&obj);
152 std::construct_at(&obj, std::forward<Args>(args)...);
153 }
154};
155
156} // namespace helios::ecs
void Update(const ConsumedMessagesRegistry< Alloc > &consumed_registry)
Updates message lifecycle — applies consumed messages, swaps buffers, clears old messages.
Definition manager.hpp:377
Resource manager for storing and managing resources of various types.
Definition manager.hpp:21
The World class manages entities with their components and systems.
Definition world.hpp:39
MessageManager & Messages() noexcept
Gets the message manager.
Definition world.hpp:866
PMR arena allocator with lock-free hot allocation path.
MessageQueue< std::pmr::polymorphic_allocator< std::byte > > PmrMessageQueue
Definition queue.hpp:362
helios::ecs::ConsumedMessagesRegistry< std::pmr::polymorphic_allocator< std::byte > > PmrConsumedMessagesRegistry
CmdQueue< std::pmr::polymorphic_allocator< std::byte > > PmrCmdQueue
Command queue for deferred ECS operations that uses a polymorphic allocator.
Definition queue.hpp:174
Options for system local data.
static constexpr size_t kDefaultPreallocatedSize
void Update(World &world)
Updates the system local data by executing commands and merging messages.
mem::ArenaAllocator allocator
local arena allocator
void ResetArena() noexcept
Resets the arena allocator and clears all local data.
bool HasPendingWork() const noexcept
Checks whether commands or messages are still pending application.
SystemLocalData & operator=(SystemLocalData &&other) noexcept
void ExecuteCommands(World &world)
Executes all commands in the local command queue.
PmrConsumedMessagesRegistry consumed_messages
local consumed messages registry
void MergeMessages(World &world)
Merges messages from the local message queue into the world message manager.
SystemLocalData(SystemLocalDataOptions options)
void Clear()
Clears the system local data.
static SystemLocalData From(SystemLocalDataOptions options={})
Creates system local data from system local data options.
SystemLocalData(const SystemLocalData &)=delete
SystemLocalData & operator=(const SystemLocalData &)=delete
PmrMessageQueue message_queue
local message queue
SystemLocalData(SystemLocalData &&other) noexcept
ResourceManager resource_manager
local resource manager
PmrCmdQueue cmd_queue
local command queue