Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
5#include <cstddef>
6#include <cstdint>
7#include <limits>
8
9namespace helios::ecs {
10
11/**
12 * @brief Unique identifier for entities with generation counter to handle recycling.
13 * @details Entity uses a combination of index and generation to provide stable references
14 * even when entities are destroyed and their indices are recycled. The generation counter
15 * ensures that old entity references become invalid when the index is reused.
16 *
17 * Memory layout: 32-bit index + 32-bit generation = 64-bit total
18 *
19 * @note This class is thread-safe for all operations.
20 */
21class Entity {
22public:
25
26 static constexpr IndexType kInvalidIndex = std::numeric_limits<IndexType>::max();
27 static constexpr GenerationType kInvalidGeneration = 0;
28
29 /**
30 * @brief Constructs an invalid entity.
31 * @details Creates an entity with invalid index and generation values.
32 */
33 constexpr Entity() noexcept = default;
34
35 /**
36 * @brief Constructs entity with specific index and generation.
37 * @details Private constructor used by entity manager to create valid entities.
38 * @param index The entity index
39 * @param generation The entity generation
40 */
41 constexpr Entity(IndexType index, GenerationType generation) noexcept : index_(index), generation_(generation) {}
42 constexpr Entity(const Entity&) noexcept = default;
43 constexpr Entity(Entity&&) noexcept = default;
45
48
52
53 /**
54 * @brief Checks if the entity is valid.
55 * @details An entity is valid if both its index and generation are not the reserved invalid values.
56 * @return True if entity has valid index and generation, false otherwise
57 */
59 return index_ != kInvalidIndex && generation_ != kInvalidGeneration;
60 }
61
62 /**
63 * @brief Generates a hash value for this entity.
64 * @details Combines index and generation into a 64-bit hash value.
65 * Invalid entities always return hash value of 0.
66 * @return Hash combining generation (high bits) and index (low bits)
67 */
68 [[nodiscard]] constexpr size_t Hash() const noexcept;
69
70 /**
71 * @brief Gets the index component of the entity.
72 * @details The index identifies the entity's storage location in sparse arrays.
73 * @return Entity index, or kInvalidIndex if entity is invalid
74 */
76
77 /**
78 * @brief Gets the generation component of the entity.
79 * @details The generation counter prevents use of stale entity references after recycling.
80 * @return Entity generation, or kInvalidGeneration if entity is invalid
81 */
82 [[nodiscard]] constexpr GenerationType Generation() const noexcept { return generation_; }
83
84private:
85 IndexType index_ = kInvalidIndex; ///< Entity index for storage lookup
86 GenerationType generation_ = kInvalidGeneration; ///< Generation counter for recycling safety
87};
88
89constexpr bool Entity::operator<(const Entity& other) const noexcept {
90 if (index_ != other.index_) {
91 return index_ < other.index_;
92 }
93 return generation_ < other.generation_;
94}
95
96constexpr size_t Entity::Hash() const noexcept {
97 if (!Valid()) [[unlikely]] {
98 return 0;
99 }
100
101 return (static_cast<size_t>(generation_) << (sizeof(size_t) / 2)) | static_cast<size_t>(index_);
102}
103
104} // namespace helios::ecs
105
106/**
107 * @brief Standard hash specialization for Entity.
108 * @details Enables use of Entity as key in standard hash-based containers.
109 */
110template <>
111struct std::hash<helios::ecs::Entity> {
112 constexpr size_t operator()(const helios::ecs::Entity& entity) const noexcept { return entity.Hash(); }
113};
Unique identifier for entities with generation counter to handle recycling.
Definition entity.hpp:21
constexpr bool operator<(const Entity &other) const noexcept
Definition entity.hpp:89
constexpr bool Valid() const noexcept
Checks if the entity is valid.
Definition entity.hpp:58
uint32_t IndexType
Definition entity.hpp:23
constexpr GenerationType Generation() const noexcept
Gets the generation component of the entity.
Definition entity.hpp:82
uint32_t GenerationType
Definition entity.hpp:24
constexpr Entity(const Entity &) noexcept=default
constexpr Entity() noexcept=default
Constructs an invalid entity.
static constexpr GenerationType kInvalidGeneration
Definition entity.hpp:27
constexpr IndexType Index() const noexcept
Gets the index component of the entity.
Definition entity.hpp:75
constexpr Entity(Entity &&) noexcept=default
constexpr size_t Hash() const noexcept
Generates a hash value for this entity.
Definition entity.hpp:96
static constexpr IndexType kInvalidIndex
Definition entity.hpp:26
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481
constexpr size_t operator()(const helios::ecs::Entity &entity) const noexcept
Definition entity.hpp:112