27 [[nodiscard]]
constexpr operator entity() const noexcept {
45 [[nodiscard]]
constexpr auto alive(
entity ent)
const noexcept ->
bool {
56 auto location = get_location(ent);
59 auto moved = location.archetype->swap_erase(location);
60 remove_location(ent.id());
63 set_location(moved->id(), location);
113 [[nodiscard]]
constexpr auto has(
entity ent)
const ->
bool {
114 const auto& location = get_location(ent);
115 return (location.archetype->template contains<C>() && ...);
122 const auto& location = get_location(ent);
124 location.archetype->visit(location, std::forward<
decltype(func)>(func));
131 const auto& location = get_location(ent);
143 [[maybe_unused]] detail::unique_types<Components...> uniqueness_check;
147 auto location =
archetype->template emplace<Components...>(
entity, std::forward<Components>(args)...);
148 set_location(
entity.id(), location);
152 template<component C>
154 auto& location = get_location(ent);
158 return {
false, std::addressof(
archetype->template get<C>(location)) };
161 auto [new_location, moved] =
archetype->
move(location, *new_archetype);
163 auto ptr = std::addressof(new_archetype->template get<C>(new_location));
166 set_location(moved->id(), location);
170 set_location(ent.id(), new_location);
172 return {
true, ptr };
176 template<component C>
178 auto& location = get_location(ent);
185 auto [new_location, moved] =
archetype->
move(location, *new_archetype);
187 set_location(moved->id(), location);
191 set_location(ent.id(), new_location);
196 auto& location = self.get_location(ent);
198 constexpr auto is_const = std::is_const_v<std::remove_reference_t<
decltype(self)>>;
199 using archetype_t = std::conditional_t<is_const, const archetype, archetype>;
202 return std::forward_as_tuple(
archetype->template get<Args>(location)...);
210 assert((&dest !=
this) &&
"Move to the same registry does not make sense");
219 auto location = get_location(ent);
220 auto* src_archetype = location.archetype;
223 auto [new_location, moved] = src_archetype->move(location, *dst_archetype);
225 remove_location(ent.id());
226 if (moved && moved != ent) {
227 set_location(moved->id(), location);
234 [new_location.chunk_index]
235 .ptr_unchecked<
entity>(new_location.entry_index) = placeholder;
237 dest.set_location(placeholder.
get_entity().id(), new_location);
243 auto location = get_location(ent);
244 auto* src_archetype = location.archetype;
246 auto new_location = src_archetype->copy(location, *dst_archetype);
251 [new_location.chunk_index]
252 .ptr_unchecked<
entity>(new_location.entry_index) = placeholder;
254 dest.set_location(placeholder.
get_entity().id(), new_location);
264 return copy(ent, *
this, placeholder);
268 constexpr void ensure_alive(
const entity& ent)
const {
274 [[nodiscard]]
constexpr auto get_location(
entity ent) -> entity_location& {
279 [[nodiscard]]
constexpr auto get_location(
entity ent)
const ->
const entity_location& {
284 constexpr void set_location(
typename entity::id_t entity_id,
const entity_location& location) {
288 constexpr void remove_location(
typename entity::id_t entity_id) {
Archetype groups entities that share the same types of components. Archetype has a list of fixed size...
Definition archetype.hpp:15
auto contains() const noexcept -> bool
Check if archetype has component C.
Definition archetype.hpp:172
archetype()=default
Construct a new archetype object without components.
auto move(const entity_location &location, archetype &other) -> std::pair< entity_location, std::optional< entity > >
Move entity and its components to a different archetype and returns a pair where the first element is...
Definition archetype.hpp:94
void visit(const entity_location &location, auto &&func)
Visit all components of an entity.
Definition archetype.hpp:135
Container for archetypes, holds a map from component set to archetype.
Definition archetype.hpp:292
auto ensure_archetype_removed(const archetype *anchor_archetype) -> archetype *
Get or create an archetype by removing Components from an anchor archetype.
Definition archetype.hpp:359
auto ensure_archetype_added(const archetype *anchor_archetype) -> archetype *
Get or create an archetype by adding new Components to an anchor archetype.
Definition archetype.hpp:340
auto ensure_archetype(const component_meta_set &components) -> archetype *
Get or create an archetype given the component meta set.
Definition archetype.hpp:306
Definition base_registry.hpp:35
constexpr void sync()
Synchronizes concurently reserved entities.
Definition base_registry.hpp:104
constexpr auto create_impl(Components &&... args) -> entity
Definition base_registry.hpp:141
constexpr auto reserve() -> placeholder_entity
Reserves an entity in a thread-safe manner.
Definition base_registry.hpp:98
detail::sparse_map< typename entity::id_t, entity_location > _entity_archetype_map
Definition base_registry.hpp:295
constexpr auto has(entity ent) const -> bool
Checks if the specified entity has all the given components.
Definition base_registry.hpp:113
class archetypes _archetypes
Definition base_registry.hpp:294
constexpr auto alive(entity ent) const noexcept -> bool
Checks if the specified entity is currently active within the registry.
Definition base_registry.hpp:45
void destroy(entity ent)
Destroys the given entity.
Definition base_registry.hpp:55
entity move(entity ent, base_registry &dest)
Definition base_registry.hpp:209
entity move(entity ent, base_registry &dest, placeholder_entity placeholder)
Definition base_registry.hpp:218
static constexpr auto get_impl(auto &&self, entity ent) -> decltype(auto)
Definition base_registry.hpp:195
constexpr void visit(entity ent, auto &&func)
Visit all components of an entity.
Definition base_registry.hpp:121
constexpr auto set_impl(entity ent) -> std::pair< bool, C * >
Definition base_registry.hpp:153
entity copy(entity ent, base_registry &dest, placeholder_entity placeholder) const
Definition base_registry.hpp:242
entity_pool _entity_pool
Definition base_registry.hpp:293
constexpr void visit(entity ent, auto &&func) const
Visit all components of an entity (const variant).
Definition base_registry.hpp:130
entity clone(entity ent)
Definition base_registry.hpp:259
constexpr auto archetypes() const noexcept -> const class archetypes &
Provides access to the immutable list of archetypes in the registry.
Definition base_registry.hpp:87
entity copy(entity ent, base_registry &dest) const
Definition base_registry.hpp:214
constexpr void remove(entity ent)
Definition base_registry.hpp:177
constexpr auto archetypes() noexcept -> class archetypes &
Provides access to the modifiable list of archetypes in the registry.
Definition base_registry.hpp:76
entity clone(entity ent, placeholder_entity placeholder)
Definition base_registry.hpp:263
constexpr auto allocate() -> entity
Definition base_registry.hpp:205
Represents a reference to an entity within a registry.
Definition entity_ref.hpp:355
Exception raised when accessing non existing entity.
Definition exceptions.hpp:13
Represents a reference to an entity within a registry.
Definition entity_ref.hpp:13
Placeholder (reserved) entity.
Definition base_registry.hpp:13
constexpr entity get_entity() const noexcept
Get underlaying entity handle.
Definition base_registry.hpp:17
Component concept. The component must be a struct/class that can be move constructed and move assigna...
Definition component.hpp:86
sparse_table< K, T, true, Allocator > sparse_map
Sparse map.
Definition sparse_map.hpp:12
Definition archetype.hpp:11
detail::handle_pool< entity > entity_pool
Pool of entities that generates and recycles entity IDs.
Definition entity.hpp:16
detail::handle< struct entity_tag_t > entity
Represents an entity, consisting of an ID and generation.
Definition entity.hpp:13