sandbox
Loading...
Searching...
No Matches
entity.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_ENTITY_HPP_
3#define LIBSBX_ENTITY_HPP_
4
5#include <cinttypes>
6#include <memory>
7#include <type_traits>
8#include <utility>
9
10#include <libsbx/memory/concepts.hpp>
11
12namespace sbx::ecs {
13
19template<typename Type>
21
25template<>
26struct basic_entity_traits<std::uint32_t> {
27 using value_type = std::uint32_t;
28
29 using entity_type = std::uint32_t;
30 using version_type = std::uint16_t;
31
32 inline static constexpr auto entity_mask = entity_type{0xFFFFF};
33 inline static constexpr auto version_mask = entity_type{0xFFF};
34}; // struct basic_entity_traits
35
39template<>
40struct basic_entity_traits<std::uint64_t> {
41 using value_type = std::uint64_t;
42
43 using entity_type = std::uint64_t;
44 using version_type = std::uint32_t;
45
46 inline static constexpr auto entity_mask = entity_type{0xFFFFFFFF};
47 inline static constexpr auto version_mask = entity_type{0xFFFFFFFF};
48}; // struct basic_entity_traits
49
55template<typename Type>
56requires (std::is_enum_v<Type>)
57struct basic_entity_traits<Type> : basic_entity_traits<std::underlying_type_t<Type>> {
58 using value_type = Type;
59}; // struct basic_entity_traits
60
61template<typename Type>
62requires (std::is_class_v<Type>)
64 using value_type = Type;
65}; // struct basic_entity_traits
66
72template<typename Type>
74
77 using version_type = basic_entity_traits<Type>::version_type;
78
79 inline static constexpr auto entity_mask = basic_entity_traits<Type>::entity_mask;
80 inline static constexpr auto version_mask = basic_entity_traits<Type>::version_mask;
81 inline static constexpr auto version_shift = std::popcount(entity_mask);
82
83 inline static constexpr auto page_size = std::size_t{4096};
84
85 static constexpr auto to_integral(const value_type value) noexcept -> entity_type;
86
87 static constexpr auto to_entity(const value_type value) noexcept -> entity_type;
88
89 static constexpr auto to_version(const value_type value) noexcept -> version_type;
90
91 static constexpr auto next(const value_type value) noexcept -> value_type;
92
93 static constexpr auto construct(const entity_type entity, const version_type version = version_type{0}) noexcept -> value_type;
94
95 static constexpr auto combine(const entity_type lhs, const entity_type rhs) noexcept -> value_type;
96
97}; // struct entity_traits
98
99enum class entity : std::uint32_t { };
100
102
103 template<typename Entity>
104 [[nodiscard]] constexpr operator Entity() const noexcept;
105
106 [[nodiscard]] constexpr auto operator==([[maybe_unused]] const basic_null_entity other) const noexcept -> bool;
107
108 template<typename Entity>
109 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept;
110
111}; // struct basic_null_entity
112
113inline constexpr auto null_entity = basic_null_entity{};
114
116
117 template<typename Entity>
118 [[nodiscard]] constexpr operator Entity() const noexcept;
119
120 [[nodiscard]] constexpr auto operator==([[maybe_unused]] const basic_tombstone_entity other) const noexcept -> bool;
121
122 template<typename Entity>
123 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept;
124
125}; // struct basic_tombstone_entity
126
127inline constexpr auto tombstone_entity = basic_tombstone_entity{};
128
129} // namespace sbx::ecs
130
131#include <libsbx/ecs/entity.ipp>
132
133#endif // LIBSBX_ENTITY_HPP_
Primary template.
Definition: entity.hpp:20
Definition: entity.hpp:101
Definition: entity.hpp:115
Entity traits.
Definition: entity.hpp:73