review: snapshot (close #219)

This commit is contained in:
Michele Caini
2019-04-14 15:31:00 +02:00
parent 7fc5e304e3
commit 8aacf3497e
2 changed files with 34 additions and 8 deletions

View File

@@ -47,7 +47,11 @@ class basic_snapshot {
const auto entt = *(first++);
if(reg.template has<Component>(entt)) {
archive(entt, reg.template get<Component>(entt));
if constexpr(std::is_empty_v<Component>) {
archive(entt);
} else {
archive(entt, reg.template get<Component>(entt));
}
}
}
}
@@ -138,7 +142,12 @@ public:
for(std::remove_const_t<decltype(sz)> i{}; i < sz; ++i) {
const auto entt = entities[i];
archive(entt, reg.template get<Component...>(entt));
if constexpr(std::is_empty_v<Component...>) {
archive(entt);
} else {
archive(entt, reg.template get<Component...>(entt));
}
};
} else {
(component<Component>(archive), ...);
@@ -219,7 +228,13 @@ class basic_snapshot_loader {
while(length--) {
Entity entt{};
Type instance{};
archive(entt, instance);
if constexpr(std::is_empty_v<Type>) {
archive(entt);
} else {
archive(entt, instance);
}
static constexpr auto destroyed = false;
force(reg, entt, destroyed);
reg.template assign<Type>(args..., entt, std::as_const(instance));
@@ -402,7 +417,13 @@ class basic_continuous_loader {
while(length--) {
Entity entt{};
Other instance{};
archive(entt, instance);
if constexpr(std::is_empty_v<Other>) {
archive(entt);
} else {
archive(entt, instance);
}
restore(entt);
(update(instance, member), ...);
func(map(entt), instance);

View File

@@ -3,6 +3,7 @@
#include <vector>
#include <cereal/archives/json.hpp>
#include <entt/entity/registry.hpp>
#include <entt/entity/helper.hpp>
struct position {
float x;
@@ -52,6 +53,7 @@ TEST(Snapshot, Full) {
auto e3 = source.create();
source.assign<timer>(e3, 1000, 100);
source.assign<entt::tag<"empty"_hs>>(e3);
source.destroy(e2);
auto v2 = source.current(e2);
@@ -60,12 +62,12 @@ TEST(Snapshot, Full) {
// output finishes flushing its contents when it goes out of scope
cereal::JSONOutputArchive output{storage};
source.snapshot().entities(output).destroyed(output)
.component<position, timer, relationship>(output);
.component<position, timer, relationship, entt::tag<"empty"_hs>>(output);
}
cereal::JSONInputArchive input{storage};
destination.loader().entities(input).destroyed(input)
.component<position, timer, relationship>(input);
.component<position, timer, relationship, entt::tag<"empty"_hs>>(input);
ASSERT_TRUE(destination.valid(e0));
ASSERT_TRUE(destination.has<position>(e0));
@@ -84,6 +86,7 @@ TEST(Snapshot, Full) {
ASSERT_TRUE(destination.valid(e3));
ASSERT_TRUE(destination.has<timer>(e3));
ASSERT_TRUE(destination.has<entt::tag<"empty"_hs>>(e3));
ASSERT_EQ(destination.get<timer>(e3).duration, 1000);
ASSERT_EQ(destination.get<timer>(e3).elapsed, 0);
}
@@ -118,18 +121,19 @@ TEST(Snapshot, Continuous) {
auto e3 = source.create();
source.assign<timer>(e3, 1000, 1000);
source.assign<relationship>(e3, e2);
source.assign<entt::tag<"empty"_hs>>(e3);
{
// output finishes flushing its contents when it goes out of scope
cereal::JSONOutputArchive output{storage};
source.snapshot().entities(output).component<position, relationship, timer>(output);
source.snapshot().entities(output).component<position, relationship, timer, entt::tag<"empty"_hs>>(output);
}
cereal::JSONInputArchive input{storage};
entt::continuous_loader loader{destination};
loader.entities(input)
.component<position, relationship>(input, &relationship::parent)
.component<timer>(input);
.component<timer, entt::tag<"empty"_hs>>(input);
ASSERT_FALSE(destination.valid(e0));
ASSERT_TRUE(loader.has(e0));
@@ -178,4 +182,5 @@ TEST(Snapshot, Continuous) {
ASSERT_EQ(destination.get<timer>(l3).elapsed, 0);
ASSERT_TRUE(destination.has<relationship>(l3));
ASSERT_EQ(destination.get<relationship>(l3).parent, l2);
ASSERT_TRUE(destination.has<entt::tag<"empty"_hs>>(l3));
}