vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
types.h
1#pragma once
2
3#include <cstdint>
4#include <typeinfo>
5
6namespace Vkm::Engine {
7
15 uint32_t index = 0;
16 uint32_t generation = 0;
17
18 constexpr explicit operator bool() const noexcept { return index != 0; }
19
27 constexpr bool operator==(const StorageIndex& other) const noexcept {
28 return index == other.index && generation == other.generation;
29 }
30 constexpr bool operator!=(const StorageIndex& other) const noexcept {
31 return !(*this == other);
32 }
33};
34
35static_assert(sizeof(StorageIndex) == 8, "StorageIndex must be 8 bytes");
36
41 uint32_t value = 0;
42
43 static constexpr uint32_t ALIVE_BIT = uint32_t(1) << 31;
44 static constexpr uint32_t GEN_MASK = ~ALIVE_BIT;
45
51 void setAlive(bool alive) { value = (value & GEN_MASK) | (alive ? ALIVE_BIT : 0); }
52
58 void bumpGeneration() { value = (value & ALIVE_BIT) | (((value & GEN_MASK) + 1) & GEN_MASK); }
59
60 bool alive() const { return value & ALIVE_BIT; }
61 uint32_t generation() const { return value & GEN_MASK; }
62};
63
64static_assert(sizeof(GenerationIndex) == 4, "GenerationIndex must be 4 bytes");
65
73using TypeId = uint32_t;
74
75namespace detail {
82 TypeId typeIdFromInfo(const std::type_info& info);
83}
84
85template<typename T>
86TypeId typeId() {
87 // Cached per (module, T); the shared registry keeps the modules agreeing.
88 static const TypeId s_id = detail::typeIdFromInfo(typeid(T));
89 return s_id;
90}
91
92} // namespace Vkm::Engine
Per-slot alive flag (bit 31) and generation counter (bits 0-30) in one uint32_t.
Definition types.h:40
void setAlive(bool alive)
Set the alive/dead status, leaving the generation untouched.
Definition types.h:51
void bumpGeneration()
Increment the generation counter, preserving the alive bit.
Definition types.h:58
Opaque handle pairing a sparse-array index with a generation counter.
Definition types.h:14
constexpr bool operator==(const StorageIndex &other) const noexcept
Compare two handles for identity.
Definition types.h:27
uint32_t generation
The slot's generation when minted.
Definition types.h:16
uint32_t index
Sparse slot index; 0 is null.
Definition types.h:15