1#ifndef ENTT_ENTITY_SNAPSHOT_HPP
2#define ENTT_ENTITY_SNAPSHOT_HPP
10#include "../config/config.h"
11#include "../container/dense_map.hpp"
12#include "../core/type_traits.hpp"
22template<
typename Registry>
23void orphans(Registry ®istry) {
24 auto &
storage =
registry.template storage<typename Registry::entity_type>();
26 for(
auto entt: storage) {
46template<
typename Registry>
48 static_assert(!std::is_const_v<Registry>,
"Non-const registry type required");
93 template<typename Type, typename Archive>
96 const typename registry_type::common_type &base = *storage;
98 archive(static_cast<typename traits_type::entity_type>(storage->size()));
100 if constexpr(std::is_same_v<Type, entity_type>) {
101 archive(static_cast<typename traits_type::entity_type>(storage->free_list()));
103 for(auto first = base.rbegin(), last = base.rend(); first != last; ++first) {
107 for(auto it = base.rbegin(), last = base.rend(); it != last; ++it) {
108 if(const auto entt = *it; entt == tombstone) {
109 archive(static_cast<entity_type>(null));
112 std::apply([&archive](auto &&...args) { (archive(std::forward<decltype(args)>(args)), ...); }, storage->get_as_tuple(entt));
116 for(
auto elem:
storage->reach()) {
117 std::apply([&archive](
auto &&...args) { (archive(std::forward<
decltype(args)>(args)), ...); }, elem);
121 archive(
typename traits_type::entity_type{});
139 template<
typename Type,
typename Archive,
typename It>
141 static_assert(!std::is_same_v<Type, entity_type>,
"Entity types not supported");
146 for(; first != last; ++first) {
149 std::apply([&archive](
auto &&...args) { (archive(std::forward<
decltype(args)>(args)), ...); },
storage->get_as_tuple(
entt));
162 const registry_type *reg;
175template<
typename Registry>
177 static_assert(!std::is_const_v<Registry>,
"Non-const registry type required");
225 template<typename Type, typename Archive>
232 if constexpr(std::is_same_v<Type, entity_type>) {
242 placeholder = (
entity > placeholder) ?
entity : placeholder;
254 ENTT_ASSERT(
entity ==
entt,
"Entity not available for use");
256 if constexpr(std::tuple_size_v<
decltype(
storage.get_as_tuple({}))> == 0u) {
281 internal::orphans(*reg);
305template<
typename Registry>
307 static_assert(!std::is_const_v<Registry>,
"Non-const registry type required");
310 void restore(
typename Registry::entity_type
entt) {
312 if(!reg->valid(remloc[
entity].second)) {
313 remloc[
entity].second = reg->create();
316 remloc.insert_or_assign(
entity, std::make_pair(
entt, reg->create()));
320 template<
typename Container>
321 auto update(
int, Container &container) ->
decltype(
typename Container::mapped_type{}, void()) {
325 for(
auto &&pair: container) {
326 using first_type = std::remove_const_t<
typename std::decay_t<
decltype(pair)>::first_type>;
327 using second_type =
typename std::decay_t<
decltype(pair)>::second_type;
329 if constexpr(std::is_same_v<first_type, entity_type> && std::is_same_v<second_type, entity_type>) {
330 other.emplace(
map(pair.first),
map(pair.second));
331 }
else if constexpr(std::is_same_v<first_type, entity_type>) {
332 other.emplace(
map(pair.first), std::move(pair.second));
334 static_assert(std::is_same_v<second_type, entity_type>,
"Neither the key nor the value are of entity type");
335 other.emplace(std::move(pair.first),
map(pair.second));
340 swap(container, other);
343 template<
typename Container>
344 auto update(
char, Container &container) ->
decltype(
typename Container::value_type{}, void()) {
346 static_assert(std::is_same_v<typename Container::value_type, entity_type>,
"Invalid value type");
348 for(
auto &&
entt: container) {
353 template<
typename Component,
typename Other,
typename Member>
354 void update([[maybe_unused]] Component &instance, [[maybe_unused]] Member Other::*member) {
355 if constexpr(!std::is_same_v<Component, Other>) {
357 }
else if constexpr(std::is_same_v<Member, entity_type>) {
358 instance.*member =
map(instance.*member);
361 update(0, instance.*member);
376 : remloc{source.get_allocator()},
414 template<typename Type, typename Archive>
422 if constexpr(std::is_same_v<Type, entity_type>) {
428 for(std::size_t pos{}; pos < in_use; ++pos) {
433 for(std::size_t pos = in_use; pos < length; ++pos) {
437 if(reg->valid(remloc[
entity].second)) {
438 reg->destroy(remloc[
entity].second);
445 for(
auto &&
ref: remloc) {
453 if constexpr(std::tuple_size_v<
decltype(
storage.get_as_tuple({}))> == 0u) {
478 internal::orphans(*reg);
489 return it != remloc.cend() && it->second.first ==
entt;
498 if(
const auto it = remloc.find(
to_entity(
entt)); it != remloc.cend() && it->second.first ==
entt) {
499 return it->second.second;
basic_continuous_loader(const basic_continuous_loader &)=delete
Default copy constructor, deleted on purpose.
basic_continuous_loader(registry_type &source) noexcept
Constructs an instance that is bound to a given registry.
basic_continuous_loader(basic_continuous_loader &&) noexcept=default
Default move constructor.
typename registry_type::entity_type entity_type
bool contains(entity_type entt) const noexcept
Tests if a loader knows about a given entity.
entity_type map(entity_type entt) const noexcept
Returns the identifier to which an entity refers.
basic_continuous_loader & get(Archive &archive, const id_type id=type_hash< Type >::value())
basic_continuous_loader & orphans()
Destroys those entities that have no elements.
typename internal::entt_traits< Type >::entity_type entity_type
static constexpr value_type next(const value_type value) noexcept
bool orphan(const entity_type entt) const
Checks if an entity has elements assigned.
typename traits_type::value_type entity_type
basic_snapshot_loader(registry_type &source) noexcept
Constructs an instance that is bound to a given registry.
typename registry_type::entity_type entity_type
basic_snapshot_loader & get(Archive &archive, const id_type id=type_hash< Type >::value())
basic_snapshot_loader(basic_snapshot_loader &&) noexcept=default
Default move constructor.
basic_snapshot_loader & orphans()
Destroys those entities that have no elements.
basic_snapshot_loader(const basic_snapshot_loader &)=delete
Default copy constructor, deleted on purpose.
basic_snapshot(const registry_type &source) noexcept
Constructs an instance that is bound to a given registry.
typename registry_type::entity_type entity_type
basic_snapshot(const basic_snapshot &)=delete
Default copy constructor, deleted on purpose.
const basic_snapshot & get(Archive &archive, const id_type id=type_hash< Type >::value()) const
const basic_snapshot & get(Archive &archive, It first, It last, const id_type id=type_hash< Type >::value()) const
Serializes all elements of a type with associated identifiers for the entities in a range.
basic_snapshot(basic_snapshot &&) noexcept=default
Default move constructor.
void erase(const entity_type entt)
Erases an entity from a sparse set.
Associative container for key-value pairs with unique keys.
entity
Default entity identifier.
constexpr entt_traits< Entity >::entity_type to_entity(const Entity value) noexcept
Returns the entity part once converted to the underlying type.
constexpr void swap(compressed_pair< First, Second > &lhs, compressed_pair< First, Second > &rhs) noexcept
Swaps two compressed pair objects.
constexpr null_t null
Compile-time constant for null entities.
std::uint32_t id_type
Alias declaration for type identifiers.
basic_registry<> registry
Alias declaration for the most common use case.
@ in_place
In-place deletion policy.
@ ref
Aliasing mode, the object points to a non-const element.
basic_storage< Type > storage
Alias declaration for the most common use case.
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.