EnTT 3.13.0
Loading...
Searching...
No Matches
snapshot.hpp
1#ifndef ENTT_ENTITY_SNAPSHOT_HPP
2#define ENTT_ENTITY_SNAPSHOT_HPP
3
4#include <cstddef>
5#include <iterator>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9#include <vector>
10#include "../config/config.h"
11#include "../container/dense_map.hpp"
12#include "../core/type_traits.hpp"
13#include "entity.hpp"
14#include "fwd.hpp"
15#include "view.hpp"
16
17namespace entt {
18
20namespace internal {
21
22template<typename Registry>
23void orphans(Registry &registry) {
24 auto &storage = registry.template storage<typename Registry::entity_type>();
25
26 for(auto entt: storage) {
27 if(registry.orphan(entt)) {
28 storage.erase(entt);
29 }
30 }
31}
32
33} // namespace internal
46template<typename Registry>
48 static_assert(!std::is_const_v<Registry>, "Non-const registry type required");
49 using traits_type = typename Registry::traits_type;
50
51public:
53 using registry_type = Registry;
55 using entity_type = typename registry_type::entity_type;
56
61 basic_snapshot(const registry_type &source) noexcept
62 : reg{&source} {}
63
65 basic_snapshot(basic_snapshot &&) noexcept = default;
66
68 basic_snapshot &operator=(basic_snapshot &&) noexcept = default;
69
78 template<typename Type, typename Archive>
79 const basic_snapshot &get(Archive &archive, const id_type id = type_hash<Type>::value()) const {
80 if(const auto *storage = reg->template storage<Type>(id); storage) {
81 archive(static_cast<typename traits_type::entity_type>(storage->size()));
82
83 if constexpr(std::is_same_v<Type, entity_type>) {
84 archive(static_cast<typename traits_type::entity_type>(storage->free_list()));
85
86 for(auto first = storage->data(), last = first + storage->size(); first != last; ++first) {
87 archive(*first);
88 }
89 } else {
90 for(auto elem: storage->reach()) {
91 std::apply([&archive](auto &&...args) { (archive(std::forward<decltype(args)>(args)), ...); }, elem);
92 }
93 }
94 } else {
95 archive(typename traits_type::entity_type{});
96 }
97
98 return *this;
99 }
100
113 template<typename Type, typename Archive, typename It>
114 const basic_snapshot &get(Archive &archive, It first, It last, const id_type id = type_hash<Type>::value()) const {
115 static_assert(!std::is_same_v<Type, entity_type>, "Entity types not supported");
116
117 if(const auto *storage = reg->template storage<Type>(id); storage && !storage->empty()) {
118 archive(static_cast<typename traits_type::entity_type>(std::distance(first, last)));
119
120 for(; first != last; ++first) {
121 if(const auto entt = *first; storage->contains(entt)) {
122 archive(entt);
123 std::apply([&archive](auto &&...args) { (archive(std::forward<decltype(args)>(args)), ...); }, storage->get_as_tuple(entt));
124 } else {
125 archive(static_cast<entity_type>(null));
126 }
127 }
128 } else {
129 archive(typename traits_type::entity_type{});
130 }
131
132 return *this;
133 }
134
135private:
136 const registry_type *reg;
137};
138
149template<typename Registry>
151 static_assert(!std::is_const_v<Registry>, "Non-const registry type required");
152 using traits_type = typename Registry::traits_type;
153
154public:
156 using registry_type = Registry;
158 using entity_type = typename registry_type::entity_type;
159
165 : reg{&source} {
166 // restoring a snapshot as a whole requires a clean registry
167 ENTT_ASSERT(reg->template storage<entity_type>().empty() && (reg->storage().begin() == reg->storage().end()), "Registry must be empty");
168 }
169
172
174 basic_snapshot_loader &operator=(basic_snapshot_loader &&) noexcept = default;
175
184 template<typename Type, typename Archive>
185 basic_snapshot_loader &get(Archive &archive, const id_type id = type_hash<Type>::value()) {
186 auto &storage = reg->template storage<Type>(id);
187 typename traits_type::entity_type length{};
188
189 archive(length);
190
191 if constexpr(std::is_same_v<Type, entity_type>) {
192 typename traits_type::entity_type count{};
193
194 storage.reserve(length);
195 archive(count);
196
197 for(entity_type entity = null; length; --length) {
198 archive(entity);
200 }
201
202 storage.free_list(count);
203 } else {
204 auto &other = reg->template storage<entity_type>();
206
207 while(length--) {
208 if(archive(entt); entt != null) {
209 const auto entity = other.contains(entt) ? entt : other.emplace(entt);
210 ENTT_ASSERT(entity == entt, "Entity not available for use");
211
212 if constexpr(std::tuple_size_v<decltype(storage.get_as_tuple({}))> == 0u) {
214 } else {
215 Type elem{};
216 archive(elem);
217 storage.emplace(entity, std::move(elem));
218 }
219 }
220 }
221 }
222
223 return *this;
224 }
225
237 internal::orphans(*reg);
238 return *this;
239 }
240
241private:
242 registry_type *reg;
243};
244
261template<typename Registry>
263 static_assert(!std::is_const_v<Registry>, "Non-const registry type required");
264 using traits_type = typename Registry::traits_type;
265
266 void restore(typename Registry::entity_type entt) {
267 if(const auto entity = to_entity(entt); remloc.contains(entity) && remloc[entity].first == entt) {
268 if(!reg->valid(remloc[entity].second)) {
269 remloc[entity].second = reg->create();
270 }
271 } else {
272 remloc.insert_or_assign(entity, std::make_pair(entt, reg->create()));
273 }
274 }
275
276 template<typename Container>
277 auto update(int, Container &container) -> decltype(typename Container::mapped_type{}, void()) {
278 // map like container
279 Container other;
280
281 for(auto &&pair: container) {
282 using first_type = std::remove_const_t<typename std::decay_t<decltype(pair)>::first_type>;
283 using second_type = typename std::decay_t<decltype(pair)>::second_type;
284
285 if constexpr(std::is_same_v<first_type, entity_type> && std::is_same_v<second_type, entity_type>) {
286 other.emplace(map(pair.first), map(pair.second));
287 } else if constexpr(std::is_same_v<first_type, entity_type>) {
288 other.emplace(map(pair.first), std::move(pair.second));
289 } else {
290 static_assert(std::is_same_v<second_type, entity_type>, "Neither the key nor the value are of entity type");
291 other.emplace(std::move(pair.first), map(pair.second));
292 }
293 }
294
295 using std::swap;
296 swap(container, other);
297 }
298
299 template<typename Container>
300 auto update(char, Container &container) -> decltype(typename Container::value_type{}, void()) {
301 // vector like container
302 static_assert(std::is_same_v<typename Container::value_type, entity_type>, "Invalid value type");
303
304 for(auto &&entt: container) {
305 entt = map(entt);
306 }
307 }
308
309 template<typename Component, typename Other, typename Member>
310 void update([[maybe_unused]] Component &instance, [[maybe_unused]] Member Other::*member) {
311 if constexpr(!std::is_same_v<Component, Other>) {
312 return;
313 } else if constexpr(std::is_same_v<Member, entity_type>) {
314 instance.*member = map(instance.*member);
315 } else {
316 // maybe a container? let's try...
317 update(0, instance.*member);
318 }
319 }
320
321public:
323 using registry_type = Registry;
325 using entity_type = typename registry_type::entity_type;
326
332 : remloc{source.get_allocator()},
333 reg{&source} {}
334
337
340
355 template<typename Type, typename Archive>
357 auto &storage = reg->template storage<Type>(id);
358 typename traits_type::entity_type length{};
360
361 archive(length);
362
363 if constexpr(std::is_same_v<Type, entity_type>) {
364 typename traits_type::entity_type in_use{};
365
366 storage.reserve(length);
367 archive(in_use);
368
369 for(std::size_t pos{}; pos < in_use; ++pos) {
370 archive(entt);
371 restore(entt);
372 }
373
374 for(std::size_t pos = in_use; pos < length; ++pos) {
375 archive(entt);
376
377 if(const auto entity = to_entity(entt); remloc.contains(entity)) {
378 if(reg->valid(remloc[entity].second)) {
379 reg->destroy(remloc[entity].second);
380 }
381
382 remloc.erase(entity);
383 }
384 }
385 } else {
386 for(auto &&ref: remloc) {
387 storage.remove(ref.second.second);
388 }
389
390 while(length--) {
391 if(archive(entt); entt != null) {
392 restore(entt);
393
394 if constexpr(std::tuple_size_v<decltype(storage.get_as_tuple({}))> == 0u) {
396 } else {
397 Type elem{};
398 archive(elem);
399 storage.emplace(map(entt), std::move(elem));
400 }
401 }
402 }
403 }
404
405 return *this;
406 }
407
419 internal::orphans(*reg);
420 return *this;
421 }
422
428 [[nodiscard]] bool contains(entity_type entt) const noexcept {
429 const auto it = remloc.find(to_entity(entt));
430 return it != remloc.cend() && it->second.first == entt;
431 }
432
438 [[nodiscard]] entity_type map(entity_type entt) const noexcept {
439 if(const auto it = remloc.find(to_entity(entt)); it != remloc.cend() && it->second.first == entt) {
440 return it->second.second;
441 }
442
443 return null;
444 }
445
446private:
448 registry_type *reg;
449};
450
451} // namespace entt
452
453#endif
Utility class for continuous loading.
Definition snapshot.hpp:262
basic_continuous_loader & operator=(basic_continuous_loader &&)=default
Default move assignment operator.
basic_continuous_loader(basic_continuous_loader &&)=default
Default move constructor.
basic_continuous_loader(registry_type &source) noexcept
Constructs an instance that is bound to a given registry.
Definition snapshot.hpp:331
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition snapshot.hpp:325
bool contains(entity_type entt) const noexcept
Tests if a loader knows about a given entity.
Definition snapshot.hpp:428
entity_type map(entity_type entt) const noexcept
Returns the identifier to which an entity refers.
Definition snapshot.hpp:438
basic_continuous_loader & get(Archive &archive, const id_type id=type_hash< Type >::value())
Restores all elements of a type with associated identifiers.
Definition snapshot.hpp:356
basic_continuous_loader & orphans()
Destroys those entities that have no components.
Definition snapshot.hpp:418
Utility class to restore a snapshot as a whole.
Definition snapshot.hpp:150
basic_snapshot_loader(registry_type &source) noexcept
Constructs an instance that is bound to a given registry.
Definition snapshot.hpp:164
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition snapshot.hpp:158
basic_snapshot_loader(basic_snapshot_loader &&) noexcept=default
Default move constructor.
basic_snapshot_loader & orphans()
Destroys those entities that have no components.
Definition snapshot.hpp:236
Utility class to create snapshots from a registry.
Definition snapshot.hpp:47
basic_snapshot(const registry_type &source) noexcept
Constructs an instance that is bound to a given registry.
Definition snapshot.hpp:61
typename registry_type::entity_type entity_type
Underlying entity identifier.
Definition snapshot.hpp:55
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.
Definition snapshot.hpp:114
basic_snapshot(basic_snapshot &&) noexcept=default
Default move constructor.
pointer data() const noexcept
Direct access to the internal packed array.
size_type size() const noexcept
Returns the number of elements in a sparse set.
bool contains(const entity_type entt) const noexcept
Checks if a sparse set contains an entity.
size_type free_list() const noexcept
Returns the head of the free list, if any.
bool empty() const noexcept
Checks whether a sparse set is empty.
bool remove(const entity_type entt)
Removes an entity from a sparse set if it exists.
Basic storage implementation.
Definition storage.hpp:229
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:757
void reserve(const size_type cap) override
Increases the capacity of a storage.
Definition storage.hpp:500
value_type & emplace(const entity_type entt, Args &&...args)
Assigns an entity to a storage and constructs its object.
Definition storage.hpp:663
std::tuple< const value_type & > get_as_tuple(const entity_type entt) const noexcept
Returns the object assigned to an entity as a tuple.
Definition storage.hpp:641
Associative container for key-value pairs with unique keys.
bool contains(const key_type &key) const
Checks if the container contains an element with a given key.
const_iterator cend() const noexcept
Returns an iterator to the end.
iterator find(const key_type &key)
Finds an element with a given key.
iterator erase(const_iterator pos)
Removes an element from a given position.
std::pair< iterator, bool > insert_or_assign(const key_type &key, Arg &&value)
Inserts an element into the container or assigns to the current element if the key already exists.
EnTT default namespace.
Definition dense_map.hpp:21
entity
Default entity identifier.
Definition fwd.hpp:13
constexpr entt_traits< Entity >::entity_type to_entity(const Entity value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:181
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:13
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:363
basic_storage< Type > storage
Alias declaration for the most common use case.
Definition fwd.hpp:72
constexpr void swap(compressed_pair< First, Second > &lhs, compressed_pair< First, Second > &rhs)
Swaps two compressed pair objects.
constexpr get_t< Type... > get
Variable template for lists of observed components.
Definition fwd.hpp:157
basic_registry<> registry
Alias declaration for the most common use case.
Definition fwd.hpp:82
@ ref
Aliasing mode, the object points to a non-const element.
Type hash.
Definition type_info.hpp:92