Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Types.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21// Standard library
22#include <atomic>
23#include <cstdint>
24#include <ostream>
25#include <limits>
26#include <memory>
27#include <span>
28#include <string>
29#include <vector>
30
31// Project includes
32#include "PackedId.hpp"
33
34namespace esengine {
35
36// =============================================================================
37// Integer Types
38// =============================================================================
39
41using u8 = std::uint8_t;
43using u16 = std::uint16_t;
45using u32 = std::uint32_t;
47using u64 = std::uint64_t;
48
50using i8 = std::int8_t;
52using i16 = std::int16_t;
54using i32 = std::int32_t;
56using i64 = std::int64_t;
57
59using f32 = float;
61using f64 = double;
62
64using usize = std::size_t;
65
66// =============================================================================
67// Smart Pointer Aliases
68// =============================================================================
69
74template<typename T>
75using Unique = std::unique_ptr<T>;
76
81template<typename T>
82using Shared = std::shared_ptr<T>;
83
88template<typename T>
89using Weak = std::weak_ptr<T>;
90
98template<typename T, typename... Args>
99constexpr Unique<T> makeUnique(Args&&... args) {
100 return std::make_unique<T>(std::forward<Args>(args)...);
101}
102
110template<typename T, typename... Args>
111constexpr Shared<T> makeShared(Args&&... args) {
112 return std::make_shared<T>(std::forward<Args>(args)...);
113}
114
115// =============================================================================
116// Entity Types
117// =============================================================================
118
135struct Entity {
137
140 static constexpr u32 GEN_BITS = Layout::GEN_BITS;
142 static constexpr u32 GEN_MASK = Layout::GEN_MASK;
143 static constexpr u32 INVALID_RAW = 0xFFFFFFFF;
144
145 constexpr Entity() : raw(INVALID_RAW) {}
146 explicit constexpr Entity(u32 r) : raw(r) {}
147 explicit constexpr operator u32() const { return raw; }
148
150 constexpr u32 index() const { return Layout::indexOf(raw); }
151
153 constexpr u32 generation() const { return Layout::generationOf(raw); }
154
156 constexpr bool isValid() const { return raw != INVALID_RAW; }
157
159 constexpr u32 id() const { return raw; }
160
162 static constexpr Entity fromRaw(u32 v) { return Entity(v); }
163
165 static constexpr Entity make(u32 idx, u32 gen) { return Entity(Layout::pack(idx, gen)); }
166
167 constexpr bool operator==(Entity other) const { return raw == other.raw; }
168 constexpr bool operator!=(Entity other) const { return raw != other.raw; }
169};
170
171static_assert(sizeof(Entity) == sizeof(u32), "Entity must be 4 bytes for FFI and dense array performance");
172
175
177constexpr u32 INVALID_DENSE_INDEX = std::numeric_limits<u32>::max();
178
179// =============================================================================
180// Type ID System
181// =============================================================================
182
187using TypeId = u32;
188
189namespace detail {
201 template<typename Domain>
202 inline std::atomic<TypeId>& typeIdCounter() {
203 static std::atomic<TypeId> counter{0};
204 return counter;
205 }
206
207 template<typename Domain, typename T>
209 static const TypeId id = typeIdCounter<Domain>().fetch_add(1, std::memory_order_relaxed);
210 return id;
211 }
212} // namespace detail
213
220
230template<typename T>
232
234template<typename T>
236
237// =============================================================================
238// Span Types
239// =============================================================================
240
241template<typename T>
242using ConstSpan = std::span<const T>;
243
244} // namespace esengine
245
246// =============================================================================
247// std::hash specialization
248// =============================================================================
249
250template<>
251struct std::hash<esengine::Entity> {
252 std::size_t operator()(esengine::Entity e) const noexcept {
253 return std::hash<esengine::u32>{}(e.raw);
254 }
255};
256
257inline std::ostream& operator<<(std::ostream& os, esengine::Entity e) {
258 return os << e.raw;
259}
Single source of truth for index+generation bit-packing.
std::ostream & operator<<(std::ostream &os, esengine::Entity e)
Definition Types.hpp:257
TypeId domainTypeId()
Definition Types.hpp:208
std::atomic< TypeId > & typeIdCounter()
One independent TypeId counter per Domain tag.
Definition Types.hpp:202
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
Definition Types.hpp:99
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
TypeId componentTypeId()
Dense 0-based runtime id for a component type (ECS pool index + mask bit).
Definition Types.hpp:231
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
constexpr u32 INVALID_DENSE_INDEX
Sentinel for empty sparse set slots.
Definition Types.hpp:177
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::weak_ptr< T > Weak
Weak reference smart pointer.
Definition Types.hpp:89
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::shared_ptr< T > Shared
Shared ownership smart pointer.
Definition Types.hpp:82
std::int16_t i16
Signed 16-bit integer.
Definition Types.hpp:52
TypeId serviceTypeId()
Dense 0-based runtime id for a service type (ServiceRegistry key).
Definition Types.hpp:235
std::span< const T > ConstSpan
Definition Types.hpp:242
std::unique_ptr< T > Unique
Unique ownership smart pointer.
Definition Types.hpp:75
std::int8_t i8
Signed 8-bit integer.
Definition Types.hpp:50
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
std::int64_t i64
Signed 64-bit integer.
Definition Types.hpp:56
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
double f64
64-bit floating point
Definition Types.hpp:61
u32 TypeId
Runtime type identifier.
Definition Types.hpp:187
constexpr Shared< T > makeShared(Args &&... args)
Creates a Shared pointer with the given arguments.
Definition Types.hpp:111
float f32
32-bit floating point
Definition Types.hpp:59
Domain tags — each yields an independent, dense, 0-based TypeId sequence. Use the typed helpers below...
Definition Types.hpp:218
Entity identifier with packed index + generation.
Definition Types.hpp:135
PackedId< 22, 10 > Layout
Definition Types.hpp:138
constexpr Entity(u32 r)
Definition Types.hpp:146
static constexpr u32 INVALID_RAW
Definition Types.hpp:143
static constexpr u32 GEN_BITS
Definition Types.hpp:140
constexpr bool operator!=(Entity other) const
Definition Types.hpp:168
constexpr u32 id() const
Returns the raw packed value for FFI serialization.
Definition Types.hpp:159
static constexpr u32 GEN_MASK
Definition Types.hpp:142
static constexpr Entity fromRaw(u32 v)
Reconstructs an Entity from a raw packed value.
Definition Types.hpp:162
constexpr bool operator==(Entity other) const
Definition Types.hpp:167
static constexpr Entity make(u32 idx, u32 gen)
Creates an Entity from index and generation.
Definition Types.hpp:165
constexpr bool isValid() const
Checks if this is not the invalid sentinel.
Definition Types.hpp:156
static constexpr u32 INDEX_MASK
Definition Types.hpp:141
static constexpr u32 INDEX_BITS
Definition Types.hpp:139
constexpr Entity()
Definition Types.hpp:145
constexpr u32 generation() const
Gets the generation number.
Definition Types.hpp:153
constexpr u32 index() const
Gets the entity index.
Definition Types.hpp:150
Index+generation packing into a u32, parameterized by the bit split.
Definition PackedId.hpp:42
static constexpr std::uint32_t generationOf(std::uint32_t raw)
Definition PackedId.hpp:62
static constexpr std::uint32_t INDEX_MASK
Definition PackedId.hpp:51
static constexpr std::uint32_t pack(std::uint32_t index, std::uint32_t generation)
Definition PackedId.hpp:71
static constexpr std::uint32_t INDEX_BITS
Definition PackedId.hpp:49
static constexpr std::uint32_t indexOf(std::uint32_t raw)
Definition PackedId.hpp:60
static constexpr std::uint32_t GEN_BITS
Definition PackedId.hpp:50
static constexpr std::uint32_t GEN_MASK
Definition PackedId.hpp:52
std::size_t operator()(esengine::Entity e) const noexcept
Definition Types.hpp:252