diff --git a/TODO b/TODO
index 536f4abd0..d73812afa 100644
--- a/TODO
+++ b/TODO
@@ -7,12 +7,10 @@
* define basic reactive systems (track entities to which component is attached, track entities from which component is removed, and so on)
* define systems as composable mixins (initializazion, reactive, update, whatever) with flexible auto-detected arguments (registry, views, etc)
* runner proposal: https://en.wikipedia.org/wiki/Fork%E2%80%93join_model https://slide-rs.github.io/specs/03_dispatcher.html
-* optimize for empty components, it would be a mid improvement in terms of memory usage (see std::is_empty)
* deep copy of a registry (or use the snapshot stuff to copy components and keep intact ids at least)
* is it possible to iterate all the components assigned to an entity through a common base class?
* can we do more for shared libraries? who knows... see #144
* work stealing job system (see #100)
-* composable looper so as to pack erased systems, compose runners at different rates and run them at once in the loop
* meta: sort of meta view based on meta stuff to iterate entities, void * and meta info objects
* meta: move-to-head optimization when searching by name on parts (data, func, etc)
* hashed string: add implicit check on construction for uniqueness (optional)
@@ -22,4 +20,7 @@
* add on-the-fly sort functionality (is it possible?)
* write/show how to work with missing components using and-strategies
* write/show how to create an archetype based model on top of EnTT
-* mention hunter and conan in the readme file, section packaging tools
+* mention hunter in the readme file, section packaging tools
+* monostate: make constraint trivially copyable due to atomic optional
+* monostate: use template variable to avoid {}
+* travis + windows is now available, try it
diff --git a/docs/entity.md b/docs/entity.md
index e603802fd..86b1ea3d9 100644
--- a/docs/entity.md
+++ b/docs/entity.md
@@ -839,6 +839,8 @@ All of them have pros and cons to take in consideration. In particular:
they don't have any type of initialization.
* They are the best tool for iterating multiple components when most entities
have them all.
+ * They are also the only type of views that supports filters without incurring
+ in a loss of performance during iterations.
Cons:
@@ -895,8 +897,9 @@ To sum up and as a rule of thumb:
* Use a persistent view when you want to iterate multiple components and each
component is assigned to a great number of entities but the intersection
between the sets of entities is small.
-* Use a persistent view in all the cases where a standard view wouldn't fit well
- otherwise.
+* Use a persistent view when you want to set more complex filters that would
+ translate otherwise in a bunch of `if`s within a loop.
+* Use a persistent view in all the cases where a standard view doesn't fit well.
* Finally, in case you don't know at compile-time what are the components to
use, choose a runtime view and set them during execution.
@@ -1019,17 +1022,30 @@ mind that it works only with the components of the view itself.
## Persistent View
A persistent view returns all the entities and only the entities that have at
-least the given components. Moreover, it's guaranteed that the entity list is
-tightly packed in memory for fast iterations.
+least the given components and respect the given filters. Moreover, it's
+guaranteed that the entity list is tightly packed in memory for fast
+iterations.
In general, persistent views don't stay true to the order of any set of
components unless users explicitly sort them.
-Persistent views can be used only to iterate multiple components at once:
+Persistent views are used mainly to iterate multiple components at once:
```cpp
auto view = registry.persistent_view();
```
+Moreover, filters can be applied to a persistent view to some extents:
+
+```cpp
+auto view = registry.persistent_view(entt::type_list);
+```
+
+In this case, the view will return all the entities that have both components
+`position` and `velocity` but don't have component `renderable`.
+Exclusive filters (ie the entities that have either `position` or `velocity`)
+aren't supported for performance reasons. Similarly, a filter cannot be applied
+to a persistent view with an empty template parameters list.
+
There is no need to store views around for they are extremely cheap to
construct, even though they can be copied without problems and reused freely. In
fact, they return newly created and correctly initialized iterators whenever
@@ -1180,7 +1196,7 @@ views.
# Types: const, non-const and all in between
The `registry` class offers two overloads for most of the member functions used
-to construct views: a const one and a non-const one. The former accepts both
+to construct views: a const version and a non-const one. The former accepts both
const and non-const types as template parameters, the latter accepts only const
types instead.
It means that views can be constructed also from a const registry and they
diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp
new file mode 100644
index 000000000..99e6b48eb
--- /dev/null
+++ b/src/entt/core/type_traits.hpp
@@ -0,0 +1,16 @@
+#ifndef ENTT_CORE_TYPE_TRAITS_HPP
+#define ENTT_CORE_TYPE_TRAITS_HPP
+
+
+namespace entt {
+
+
+/*! @brief A class to use to push around lists of types, nothing more. */
+template
+struct type_list {};
+
+
+}
+
+
+#endif // ENTT_CORE_TYPE_TRAITS_HPP
diff --git a/src/entt/entity/actor.hpp b/src/entt/entity/actor.hpp
index 41639cdf1..97c827305 100644
--- a/src/entt/entity/actor.hpp
+++ b/src/entt/entity/actor.hpp
@@ -32,7 +32,7 @@ struct actor {
* @brief Constructs an actor by using the given registry.
* @param reg An entity-component system properly initialized.
*/
- actor(registry ®)
+ actor(registry_type ®)
: reg{®}, entt{reg.create()}
{}
diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp
index 12c13fe1b..4cd9a6215 100644
--- a/src/entt/entity/registry.hpp
+++ b/src/entt/entity/registry.hpp
@@ -17,6 +17,7 @@
#include "../config/config.h"
#include "../core/algorithm.hpp"
#include "../core/family.hpp"
+#include "../core/type_traits.hpp"
#include "../signal/sigh.hpp"
#include "entity.hpp"
#include "entt_traits.hpp"
@@ -43,12 +44,9 @@ class registry {
using component_family = family;
using handler_family = family;
using component_signal_type = sigh;
- using pool_signal_type = sigh;
+ using pool_signal_type = sigh;
using traits_type = entt_traits;
- template
- using handler_type = sparse_set::size_type, N>>;
-
template
struct component_pool: sparse_set {
component_pool(registry *reg) ENTT_NOEXCEPT
@@ -81,27 +79,61 @@ class registry {
registry *reg;
};
- template
- static void creating(registry ®, const Entity entity) {
- if((reg.pool().has(entity) && ...)) {
- auto *handler = static_cast *>(reg.handlers[handler_family::type].get());
- handler->construct(entity, reg.pools[component_family::type]->get(entity)...);
- }
- }
+ template
+ struct handler_pool;
- template
- static void destroying(registry ®, const Entity entity) {
- auto *handler = static_cast *>(reg.handlers[handler_family::type].get());
- const sparse_set &cpool = reg.pool();
- const auto last = *cpool.begin();
-
- if(handler->has(last)) {
- handler->get(last)[Index] = cpool.get(entity);
+ template
+ struct handler_pool, type_list>: sparse_set::size_type, sizeof...(Component)>> {
+ void candidate(registry ®, const Entity entity) {
+ if((reg.pool().has(entity) && ...) && !(reg.pool().has(entity) || ...)) {
+ handler_pool::construct(entity, reg.pools[component_family::type]->get(entity)...);
+ }
}
- if(handler->has(entity)) {
- handler->destroy(entity);
+ template
+ void check(registry ®, const Entity entity) {
+ const sparse_set &cpool = reg.pool();
+ const auto last = *cpool.begin();
+
+ if(handler_pool::has(last)) {
+ handler_pool::get(last)[Index] = cpool.get(entity);
+ }
+
+ if(handler_pool::has(entity)) {
+ handler_pool::destroy(entity);
+ }
}
+
+ void discard(registry &, const Entity entity) {
+ if(handler_pool::has(entity)) {
+ handler_pool::destroy(entity);
+ }
+ }
+
+ void rebuild(registry ®, const typename component_family::family_type ctype) {
+ auto index = sizeof...(Component);
+ decltype(index) cnt{};
+
+ ((index = (component_family::type == ctype) ? cnt++ : (static_cast(++cnt), index)), ...);
+
+ if(index != sizeof...(Component)) {
+ auto begin = sparse_set::begin();
+ const auto &cpool = *reg.pools[ctype];
+
+ for(auto &&indexes: *this) {
+ indexes[index] = cpool.get(*(begin++));
+ }
+ }
+ }
+ };
+
+ template
+ void connect(handler_pool, type_list> *handler, std::index_sequence) {
+ using handler_type = handler_pool, type_list>;
+ (pool().construction().template connect<&handler_type::candidate>(handler), ...);
+ (pool().construction().template connect<&handler_type::discard>(handler), ...);
+ (pool().destruction().template connect<&handler_type::template check>(handler), ...);
+ (pool().destruction().template connect<&handler_type::candidate>(handler), ...);
}
template
@@ -121,33 +153,6 @@ class registry {
return const_cast> &>(std::as_const(*this).template pool());
}
- template
- void connect(std::index_sequence) {
- (pool().construction().template connect<®istry::creating>(), ...);
- (pool().destruction().template connect<®istry::destroying>(), ...);
- }
-
- template
- void rebuild(const typename component_family::family_type ctype, std::index_sequence) {
- auto index = sizeof...(Indexes);
- ((index = (component_family::type == ctype) ? Indexes : index), ...);
-
- if(index != sizeof...(Indexes)) {
- auto *handler = static_cast *>(handlers[handler_family::type].get());
- auto begin = handler->sparse_set::begin();
- const auto &cpool = *pools[ctype];
-
- for(auto &&indexes: *handler) {
- indexes[index] = cpool.get(*(begin++));
- }
- }
- }
-
- template
- void refresh(const typename component_family::family_type ctype) {
- rebuild(ctype, std::make_index_sequence{});
- }
-
template
void assure() {
const auto ctype = component_family::type;
@@ -920,7 +925,7 @@ public:
void sort(Compare compare, Sort sort = Sort{}, Args &&... args) {
assure();
pool().sort(std::move(compare), std::move(sort), std::forward(args)...);
- invalidate.publish(component_family::type);
+ invalidate.publish(*this, component_family::type);
}
/**
@@ -958,7 +963,7 @@ public:
assure();
assure();
pool().respect(pool());
- invalidate.publish(component_family::type);
+ invalidate.publish(*this, component_family::type);
}
/**
@@ -1202,8 +1207,9 @@ public:
*
* Persistent views are the right choice to iterate entities when the number
* of components grows up and the most of the entities have all the given
- * components.
- * However they have also drawbacks:
+ * components. They are also the only type of views that supports filters
+ * without incurring in a loss of performance during iterations.
+ * However, persistent views have also drawbacks:
*
* * Each kind of persistent view requires a dedicated data structure that
* is allocated within the registry and it increases memory pressure.
@@ -1214,12 +1220,6 @@ public:
* That being said, persistent views are an incredibly powerful tool if used
* with care and offer a boost of performance undoubtedly.
*
- * @note
- * Consider to use the `prepare` member function to initialize the internal
- * data structures used by persistent views when the registry is still
- * empty. Initialization could be a costly operation otherwise and it will
- * be performed the very first time each view is created.
- *
* @sa view
* @sa view
* @sa persistent_view
@@ -1227,11 +1227,13 @@ public:
* @sa runtime_view
*
* @tparam Component Types of components used to construct the view.
+ * @tparam Exclude Types of components used to filter the view.
* @return A newly created persistent view.
*/
- template
- entt::persistent_view persistent_view() {
- static_assert(sizeof...(Component) > 1);
+ template
+ entt::persistent_view persistent_view(type_list = type_list<>{}) {
+ static_assert(sizeof...(Component));
+ using handler_type = handler_pool, type_list>;
const auto htype = handler_family::type;
if(!(htype < handlers.size())) {
@@ -1240,19 +1242,24 @@ public:
if(!handlers[htype]) {
(assure(), ...);
- connect(std::make_index_sequence{});
- invalidate.sink().template connect<®istry::refresh>(this);
+ (assure(), ...);
- handlers[htype] = std::make_unique>();
- auto *handler = static_cast *>(handlers[htype].get());
+ auto handler = std::make_unique();
+
+ connect(handler.get(), std::make_index_sequence{});
+ invalidate.sink().template connect<&handler_type::rebuild>(handler.get());
for(const auto entity: view()) {
- handler->construct(entity, pools[component_family::type]->get(entity)...);
+ if(!(pool().has(entity) || ...)) {
+ handler->construct(entity, pools[component_family::type]->get(entity)...);
+ }
}
+
+ handlers[htype] = std::move(handler);
}
return {
- static_cast *>(handlers[htype].get()),
+ static_cast(handlers[htype].get()),
&pool()...
};
}
@@ -1272,8 +1279,9 @@ public:
*
* Persistent views are the right choice to iterate entities when the number
* of components grows up and the most of the entities have all the given
- * components.
- * However they have also drawbacks:
+ * components. They are also the only type of views that supports filters
+ * without incurring in a loss of performance during iterations.
+ * However, persistent views have also drawbacks:
*
* * Each kind of persistent view requires a dedicated data structure that
* is allocated within the registry and it increases memory pressure.
@@ -1284,12 +1292,6 @@ public:
* That being said, persistent views are an incredibly powerful tool if used
* with care and offer a boost of performance undoubtedly.
*
- * @note
- * Consider to use the `prepare` member function to initialize the internal
- * data structures used by persistent views when the registry is still
- * empty. Initialization could be a costly operation otherwise and it will
- * be performed the very first time each view is created.
- *
* @sa view
* @sa view
* @sa persistent_view
@@ -1297,12 +1299,13 @@ public:
* @sa runtime_view
*
* @tparam Component Types of components used to construct the view.
+ * @tparam Exclude Types of components used to filter the view.
* @return A newly created persistent view.
*/
- template
- inline entt::persistent_view persistent_view() const {
+ template
+ inline entt::persistent_view persistent_view(type_list = type_list<>{}) const {
static_assert(std::conjunction_v...>);
- return const_cast(this)->persistent_view();
+ return const_cast(this)->persistent_view(type_list{});
}
/**
diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp
index a199ba56a..2cda81047 100644
--- a/src/entt/entity/view.hpp
+++ b/src/entt/entity/view.hpp
@@ -70,7 +70,7 @@ class registry;
*/
template
class persistent_view final {
- static_assert(sizeof...(Component) > 1);
+ static_assert(sizeof...(Component));
/*! @brief A registry is allowed to create views. */
friend class registry;
diff --git a/src/entt/entt.hpp b/src/entt/entt.hpp
index 1af3c789d..1ffbee467 100644
--- a/src/entt/entt.hpp
+++ b/src/entt/entt.hpp
@@ -3,6 +3,7 @@
#include "core/hashed_string.hpp"
#include "core/ident.hpp"
#include "core/monostate.hpp"
+#include "core/type_traits.hpp"
#include "core/utility.hpp"
#include "entity/actor.hpp"
#include "entity/entity.hpp"
diff --git a/test/entt/entity/view.cpp b/test/entt/entity/view.cpp
index 130f459d9..7eb17afc8 100644
--- a/test/entt/entity/view.cpp
+++ b/test/entt/entity/view.cpp
@@ -1,5 +1,6 @@
#include
#include
+#include
#include
#include
#include
@@ -251,6 +252,71 @@ TEST(PersistentView, Find) {
ASSERT_EQ(++view.find(e0), view.end());
}
+TEST(PersistentView, SingleComponent) {
+ entt::registry<> registry;
+ const auto view = registry.persistent_view();
+
+ registry.assign(registry.create());
+
+ const auto entity = registry.create();
+ registry.assign(entity);
+
+ registry.assign(registry.create());
+
+ registry.destroy(entity);
+ registry.assign(registry.create());
+
+ ASSERT_TRUE(std::equal(view.begin(), view.end(), registry.view().begin()));
+}
+
+TEST(PersistentView, ExcludedComponents) {
+ entt::registry<> registry;
+
+ const auto e0 = registry.create();
+ registry.assign(e0, 0);
+
+ const auto e1 = registry.create();
+ registry.assign(e1, 1);
+ registry.assign(e1);
+
+ const auto view = registry.persistent_view(entt::type_list{});
+
+ const auto e2 = registry.create();
+ registry.assign(e2, 2);
+
+ const auto e3 = registry.create();
+ registry.assign(e3, 3);
+ registry.assign(e3);
+
+ for(const auto entity: view) {
+ if(entity == e0) {
+ ASSERT_EQ(view.get(e0), 0);
+ } else if(entity == e2) {
+ ASSERT_EQ(view.get(e2), 2);
+ } else {
+ FAIL();
+ }
+ }
+
+ registry.assign(e0);
+ registry.assign(e2);
+
+ ASSERT_TRUE(view.empty());
+
+ registry.remove(e1);
+ registry.remove(e3);
+
+ for(const auto entity: view) {
+ if(entity == e1) {
+ ASSERT_EQ(view.get(e1), 1);
+ } else if(entity == e3) {
+ ASSERT_EQ(view.get(e3), 3);
+ } else {
+ FAIL();
+ }
+ }
+}
+
TEST(SingleComponentView, Functionalities) {
entt::registry<> registry;
auto view = registry.view();