1 #ifndef ENTT_ENTITY_REGISTRY_HPP
2 #define ENTT_ENTITY_REGISTRY_HPP
12 #include "../config/config.h"
13 #include "../container/dense_map.hpp"
14 #include "../core/algorithm.hpp"
15 #include "../core/any.hpp"
16 #include "../core/fwd.hpp"
17 #include "../core/iterator.hpp"
18 #include "../core/type_info.hpp"
19 #include "../core/type_traits.hpp"
20 #include "../core/utility.hpp"
21 #include "component.hpp"
25 #include "runtime_view.hpp"
26 #include "sparse_set.hpp"
27 #include "storage.hpp"
28 #include "utility.hpp"
41 class storage_proxy_iterator final {
42 template<
typename Other>
43 friend class storage_proxy_iterator;
45 using mapped_type = std::remove_reference_t<decltype(std::declval<It>()->second)>;
48 using value_type = std::pair<id_type, constness_as_t<typename mapped_type::element_type, mapped_type> &>;
49 using pointer = input_iterator_pointer<value_type>;
50 using reference = value_type;
51 using difference_type = std::ptrdiff_t;
52 using iterator_category = std::input_iterator_tag;
54 storage_proxy_iterator() ENTT_NOEXCEPT
57 storage_proxy_iterator(
const It iter) ENTT_NOEXCEPT
60 template<
typename Other,
typename = std::enable_if_t<!std::is_same_v<It, Other> && std::is_constructible_v<It, Other>>>
61 storage_proxy_iterator(
const storage_proxy_iterator<Other> &other) ENTT_NOEXCEPT
64 storage_proxy_iterator &operator++() ENTT_NOEXCEPT {
68 storage_proxy_iterator operator++(
int) ENTT_NOEXCEPT {
69 storage_proxy_iterator orig = *
this;
70 return ++(*this), orig;
73 storage_proxy_iterator &operator--() ENTT_NOEXCEPT {
77 storage_proxy_iterator operator--(
int) ENTT_NOEXCEPT {
78 storage_proxy_iterator orig = *
this;
79 return operator--(), orig;
82 storage_proxy_iterator &operator+=(
const difference_type value) ENTT_NOEXCEPT {
87 storage_proxy_iterator
operator+(
const difference_type value)
const ENTT_NOEXCEPT {
88 storage_proxy_iterator copy = *
this;
89 return (copy += value);
92 storage_proxy_iterator &operator-=(
const difference_type value) ENTT_NOEXCEPT {
93 return (*
this += -value);
96 storage_proxy_iterator operator-(
const difference_type value)
const ENTT_NOEXCEPT {
97 return (*
this + -value);
100 [[nodiscard]] reference operator[](
const difference_type value)
const ENTT_NOEXCEPT {
101 return {it[value].first, *it[value].second};
104 [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
105 return {it->first, *it->second};
108 [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
112 template<
typename ILhs,
typename IRhs>
113 friend std::ptrdiff_t operator-(
const storage_proxy_iterator<ILhs> &,
const storage_proxy_iterator<IRhs> &) ENTT_NOEXCEPT;
115 template<
typename ILhs,
typename IRhs>
116 friend bool operator==(
const storage_proxy_iterator<ILhs> &,
const storage_proxy_iterator<IRhs> &) ENTT_NOEXCEPT;
118 template<
typename ILhs,
typename IRhs>
119 friend bool operator<(
const storage_proxy_iterator<ILhs> &,
const storage_proxy_iterator<IRhs> &) ENTT_NOEXCEPT;
125 template<
typename ILhs,
typename IRhs>
126 [[nodiscard]] std::ptrdiff_t operator-(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
127 return lhs.it - rhs.it;
130 template<
typename ILhs,
typename IRhs>
131 [[nodiscard]]
bool operator==(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
132 return lhs.it == rhs.it;
135 template<
typename ILhs,
typename IRhs>
136 [[nodiscard]]
bool operator!=(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
137 return !(lhs == rhs);
140 template<
typename ILhs,
typename IRhs>
141 [[nodiscard]]
bool operator<(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
142 return lhs.it < rhs.it;
145 template<
typename ILhs,
typename IRhs>
146 [[nodiscard]]
bool operator>(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
150 template<
typename ILhs,
typename IRhs>
151 [[nodiscard]]
bool operator<=(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
155 template<
typename ILhs,
typename IRhs>
156 [[nodiscard]]
bool operator>=(
const storage_proxy_iterator<ILhs> &lhs,
const storage_proxy_iterator<IRhs> &rhs) ENTT_NOEXCEPT {
160 struct registry_context {
161 template<
typename Type,
typename... Args>
162 Type &emplace_hint(
const id_type id, Args &&...args) {
163 return any_cast<Type &>(data.try_emplace(
id, std::in_place_type<Type>, std::forward<Args>(args)...).first->second);
166 template<
typename Type,
typename... Args>
167 Type &emplace(Args &&...args) {
168 return emplace_hint<Type>(type_id<Type>().hash(), std::forward<Args>(args)...);
171 template<
typename Type>
172 bool erase(
const id_type id = type_id<Type>().hash()) {
173 const auto it = data.find(
id);
174 return it != data.end() && it->second.type() == type_id<Type>() ? (data.erase(it),
true) :
false;
177 template<
typename Type>
178 [[nodiscard]] std::add_const_t<Type> &at(
const id_type id = type_id<Type>().hash())
const {
179 return any_cast<std::add_const_t<Type> &>(data.at(
id));
182 template<
typename Type>
183 [[nodiscard]] Type &at(
const id_type id = type_id<Type>().hash()) {
184 return any_cast<Type &>(data.at(
id));
187 template<
typename Type>
188 [[nodiscard]] std::add_const_t<Type> *find(
const id_type id = type_id<Type>().hash())
const {
189 const auto it = data.find(
id);
190 return it != data.cend() ? any_cast<std::add_const_t<Type>>(&it->second) :
nullptr;
193 template<
typename Type>
194 [[nodiscard]] Type *find(
const id_type id = type_id<Type>().hash()) {
195 const auto it = data.find(
id);
196 return it != data.end() ? any_cast<Type>(&it->second) : nullptr;
199 template<
typename Type>
200 [[nodiscard]]
bool contains(
const id_type id = type_id<Type>().hash())
const {
201 const auto it = data.find(
id);
202 return it != data.end() && it->second.type() == type_id<Type>();
206 dense_map<id_type, basic_any<0u>, identity> data;
220 template<
typename Entity>
225 template<
typename Component>
228 template<
typename...>
229 struct group_handler;
231 template<
typename... Exclude,
typename... Get,
typename... Owned>
232 struct group_handler<
exclude_t<Exclude...>,
get_t<Get...>, Owned...> {
237 template<
typename Component>
239 [[maybe_unused]]
const auto cpools = std::forward_as_tuple(owner.assure<Owned>()...);
241 const auto is_valid = ((std::is_same_v<Component, Owned> || std::get<storage_type<Owned> &>(cpools).contains(
entt)) && ...)
242 && ((std::is_same_v<Component, Get> || owner.assure<Get>().contains(
entt)) && ...)
243 && ((std::is_same_v<Component, Exclude> || !owner.assure<Exclude>().contains(
entt)) && ...);
245 if constexpr(
sizeof...(Owned) == 0) {
250 if(is_valid && !(std::get<0>(cpools).index(
entt) <
current)) {
252 (std::get<storage_type<Owned> &>(cpools).swap_elements(std::get<storage_type<Owned> &>(cpools).data()[pos],
entt), ...);
258 if constexpr(
sizeof...(Owned) == 0) {
261 if(
const auto cpools = std::forward_as_tuple(owner.assure<Owned>()...); std::get<0>(cpools).contains(
entt) && (std::get<0>(cpools).index(
entt) <
current)) {
263 (std::get<storage_type<Owned> &>(cpools).swap_elements(std::get<storage_type<Owned> &>(cpools).data()[pos],
entt), ...);
271 std::unique_ptr<void, void (*)(
void *)>
group;
277 template<
typename Component>
279 static_assert(std::is_same_v<Component, std::decay_t<Component>>,
"Non-decayed types not allowed");
280 auto &&cpool = pools[id];
283 cpool.reset(
new storage_type<Component>{});
287 ENTT_ASSERT(cpool->type() == type_id<Component>(),
"Unexpected type");
288 return static_cast<storage_type<Component> &
>(*cpool);
291 template<
typename Component>
293 static_assert(std::is_same_v<Component, std::decay_t<Component>>,
"Non-decayed types not allowed");
295 if(
const auto it = pools.find(
id); it != pools.cend()) {
296 ENTT_ASSERT(it->second->type() == type_id<Component>(),
"Unexpected type");
297 return static_cast<const storage_type<Component> &
>(*it->second);
300 static storage_type<Component> placeholder{};
304 auto generate_identifier(
const std::size_t pos) ENTT_NOEXCEPT {
309 auto recycle_identifier() ENTT_NOEXCEPT {
310 ENTT_ASSERT(free_list !=
null,
"No entities available");
353 pools.reserve(count);
361 : pools{std::move(other.pools)},
362 groups{std::move(other.groups)},
363 entities{std::move(other.entities)},
364 free_list{other.free_list},
365 vars{std::move(other.vars)} {
366 for(
auto &&curr: pools) {
377 pools = std::move(other.pools);
378 groups = std::move(other.groups);
379 entities = std::move(other.entities);
380 free_list = other.free_list;
381 vars = std::move(other.vars);
383 for(
auto &&curr: pools) {
399 return iterable_adaptor{internal::storage_proxy_iterator{pools.begin()}, internal::storage_proxy_iterator{pools.end()}};
403 [[nodiscard]]
auto storage() const ENTT_NOEXCEPT {
404 return iterable_adaptor{internal::storage_proxy_iterator{pools.cbegin()}, internal::storage_proxy_iterator{pools.cend()}};
414 return internal::storage_proxy_iterator{pools.find(
id)};
424 return internal::storage_proxy_iterator{pools.find(
id)};
433 template<
typename Component>
435 if constexpr(std::is_const_v<Component>) {
436 return std::as_const(*this).template
storage<std::remove_const_t<Component>>(
id);
438 return assure<Component>(
id);
453 template<
typename Component>
455 return assure<std::remove_const_t<Component>>(id);
463 return entities.size();
471 auto sz = entities.size();
473 for(
auto curr = free_list; curr !=
null; --sz) {
485 entities.reserve(cap);
494 return entities.capacity();
518 return entities.data();
540 return (pos < entities.size() && entities[pos] ==
entity);
559 return (free_list ==
null) ? entities.emplace_back(generate_identifier(entities.size())) : recycle_identifier();
572 const auto length = entities.size();
577 entities.resize(
size_type(req) + 1u,
null);
579 for(
auto pos = length; pos < req; ++pos) {
580 release_entity(generate_identifier(pos), {});
583 return (entities[req] = hint);
587 auto *it = &free_list;
590 return (entities[req] = hint);
603 template<
typename It>
605 for(; free_list !=
null && first != last; ++first) {
606 *first = recycle_identifier();
609 const auto length = entities.size();
610 entities.resize(length + std::distance(first, last),
null);
612 for(
auto pos = length; first != last; ++first, ++pos) {
613 *first = entities[pos] = generate_identifier(pos);
634 template<
typename It>
636 ENTT_ASSERT(!
alive(),
"Entities still alive");
637 entities.assign(first, last);
638 free_list = destroyed;
670 return release_entity(
entity, version);
682 template<
typename It>
684 for(; first != last; ++first) {
721 for(
size_type pos = pools.size(); pos; --pos) {
722 pools.begin()[pos - 1u].second->remove(
entity);
725 return release_entity(
entity, version);
737 template<
typename It>
739 for(; first != last; ++first) {
759 template<
typename Component,
typename... Args>
762 return assure<Component>().emplace(
entity, std::forward<Args>(args)...);
776 template<
typename Component,
typename It>
777 void insert(It first, It last,
const Component &value = {}) {
778 ENTT_ASSERT(std::all_of(first, last, [
this](
const auto entity) {
return valid(
entity); }),
"Invalid entity");
779 assure<Component>().insert(first, last, value);
794 template<typename Component, typename EIt, typename CIt, typename = std::enable_if_t<std::is_same_v<typename std::iterator_traits<CIt>::value_type, Component>>>
795 void insert(EIt first, EIt last, CIt from) {
796 ENTT_ASSERT(std::all_of(first, last, [
this](
const auto entity) {
return valid(
entity); }),
"Invalid entity");
797 assure<Component>().insert(first, last, from);
812 template<
typename Component,
typename... Args>
815 auto &cpool = assure<Component>();
817 return cpool.contains(
entity)
818 ? cpool.patch(
entity, [&args...](
auto &...curr) { ((curr = Component{std::forward<Args>(args)...}), ...); })
819 : cpool.emplace(
entity, std::forward<Args>(args)...);
846 template<
typename Component,
typename... Func>
848 ENTT_ASSERT(valid(
entity),
"Invalid entity");
849 return assure<Component>().patch(
entity, std::forward<Func>(func)...);
867 template<
typename Component,
typename... Args>
869 ENTT_ASSERT(valid(
entity),
"Invalid entity");
870 return assure<Component>().patch(
entity, [&args...](
auto &...curr) { ((curr = Component{std::forward<Args>(args)...}), ...); });
884 template<
typename Component,
typename... Other>
886 ENTT_ASSERT(valid(
entity),
"Invalid entity");
887 return (assure<Component>().remove(
entity) + ... + assure<Other>().remove(
entity));
902 template<
typename Component,
typename... Other,
typename It>
904 if constexpr(
sizeof...(Other) == 0u) {
905 ENTT_ASSERT(std::all_of(first, last, [
this](
const auto entity) {
return valid(
entity); }),
"Invalid entity");
906 return assure<Component>().remove(std::move(first), std::move(last));
910 for(
auto cpools = std::forward_as_tuple(assure<Component>(), assure<Other>()...); first != last; ++first) {
911 ENTT_ASSERT(valid(*first),
"Invalid entity");
912 count += std::apply([
entt = *first](
auto &...curr) {
return (curr.remove(
entt) + ... + 0u); }, cpools);
930 template<
typename Component,
typename... Other>
932 ENTT_ASSERT(valid(
entity),
"Invalid entity");
933 (assure<Component>().erase(
entity), (assure<Other>().erase(
entity), ...));
947 template<
typename Component,
typename... Other,
typename It>
949 if constexpr(
sizeof...(Other) == 0u) {
950 ENTT_ASSERT(std::all_of(first, last, [
this](
const auto entity) {
return valid(
entity); }),
"Invalid entity");
951 assure<Component>().erase(std::move(first), std::move(last));
953 for(
auto cpools = std::forward_as_tuple(assure<Component>(), assure<Other>()...); first != last; ++first) {
954 ENTT_ASSERT(valid(*first),
"Invalid entity");
955 std::apply([
entt = *first](
auto &...curr) { (curr.erase(
entt), ...); }, cpools);
965 template<
typename... Component>
967 if constexpr(
sizeof...(Component) == 0) {
968 for(
auto &&curr: pools) {
969 curr.second->compact();
972 (assure<Component>().compact(), ...);
986 template<
typename... Component>
988 ENTT_ASSERT(valid(
entity),
"Invalid entity");
989 return (assure<std::remove_const_t<Component>>().contains(
entity) && ...);
1003 template<
typename... Component>
1005 ENTT_ASSERT(valid(
entity),
"Invalid entity");
1006 return (assure<std::remove_const_t<Component>>().contains(
entity) || ...);
1020 template<
typename... Component>
1022 ENTT_ASSERT(valid(
entity),
"Invalid entity");
1023 return view<Component...>().
template get<const Component...>(
entity);
1027 template<
typename... Component>
1029 ENTT_ASSERT(valid(
entity),
"Invalid entity");
1030 return view<Component...>().
template get<Component...>(
entity);
1048 template<
typename Component,
typename... Args>
1050 ENTT_ASSERT(valid(
entity),
"Invalid entity");
1051 auto &cpool = assure<Component>();
1052 return cpool.contains(
entity) ? cpool.get(
entity) : cpool.emplace(
entity, std::forward<Args>(args)...);
1068 template<
typename... Component>
1070 ENTT_ASSERT(valid(
entity),
"Invalid entity");
1072 if constexpr(
sizeof...(Component) == 1) {
1073 const auto &cpool = assure<std::remove_const_t<Component>...>();
1074 return cpool.contains(
entity) ? std::addressof(cpool.get(
entity)) :
nullptr;
1076 return std::make_tuple(try_get<Component>(
entity)...);
1081 template<
typename... Component>
1083 if constexpr(
sizeof...(Component) == 1) {
1084 return (
const_cast<Component *
>(std::as_const(*this).template try_get<Component>(
entity)), ...);
1086 return std::make_tuple(try_get<Component>(
entity)...);
1094 template<
typename... Component>
1096 if constexpr(
sizeof...(Component) == 0) {
1097 for(
auto &&curr: pools) {
1098 curr.second->clear();
1101 each([
this](
const auto entity) { this->release(
entity); });
1103 (assure<Component>().clear(), ...);
1121 template<
typename Func>
1123 if(free_list ==
null) {
1124 for(
auto pos = entities.size(); pos; --pos) {
1125 func(entities[pos - 1]);
1128 for(
auto pos = entities.size(); pos; --pos) {
1129 if(
const auto entity = entities[pos - 1]; entity_traits::to_entity(
entity) == (pos - 1)) {
1142 ENTT_ASSERT(valid(
entity),
"Invalid entity");
1143 return std::none_of(pools.cbegin(), pools.cend(), [
entity](
auto &&curr) { return curr.second->contains(entity); });
1164 template<
typename Component>
1166 return assure<Component>().on_construct();
1187 template<
typename Component>
1189 return assure<Component>().on_update();
1210 template<
typename Component>
1212 return assure<Component>().on_destroy();
1228 template<
typename Component,
typename... Other,
typename... Exclude>
1230 return {assure<std::remove_const_t<Component>>(), assure<std::remove_const_t<Other>>()..., assure<Exclude>()...};
1234 template<
typename Component,
typename... Other,
typename... Exclude>
1236 return {assure<std::remove_const_t<Component>>(), assure<std::remove_const_t<Other>>()..., assure<Exclude>()...};
1263 template<
typename... Owned,
typename... Get,
typename... Exclude>
1265 static_assert(
sizeof...(Owned) +
sizeof...(Get) > 0,
"Exclusion-only groups are not supported");
1266 static_assert(
sizeof...(Owned) +
sizeof...(Get) +
sizeof...(Exclude) > 1,
"Single component groups are not allowed");
1268 using handler_type = group_handler<exclude_t<std::remove_const_t<Exclude>...>,
get_t<std::remove_const_t<Get>...>, std::remove_const_t<Owned>...>;
1270 const auto cpools = std::forward_as_tuple(assure<std::remove_const_t<Owned>>()..., assure<std::remove_const_t<Get>>()...);
1271 constexpr
auto size =
sizeof...(Owned) +
sizeof...(Get) +
sizeof...(Exclude);
1272 handler_type *handler =
nullptr;
1274 auto it = std::find_if(groups.cbegin(), groups.cend(), [size](
const auto &gdata) {
1275 return gdata.size == size
1276 && (gdata.owned(type_hash<std::remove_const_t<Owned>>::value()) && ...)
1277 && (gdata.get(type_hash<std::remove_const_t<Get>>::value()) && ...)
1278 && (gdata.exclude(type_hash<Exclude>::value()) && ...);
1281 if(it != groups.cend()) {
1282 handler =
static_cast<handler_type *
>(it->group.get());
1284 group_data candidate = {
1286 {
new handler_type{}, [](
void *instance) {
delete static_cast<handler_type *
>(instance); }},
1287 []([[maybe_unused]]
const id_type ctype) ENTT_NOEXCEPT {
return ((ctype == type_hash<std::remove_const_t<Owned>>::value()) || ...); },
1288 []([[maybe_unused]]
const id_type ctype) ENTT_NOEXCEPT {
return ((ctype == type_hash<std::remove_const_t<Get>>::value()) || ...); },
1289 []([[maybe_unused]]
const id_type ctype) ENTT_NOEXCEPT {
return ((ctype == type_hash<Exclude>::value()) || ...); },
1292 handler =
static_cast<handler_type *
>(candidate.group.get());
1294 const void *maybe_valid_if =
nullptr;
1295 const void *discard_if =
nullptr;
1297 if constexpr(
sizeof...(Owned) == 0) {
1298 groups.push_back(std::move(candidate));
1300 [[maybe_unused]]
auto has_conflict = [size](
const auto &gdata) {
1301 const auto overlapping = (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value()));
1302 const auto sz = overlapping + (0u + ... + gdata.get(type_hash<std::remove_const_t<Get>>::value())) + (0u + ... + gdata.exclude(type_hash<Exclude>::value()));
1303 return !overlapping || ((sz == size) || (sz == gdata.size));
1306 ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), std::move(has_conflict)),
"Conflicting groups");
1308 const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [size](
const auto &gdata) {
1309 return !(0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value())) || (size > gdata.size);
1312 const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](
const auto &gdata) {
1313 return (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value()));
1316 maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get());
1317 discard_if = (prev == groups.crend() ? discard_if : prev->group.get());
1318 groups.insert(next, std::move(candidate));
1321 (on_construct<std::remove_const_t<Owned>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::remove_const_t<Owned>>>(*handler), ...);
1322 (on_construct<std::remove_const_t<Get>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::remove_const_t<Get>>>(*handler), ...);
1323 (on_destroy<Exclude>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<Exclude>>(*handler), ...);
1325 (on_destroy<std::remove_const_t<Owned>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
1326 (on_destroy<std::remove_const_t<Get>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
1327 (on_construct<Exclude>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
1329 if constexpr(
sizeof...(Owned) == 0) {
1330 for(
const auto entity: view<Owned..., Get...>(exclude<Exclude...>)) {
1331 handler->current.emplace(entity);
1335 for(
auto *first = std::get<0>(cpools).data(), *last = first + std::get<0>(cpools).size(); first != last; ++first) {
1336 handler->template maybe_valid_if<type_list_element_t<0, type_list<std::remove_const_t<Owned>...>>>(*
this, *first);
1341 return {handler->current, std::get<storage_type<std::remove_const_t<Owned>> &>(cpools)..., std::get<storage_type<std::remove_const_t<Get>> &>(cpools)...};
1345 template<
typename... Owned,
typename... Get,
typename... Exclude>
1347 auto it = std::find_if(groups.cbegin(), groups.cend(), [](
const auto &gdata) {
1348 return gdata.size == (sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude))
1349 && (gdata.owned(type_hash<std::remove_const_t<Owned>>::value()) && ...)
1350 && (gdata.get(type_hash<std::remove_const_t<Get>>::value()) && ...)
1351 && (gdata.exclude(type_hash<Exclude>::value()) && ...);
1354 if(it == groups.cend()) {
1357 using handler_type = group_handler<exclude_t<std::remove_const_t<Exclude>...>, get_t<std::remove_const_t<Get>...>, std::remove_const_t<Owned>...>;
1358 return {
static_cast<handler_type *
>(it->group.get())->current, assure<std::remove_const_t<Owned>>()..., assure<std::remove_const_t<Get>>()...};
1363 template<
typename... Owned,
typename... Exclude>
1365 return group<Owned...>(
get_t<>{}, exclude<Exclude...>);
1369 template<
typename... Owned,
typename... Exclude>
1371 return group_if_exists<std::add_const_t<Owned>...>(
get_t<>{}, exclude<Exclude...>);
1380 template<
typename... Component>
1382 return std::any_of(groups.cbegin(), groups.cend(), [](
auto &&gdata) { return (gdata.owned(type_hash<std::remove_const_t<Component>>::value()) || ...); });
1392 template<
typename... Owned,
typename... Get,
typename... Exclude>
1394 constexpr
auto size =
sizeof...(Owned) +
sizeof...(Get) +
sizeof...(Exclude);
1395 auto pred = [size](
const auto &gdata) {
return (0u + ... + gdata.owned(
type_hash<std::remove_const_t<Owned>>::value())) && (size < gdata.size); };
1396 return std::find_if(groups.cbegin(), groups.cend(), std::move(pred)) == groups.cend();
1434 template<
typename Component,
typename Compare,
typename Sort =
std_sort,
typename... Args>
1435 void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
1436 ENTT_ASSERT(!owned<Component>(),
"Cannot sort owned storage");
1437 auto &cpool = assure<Component>();
1439 if constexpr(std::is_invocable_v<Compare, decltype(cpool.get({})), decltype(cpool.get({}))>) {
1440 auto comp = [&cpool, compare = std::move(compare)](
const auto lhs,
const auto rhs) {
return compare(std::as_const(cpool.get(lhs)), std::as_const(cpool.get(rhs))); };
1441 cpool.sort(std::move(comp), std::move(algo), std::forward<Args>(args)...);
1443 cpool.sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
1466 template<
typename To,
typename From>
1468 ENTT_ASSERT(!owned<To>(),
"Cannot sort owned storage");
1469 assure<To>().respect(assure<From>());
1487 std::vector<group_data> groups;
1488 std::vector<entity_type> entities;
1489 entity_type free_list;
Fast and reliable entity-component system.
size_type capacity() const
Returns the number of entities that a registry has currently allocated space for.
const context & ctx() const
Returns the context object, that is, a general purpose container.
bool any_of(const entity_type entity) const
Checks if an entity has at least one of the given components.
context & ctx()
Returns the context object, that is, a general purpose container.
auto storage(const id_type id)
Finds the storage associated with a given name, if any.
bool sortable(const basic_group< entity_type, owned_t< Owned... >, get_t< Get... >, exclude_t< Exclude... >> &)
Checks whether a group can be sorted.
bool orphan(const entity_type entity) const
Checks if an entity has components assigned.
internal::registry_context context
Context type.
bool valid(const entity_type entity) const
Checks if an identifier refers to a valid entity.
version_type current(const entity_type entity) const
Returns the actual version for an identifier.
void insert(EIt first, EIt last, CIt from)
Assigns each entity in a range the given components.
basic_registry & operator=(basic_registry &&other)
Move assignment operator.
void compact()
Removes all tombstones from a registry or only the pools for the given components.
basic_view< entity_type, get_t< std::add_const_t< Component >, std::add_const_t< Other >... >, exclude_t< Exclude... > > view(exclude_t< Exclude... >={}) const
Returns a view for the given components.
void sort()
Sorts two pools of components in the same way.
void create(It first, It last)
Assigns each element in a range an identifier.
void insert(It first, It last, const Component &value={})
Assigns each entity in a range the given component.
void assign(It first, It last, const entity_type destroyed)
Assigns identifiers to an empty registry.
decltype(auto) get([[maybe_unused]] const entity_type entity) const
Returns references to the given components for an entity.
decltype(auto) emplace_or_replace(const entity_type entity, Args &&...args)
Assigns or replaces the given component for an entity.
version_type release(const entity_type entity)
Releases an identifier.
bool all_of(const entity_type entity) const
Checks if an entity has all the given components.
void sort(Compare compare, Sort algo=Sort{}, Args &&...args)
Sorts the elements of a given component.
entity_type released() const
Returns the head of the list of released entities.
basic_registry(const size_type count)
Allocates enough memory upon construction to store count pools.
decltype(auto) emplace(const entity_type entity, Args &&...args)
Assigns the given component to an entity.
bool empty() const
Checks whether the registry is empty (no entities still in use).
size_type remove(It first, It last)
Removes the given components from all the entities in a range.
size_type size() const
Returns the number of entities created so far.
basic_view< entity_type, get_t< Component, Other... >, exclude_t< Exclude... > > view(exclude_t< Exclude... >={})
Returns a view for the given components.
void release(It first, It last)
Releases all identifiers in a range.
std::size_t size_type
Unsigned integer type.
void destroy(It first, It last)
Destroys all entities in a range and releases their identifiers.
auto storage() const
Returns an iterable object to use to visit a registry.
size_type alive() const
Returns the number of entities still in use.
version_type destroy(const entity_type entity)
Destroys an entity and releases its identifier.
basic_group< entity_type, owned_t< std::add_const_t< Owned >... >, get_t< std::add_const_t< Get >... >, exclude_t< Exclude... > > group_if_exists(get_t< Get... >, exclude_t< Exclude... >={}) const
Returns a group for the given components.
Entity entity_type
Underlying entity identifier.
typename entity_traits::version_type version_type
Underlying version type.
auto storage(const id_type id) const
Finds the storage associated with a given name, if any.
basic_registry()
Default constructor.
void each(Func func) const
Iterates all the entities that are still in use.
void erase(It first, It last)
Erases the given components from all the entities in a range.
entity_type create()
Creates a new entity or recycles a destroyed one.
void reserve(const size_type cap)
Increases the capacity (number of entities) of the registry.
auto try_get([[maybe_unused]] const entity_type entity) const
Returns pointers to the given components for an entity.
bool owned() const
Checks whether the given components belong to any group.
auto storage()
Returns an iterable object to use to visit a registry.
void clear()
Clears a whole registry or the pools for the given components.
size_type remove(const entity_type entity)
Removes the given components from an entity.
auto on_update()
Returns a sink object for the given component.
entity_type create(const entity_type hint)
Creates a new entity or recycles a destroyed one.
const entity_type * data() const
Direct access to the list of entities of a registry.
auto try_get([[maybe_unused]] const entity_type entity)
Returns pointers to the given components for an entity.
basic_registry(basic_registry &&other)
Move constructor.
void erase(const entity_type entity)
Erases the given components from an entity.
auto on_destroy()
Returns a sink object for the given component.
version_type destroy(const entity_type entity, const version_type version)
Destroys an entity and releases its identifier.
auto on_construct()
Returns a sink object for the given component.
basic_group< entity_type, owned_t< Owned... >, get_t< Get... >, exclude_t< Exclude... > > group(get_t< Get... >, exclude_t< Exclude... >={})
Returns a group for the given components.
basic_group< entity_type, owned_t< Owned... >, get_t<>, exclude_t< Exclude... > > group(exclude_t< Exclude... >={})
Returns a group for the given components.
basic_group< entity_type, owned_t< std::add_const_t< Owned >... >, get_t<>, exclude_t< Exclude... > > group_if_exists(exclude_t< Exclude... >={}) const
Returns a group for the given components.
version_type release(const entity_type entity, const version_type version)
Releases an identifier.
Basic sparse set implementation.
Basic storage implementation.
Associative container for key-value pairs with unique keys.
typename base_type::entity_type entity_type
Underlying entity type.
static constexpr entity_type to_integral(const value_type value)
Converts an entity to its underlying type.
static constexpr entity_type to_entity(const value_type value)
Returns the entity part once converted to the underlying type.
static constexpr value_type construct(const entity_type entity, const version_type version)
Constructs an identifier from its parts.
static constexpr value_type combine(const entity_type lhs, const entity_type rhs)
Combines two identifiers in a single one.
typename base_type::version_type version_type
Underlying version type.
static constexpr version_type to_version(const value_type value)
Returns the version part once converted to the underlying type.
Mixin type used to add signal support to storage types.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
entity
Default entity identifier.
std::uint32_t id_type
Alias declaration for type identifiers.
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
constexpr exclude_t< Type... > exclude
Variable template for exclusion lists.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
constexpr type_list< Type..., Other... > operator+(type_list< Type... >, type_list< Other... >)
Concatenates multiple type lists.
basic_any< Len, Align > forward_as_any(Type &&value)
Forwards its argument and avoids copies for lvalue references.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
Common way to access various properties of components.
Alias for exclusion lists.
Alias for lists of observed components.
Identity function object (waiting for C++20).
Utility class to create an iterable object from a pair of iterators.
Alias for lists of owned components.
Function object to wrap std::sort in a class type.