Skip to content

Commit

Permalink
snapshot: full support to stable types - close #1118
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Mar 28, 2024
1 parent abaa5c4 commit df87206
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/entt/entity/snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class basic_snapshot {
for(auto first = storage->data(), last = first + storage->size(); first != last; ++first) {
archive(*first);
}
} else if constexpr(component_traits<Type>::in_place_delete) {
using common_type = typename registry_type::common_type;

for(auto it = storage->common_type::rbegin(), last = storage->common_type::rend(); it != last; ++it) {
if(const auto entt = *it; entt == tombstone) {
archive(static_cast<entity_type>(null));
} else {
archive(entt);
std::apply([&archive](auto &&...args) { (archive(std::forward<decltype(args)>(args)), ...); }, storage->get_as_tuple(entt));
}
}
} else {
for(auto elem: storage->reach()) {
std::apply([&archive](auto &&...args) { (archive(std::forward<decltype(args)>(args)), ...); }, elem);
Expand Down
55 changes: 53 additions & 2 deletions test/entt/entity/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <entt/signal/sigh.hpp>
#include "../../common/config.h"
#include "../../common/empty.h"
#include "../../common/pointer_stable.h"

struct shadow {
entt::entity target{entt::null};
Expand Down Expand Up @@ -93,7 +94,7 @@ TEST(BasicSnapshot, GetType) {
constexpr auto number_of_entities = 3u;

std::array<entt::entity, number_of_entities> entity{};
const std::array<int, number_of_entities> value{1, 2, 3};
const std::array value{1, 2, 3};

registry.create(entity.begin(), entity.end());
registry.insert<int>(entity.begin(), entity.end(), value.begin());
Expand Down Expand Up @@ -130,6 +131,56 @@ TEST(BasicSnapshot, GetType) {
ASSERT_EQ(entt::any_cast<int>(data[4u]), value[2u]);
}

TEST(BasicSnapshot, GetPointerStableType) {
using namespace entt::literals;
using traits_type = entt::entt_traits<entt::entity>;

entt::registry registry;
const entt::basic_snapshot snapshot{registry};
const auto &storage = registry.storage<test::pointer_stable>();
constexpr auto number_of_entities = 3u;

std::array<entt::entity, number_of_entities> entity{};
const std::array<test::pointer_stable, number_of_entities> value{1, 2, 3};

registry.create(entity.begin(), entity.end());
registry.insert<test::pointer_stable>(entity.begin(), entity.end(), value.begin());
registry.destroy(entity[1u]);

std::vector<entt::any> data{};
auto archive = [&data](auto &&elem) { data.emplace_back(std::forward<decltype(elem)>(elem)); };

snapshot.get<test::pointer_stable>(archive, "other"_hs);

ASSERT_EQ(data.size(), 1u);

ASSERT_NE(entt::any_cast<typename traits_type::entity_type>(&data[0u]), nullptr);
ASSERT_EQ(entt::any_cast<typename traits_type::entity_type>(data[0u]), 0u);

data.clear();
snapshot.get<test::pointer_stable>(archive);

ASSERT_EQ(data.size(), 6u);

ASSERT_NE(entt::any_cast<typename traits_type::entity_type>(&data[0u]), nullptr);
ASSERT_EQ(entt::any_cast<typename traits_type::entity_type>(data[0u]), storage.size());

ASSERT_NE(entt::any_cast<entt::entity>(&data[1u]), nullptr);
ASSERT_EQ(entt::any_cast<entt::entity>(data[1u]), entity[0u]);

ASSERT_NE(entt::any_cast<test::pointer_stable>(&data[2u]), nullptr);
ASSERT_EQ(entt::any_cast<test::pointer_stable>(data[2u]), value[0u]);

ASSERT_NE(entt::any_cast<entt::entity>(&data[3u]), nullptr);
ASSERT_EQ(entt::any_cast<entt::entity>(data[3u]), static_cast<entt::entity>(entt::null));

ASSERT_NE(entt::any_cast<entt::entity>(&data[4u]), nullptr);
ASSERT_EQ(entt::any_cast<entt::entity>(data[4u]), entity[2u]);

ASSERT_NE(entt::any_cast<test::pointer_stable>(&data[5u]), nullptr);
ASSERT_EQ(entt::any_cast<test::pointer_stable>(data[5u]), value[2u]);
}

TEST(BasicSnapshot, GetEmptyType) {
using namespace entt::literals;
using traits_type = entt::entt_traits<entt::entity>;
Expand Down Expand Up @@ -179,7 +230,7 @@ TEST(BasicSnapshot, GetTypeSparse) {
constexpr auto number_of_entities = 3u;

std::array<entt::entity, number_of_entities> entity{};
const std::array<int, number_of_entities> value{1, 2, 3};
const std::array value{1, 2, 3};

registry.create(entity.begin(), entity.end());
registry.insert<int>(entity.begin(), entity.end(), value.begin());
Expand Down

0 comments on commit df87206

Please sign in to comment.