From 67e41e79f607e6421e9c8727ea1032ba04e0b91f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 7 Apr 2017 09:32:35 +0200 Subject: [PATCH] updated docs + added reset to the registry --- README.md | 23 +++++++++++++---------- src/component_pool.hpp | 5 +++++ src/registry.hpp | 29 +++++++++++++++++------------ test/component_pool.cpp | 13 +++++++++++++ test/registry.cpp | 15 +++++++++++++++ 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index f9b2f267e..cf9805a27 100644 --- a/README.md +++ b/README.md @@ -157,9 +157,10 @@ Once you have created a registry, the followings are the exposed member function * `size`: returns the number of entities still alive. * `capacity`: returns the maximum number of entities created till now. +* `empty`: returns `true` if at least an instance of `Component` exists, `false` otherwise. * `empty`: returns `true` if all the entities have been destroyed, `false` otherwise. -* `create`: creates a new entity and returns it, no components assigned. * `create`: creates a new entity and assigns it the given components, then returns the entity. +* `create`: creates a new entity and returns it, no components assigned. * `destroy`: destroys the entity and all its components. * `assign(entity, args...)`: assigns the given component to the entity and uses `args...` to initialize it. * `remove(entity)`: removes the given component from the entity. @@ -169,6 +170,7 @@ Once you have created a registry, the followings are the exposed member function * `clone(entity)`: clones an entity and all its components, then returns the new entity identifier. * `copy(from, to)`: copies a component from an entity to another one (both the entities must already have been assigned the component, undefined behaviour otherwise). * `copy(from, to)`: copies all the components and their contents from an entity to another one (comoonents are created or destroyed if needed). +* `reset()`: destroys all the instances of `Component`. * `reset()`: resets the pool and destroys all the entities and their components. * `view()`: gets a view of the entities that have the given components (see below for further details). @@ -267,15 +269,16 @@ Even thoug the underlying pool doesn't store the components separately, the regi specific actions (like `destroy` or `copy`). That's why they must be explicitly specified.
A generic pool should expose at least the following memeber functions: -* `template bool empty() const noexcept;` -* `template size_type capacity() const noexcept;` -* `template size_type size() const noexcept;` -* `template const entity_type * entities() const noexcept;` -* `template bool has(entity_type entity) const noexcept;` -* `template const Comp & get(entity_type entity) const noexcept;` -* `template Comp & get(entity_type entity) noexcept;` -* `template Comp & construct(entity_type entity, Args&&... args);` -* `template void destroy(entity_type entity);` +* `template bool empty() const noexcept;` +* `template size_type capacity() const noexcept;` +* `template size_type size() const noexcept;` +* `template const entity_type * entities() const noexcept;` +* `template bool has(entity_type entity) const noexcept;` +* `template const Comp & get(entity_type entity) const noexcept;` +* `template Comp & get(entity_type entity) noexcept;` +* `template Comp & construct(entity_type entity, Args&&... args);` +* `template void destroy(entity_type entity);` +* `template void reset();` * `void reset();` Good luck. If you come out with a more performant components pool, do not forget to make a PR so that I can add it to diff --git a/src/component_pool.hpp b/src/component_pool.hpp index 05c78daba..ca36190ad 100644 --- a/src/component_pool.hpp +++ b/src/component_pool.hpp @@ -176,6 +176,11 @@ struct ComponentPool final { std::get>(pools).destroy(entity); } + template + void reset() { + std::get>(pools).reset(); + } + void reset() { using accumulator_type = int[]; std::get>(pools).reset(); diff --git a/src/registry.hpp b/src/registry.hpp index c4a087f42..75ad6e38b 100644 --- a/src/registry.hpp +++ b/src/registry.hpp @@ -252,13 +252,22 @@ public: return count; } + template + bool empty() const noexcept { + return pool.template empty(); + } + bool empty() const noexcept { return available.size() == count; } - template - bool empty() const noexcept { - return pool.template empty(); + template + entity_type create() noexcept { + using accumulator_type = int[]; + auto entity = create(); + accumulator_type accumulator = { 0, (assign(entity), 0)... }; + (void)accumulator; + return entity; } entity_type create() noexcept { @@ -274,15 +283,6 @@ public: return entity; } - template - entity_type create() noexcept { - using accumulator_type = int[]; - auto entity = create(); - accumulator_type accumulator = { 0, (assign(entity), 0)... }; - (void)accumulator; - return entity; - } - void destroy(entity_type entity) { using accumulator_type = int[]; accumulator_type accumulator = { 0, (destroy(entity), 0)... }; @@ -339,6 +339,11 @@ public: (void)accumulator; } + template + void reset() { + pool.reset(); + } + void reset() { available.clear(); count = 0; diff --git a/test/component_pool.cpp b/test/component_pool.cpp index bb233280c..0f21f74e9 100644 --- a/test/component_pool.cpp +++ b/test/component_pool.cpp @@ -148,5 +148,18 @@ TEST(ComponentPool, EntitiesReset) { ASSERT_EQ(pool.entities()[1], typename pool_type::entity_type{1}); ASSERT_EQ(pool.entities()[2], typename pool_type::entity_type{3}); + ASSERT_EQ(pool.construct(0, 'c'), 'c'); + + ASSERT_FALSE(pool.empty()); + ASSERT_FALSE(pool.empty()); + + ASSERT_NO_THROW(pool.reset()); + + ASSERT_FALSE(pool.empty()); + ASSERT_TRUE(pool.empty()); + ASSERT_NO_THROW(pool.reset()); + + ASSERT_TRUE(pool.empty()); + ASSERT_TRUE(pool.empty()); } diff --git a/test/registry.cpp b/test/registry.cpp index 966758bf1..d609dcfd9 100644 --- a/test/registry.cpp +++ b/test/registry.cpp @@ -78,6 +78,21 @@ TEST(DefaultRegistry, Functionalities) { ASSERT_EQ(registry.size(), registry_type::size_type{0}); ASSERT_EQ(registry.capacity(), registry_type::size_type{0}); ASSERT_TRUE(registry.empty()); + + registry.create(); + + ASSERT_FALSE(registry.empty()); + ASSERT_FALSE(registry.empty()); + + ASSERT_NO_THROW(registry.reset()); + + ASSERT_TRUE(registry.empty()); + ASSERT_FALSE(registry.empty()); + + ASSERT_NO_THROW(registry.reset()); + + ASSERT_TRUE(registry.empty()); + ASSERT_TRUE(registry.empty()); } TEST(DefaultRegistry, ViewSingleComponent) {