Skip to content

Commit

Permalink
review snapshot/loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Apr 6, 2018
1 parent d81ecfe commit 5013a92
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/entt/entity/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ class Registry {
* more instances of this class in sync, as an example in a client-server
* architecture.
*
* @return A not movable and not copyable object to use to take snasphosts.
* @return A temporary object to use to take snasphosts.
*/
Snapshot<Entity> snapshot() const {
using follow_fn_type = entity_type(*)(const Registry &, entity_type);
Expand Down Expand Up @@ -1289,7 +1289,7 @@ class Registry {
* In case it isn't, all the data will be automatically deleted before to
* return.
*
* @return A not movable and not copyable object to use to load snasphosts.
* @return A temporary object to use to load snasphosts.
*/
SnapshotLoader<Entity> restore() {
using assure_fn_type = void(*)(Registry &, entity_type, bool);
Expand Down
59 changes: 33 additions & 26 deletions src/entt/entity/snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ class Snapshot final {
raw{raw}
{}

Snapshot(const Snapshot &) = default;
Snapshot(Snapshot &&) = default;

Snapshot & operator=(const Snapshot &) = default;
Snapshot & operator=(Snapshot &&) = default;

template<typename Component, typename Archive>
void get(Archive &archive, const Registry<Entity> &registry) {
const auto component = registry.template component<Component>();
Expand Down Expand Up @@ -83,6 +77,16 @@ class Snapshot final {
}

public:
/*! @brief Copying a snapshot isn't allowed. */
Snapshot(const Snapshot &) = delete;
/*! @brief Default move constructor. */
Snapshot(Snapshot &&) = default;

/*! @brief Copying a snapshot isn't allowed. @return This snapshot. */
Snapshot & operator=(const Snapshot &) = delete;
/*! @brief Default move assignment operator. @return This snapshot. */
Snapshot & operator=(Snapshot &&) = default;

/**
* @brief Puts aside all the entities that are still in use.
*
Expand All @@ -94,7 +98,7 @@ class Snapshot final {
* @return An object of this type to continue creating the snapshot.
*/
template<typename Archive>
Snapshot entities(Archive &archive) && {
Snapshot & entities(Archive &archive) {
archive(static_cast<Entity>(registry.size()));
registry.each([&archive, this](auto entity) { archive(entity); });
return *this;
Expand All @@ -111,7 +115,7 @@ class Snapshot final {
* @return An object of this type to continue creating the snapshot.
*/
template<typename Archive>
Snapshot destroyed(Archive &archive) && {
Snapshot & destroyed(Archive &archive) {
archive(static_cast<Entity>(size));

if(size) {
Expand Down Expand Up @@ -139,7 +143,7 @@ class Snapshot final {
* @return An object of this type to continue creating the snapshot.
*/
template<typename... Component, typename Archive>
Snapshot component(Archive &archive) && {
Snapshot & component(Archive &archive) {
using accumulator_type = int[];
accumulator_type accumulator = { 0, (get<Component>(archive, registry), 0)... };
(void)accumulator;
Expand All @@ -158,7 +162,7 @@ class Snapshot final {
* @return An object of this type to continue creating the snapshot.
*/
template<typename... Tag, typename Archive>
Snapshot tag(Archive &archive) && {
Snapshot & tag(Archive &archive) {
using accumulator_type = int[];
accumulator_type accumulator = { 0, (get<Tag>(archive), 0)... };
(void)accumulator;
Expand Down Expand Up @@ -199,12 +203,6 @@ class SnapshotLoader final {
assert(!registry.capacity());
}

SnapshotLoader(const SnapshotLoader &) = default;
SnapshotLoader(SnapshotLoader &&) = default;

SnapshotLoader & operator=(const SnapshotLoader &) = default;
SnapshotLoader & operator=(SnapshotLoader &&) = default;

template<typename Archive, typename Func>
void each(Archive &archive, Func func) {
Entity length{};
Expand Down Expand Up @@ -237,6 +235,16 @@ class SnapshotLoader final {
}

public:
/*! @brief Copying a snapshot loader isn't allowed. */
SnapshotLoader(const SnapshotLoader &) = delete;
/*! @brief Default move constructor. */
SnapshotLoader(SnapshotLoader &&) = default;

/*! @brief Copying a snapshot loader isn't allowed. @return This loader. */
SnapshotLoader & operator=(const SnapshotLoader &) = delete;
/*! @brief Default move assignment operator. @return This loader. */
SnapshotLoader & operator=(SnapshotLoader &&) = default;

/**
* @brief Restores entities that were in use during serialization.
*
Expand All @@ -248,7 +256,7 @@ class SnapshotLoader final {
* @return A valid loader to continue restoring data.
*/
template<typename Archive>
SnapshotLoader entities(Archive &archive) && {
SnapshotLoader & entities(Archive &archive) {
each(archive, [this](auto entity) {
static constexpr auto destroyed = false;
assure_fn(registry, entity, destroyed);
Expand All @@ -268,7 +276,7 @@ class SnapshotLoader final {
* @return A valid loader to continue restoring data.
*/
template<typename Archive>
SnapshotLoader destroyed(Archive &archive) && {
SnapshotLoader & destroyed(Archive &archive) {
each(archive, [this](auto entity) {
static constexpr auto destroyed = true;
assure_fn(registry, entity, destroyed);
Expand All @@ -291,7 +299,7 @@ class SnapshotLoader final {
* @return A valid loader to continue restoring data.
*/
template<typename... Component, typename Archive>
SnapshotLoader component(Archive &archive) && {
SnapshotLoader & component(Archive &archive) {
using accumulator_type = int[];
accumulator_type accumulator = { 0, (assign<Component>(archive), 0)... };
(void)accumulator;
Expand All @@ -312,14 +320,13 @@ class SnapshotLoader final {
* @return A valid loader to continue restoring data.
*/
template<typename... Tag, typename Archive>
SnapshotLoader tag(Archive &archive) && {
SnapshotLoader & tag(Archive &archive) {
using accumulator_type = int[];
accumulator_type accumulator = { 0, (attach<Tag>(archive), 0)... };
(void)accumulator;
return *this;
}


/**
* @brief Destroys those entities that have neither components nor tags.
*
Expand All @@ -330,7 +337,7 @@ class SnapshotLoader final {
*
* @return A valid loader to continue restoring data.
*/
SnapshotLoader orphans() && {
SnapshotLoader & orphans() {
registry.orphans([this](auto entity) {
registry.destroy(entity);
});
Expand Down Expand Up @@ -495,13 +502,13 @@ class ContinuousLoader final {
: registry{registry}
{}

/*! @brief Default copy constructor. */
ContinuousLoader(const ContinuousLoader &) = default;
/*! @brief Copying a snapshot loader isn't allowed. */
ContinuousLoader(const ContinuousLoader &) = delete;
/*! @brief Default move constructor. */
ContinuousLoader(ContinuousLoader &&) = default;

/*! @brief Default copy assignment operator. @return This loader. */
ContinuousLoader & operator=(const ContinuousLoader &) = default;
/*! @brief Copying a snapshot loader isn't allowed. @return This loader. */
ContinuousLoader & operator=(const ContinuousLoader &) = delete;
/*! @brief Default move assignment operator. @return This loader. */
ContinuousLoader & operator=(ContinuousLoader &&) = default;

Expand Down

0 comments on commit 5013a92

Please sign in to comment.