Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
commands.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/assert.hpp>
10#include <helios/ecs/world.hpp>
11
12#include <functional>
13#include <memory_resource>
14#include <ranges>
15
16namespace helios::ecs {
17
18/**
19 * @brief Thin wrapper over command queue and world for deferred ECS operations.
20 * @details Provides a convenient interface for spawning, despawning, and
21 * manipulating entities and world state through command buffers.
22 * @note Not thread-safe.
23 */
24class Commands {
25public:
26 constexpr Commands(PmrCmdQueue& queue, World& world,
27 std::pmr::memory_resource* resource =
28 std::pmr::get_default_resource()) noexcept
29 : queue_(queue), world_(world), resource_(resource) {}
30
31 Commands(const Commands&) = delete;
32 Commands(Commands&&) = delete;
33 constexpr ~Commands() = default;
34
35 Commands& operator=(const Commands&) = delete;
37
38 /**
39 * @brief Spawns a new entity and returns a command buffer for it.
40 * @details Reserves a new entity ID and returns a command buffer to schedule
41 * operations on it.
42 * @return Command buffer for the newly reserved entity
43 */
45
46 /**
47 * @brief Enqueues a command to despawn an entity.
48 * @warning Triggers assertion if entity is invalid.
49 * @param entity Entity to despawn
50 */
51 void Despawn(Entity entity) {
52 queue_.get().Enqueue(DestroyEntityCmd(entity));
53 }
54
55 /**
56 * @brief Returns a command buffer for an existing entity.
57 * @warning Triggers assertion if entity is invalid or does not exist in the
58 * world.
59 * @param entity Entity to get command buffer for
60 * @return Command buffer for the entity
61 */
62 [[nodiscard]] constexpr PmrEntityCmdBuffer Entity(Entity entity);
63
64 /**
65 * @brief Returns a command buffer for world-level operations.
66 * @return Command buffer for world operations
67 */
68 [[nodiscard]] constexpr PmrWorldCmdBuffer World() {
69 return {queue_.get(), resource_};
70 }
71
72 /**
73 * @brief Enqueues a command.
74 * @tparam Cmd Command type, must satisfy `CommandTrait`
75 * @param cmd Command to enqueue
76 */
77 template <CommandTrait Cmd>
78 void Enqueue(Cmd&& cmd) {
79 queue_.get().Enqueue(std::forward<Cmd>(cmd));
80 }
81
82 /**
83 * @brief Enqueues multiple commands in bulk.
84 * @tparam R Range type
85 * @param range Range of commands to enqueue
86 */
87 template <std::ranges::input_range R>
89 void EnqueueBulk(R&& range) {
90 queue_.get().EnqueueBulk(std::forward<R>(range));
91 }
92
93private:
94 std::reference_wrapper<PmrCmdQueue> queue_; ///< Command queue reference
95 std::reference_wrapper<class World> world_; ///< World reference
96 std::pmr::memory_resource* resource_; ///< Memory resource for buffers
97};
98
100 auto entity = world_.get().ReserveEntity();
101 return {entity, queue_.get(), resource_};
102}
103
105 HELIOS_ASSERT(entity.Valid(), "Entity '{}' is not valid!", entity);
106 HELIOS_ASSERT(world_.get().Exists(entity), "World does not own entity '{}'!",
107 entity);
108 return {entity, queue_.get(), resource_};
109}
110
111} // namespace helios::ecs
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:259
constexpr ~Commands()=default
void EnqueueBulk(R &&range)
Enqueues multiple commands in bulk.
Definition commands.hpp:89
Commands & operator=(const Commands &)=delete
Commands(Commands &&)=delete
void Despawn(Entity entity)
Enqueues a command to despawn an entity.
Definition commands.hpp:51
constexpr PmrEntityCmdBuffer Entity(Entity entity)
Returns a command buffer for an existing entity.
Definition commands.hpp:104
constexpr PmrWorldCmdBuffer World()
Returns a command buffer for world-level operations.
Definition commands.hpp:68
void Enqueue(Cmd &&cmd)
Enqueues a command.
Definition commands.hpp:78
Commands & operator=(Commands &&)=delete
constexpr Commands(PmrCmdQueue &queue, World &world, std::pmr::memory_resource *resource=std::pmr::get_default_resource()) noexcept
Definition commands.hpp:26
PmrEntityCmdBuffer Spawn()
Spawns a new entity and returns a command buffer for it.
Definition commands.hpp:99
Commands(const Commands &)=delete
Command to destroy a single entity.
Unique identifier for entities with generation counter to handle recycling.
Definition entity.hpp:22
constexpr bool Valid() const noexcept
Checks if the entity is valid.
Definition entity.hpp:64
Concept that defines the requirements for a command trait.
Definition command.hpp:18
EntityCmdBuffer< std::pmr::polymorphic_allocator< std::byte > > PmrEntityCmdBuffer
Command buffer for deferred entity operations that uses a polymorphic allocator.
WorldCmdBuffer< std::pmr::polymorphic_allocator< std::byte > > PmrWorldCmdBuffer
Command buffer for deferred World operations that uses a polymorphic allocator.
CmdQueue< std::pmr::polymorphic_allocator< std::byte > > PmrCmdQueue
Command queue for deferred ECS operations that uses a polymorphic allocator.
Definition queue.hpp:174