diff --git a/README.md b/README.md index 2d24f5718..d542850e4 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ long as it offers the expected interface. #### The Registry -There are two options to instantiate your own registry: +There are three options to instantiate your own registry: * By using the default one: @@ -142,10 +142,19 @@ There are two options to instantiate your own registry: That is, you must provide the whole list of components to be registered with the default registry. +* By using the standard one: + + ``` + auto registry = entt::StandardRegistry{args...}; + ``` + + That is, you must provide the whole list of components to be registered with the default registry **and** the desired type for the entities.
+ Note that the default type is `std::uint32_t`, that is larger enough for almost all the games but also too big for the most of the games. + * By using your own pool: ``` - auto registry = entt::Registry{args...}; + auto registry = entt::Registry>{args...}; ``` Note that the registry expects a class template where the template parameters are the components to be managed. @@ -233,11 +242,13 @@ In particular: ``` template<> -struct ComponentPool final { +struct ComponentPool final { // ... }; ``` +Where `Entity` is the desired type for the entities, `MyComponent` the type of the component to be stored. + A custom pool should expose at least the following member functions: * `bool empty() const noexcept;` @@ -262,10 +273,10 @@ In other terms, `entt::Registry` has a template template parameter that can be u components: ``` -auto registry = entt::Registry>{}; +auto registry = entt::Registry>{}; ``` -Even thoug the underlying pool doesn't store the components separately, the registry must know them to be able to do +Even though the underlying pool doesn't store the components separately, the registry must know them to be able to do 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: diff --git a/src/component_pool.hpp b/src/component_pool.hpp index 26f7533cb..578306f23 100644 --- a/src/component_pool.hpp +++ b/src/component_pool.hpp @@ -15,15 +15,15 @@ namespace entt { -template +template struct ComponentPool; -template -struct ComponentPool final { +template +struct ComponentPool final { using component_type = Component; using size_type = std::uint32_t; - using entity_type = std::uint32_t; + using entity_type = Entity; private: bool valid(entity_type entity) const noexcept { @@ -114,13 +114,17 @@ private: }; -template -struct ComponentPool final { - using size_type = typename ComponentPool::size_type; - using entity_type = typename ComponentPool::entity_type; +template +class ComponentPool final { + template + using Pool = ComponentPool; + +public: + using size_type = typename Pool::size_type; + using entity_type = typename Pool::entity_type; explicit ComponentPool(size_type dim = 4098) noexcept - : pools{ComponentPool{dim}, ComponentPool{dim}...} + : pools{Pool{dim}, Pool{dim}...} { assert(!(dim < 0)); } @@ -133,32 +137,32 @@ struct ComponentPool final { template bool empty() const noexcept { - return std::get>(pools).empty(); + return std::get>(pools).empty(); } template size_type capacity() const noexcept { - return std::get>(pools).capacity(); + return std::get>(pools).capacity(); } template size_type size() const noexcept { - return std::get>(pools).size(); + return std::get>(pools).size(); } template const entity_type * entities() const noexcept { - return std::get>(pools).entities(); + return std::get>(pools).entities(); } template bool has(entity_type entity) const noexcept { - return std::get>(pools).has(entity); + return std::get>(pools).has(entity); } template const Comp & get(entity_type entity) const noexcept { - return std::get>(pools).get(entity); + return std::get>(pools).get(entity); } template @@ -168,28 +172,28 @@ struct ComponentPool final { template Comp & construct(entity_type entity, Args... args) { - return std::get>(pools).construct(entity, args...); + return std::get>(pools).construct(entity, args...); } template void destroy(entity_type entity) { - std::get>(pools).destroy(entity); + std::get>(pools).destroy(entity); } template void reset() { - std::get>(pools).reset(); + std::get>(pools).reset(); } void reset() { using accumulator_type = int[]; - std::get>(pools).reset(); - accumulator_type accumulator = { (std::get>(pools).reset(), 0)... }; + std::get>(pools).reset(); + accumulator_type accumulator = { (std::get>(pools).reset(), 0)... }; (void)accumulator; } private: - std::tuple, ComponentPool...> pools; + std::tuple, Pool...> pools; }; diff --git a/src/registry.hpp b/src/registry.hpp index 3c93358aa..08185f8fe 100644 --- a/src/registry.hpp +++ b/src/registry.hpp @@ -19,9 +19,9 @@ template class View; -template class Pool, typename... Components, typename Type, typename... Types> -class View, Type, Types...> final { - using pool_type = Pool; +template class Pool, typename Entity, typename... Components, typename Type, typename... Types> +class View, Type, Types...> final { + using pool_type = Pool; using entity_type = typename pool_type::entity_type; class ViewIterator { @@ -121,9 +121,9 @@ private: }; -template class Pool, typename... Components, typename Type> -class View, Type> final { - using pool_type = Pool; +template class Pool, typename Entity, typename... Components, typename Type> +class View, Type> final { + using pool_type = Pool; using entity_type = typename pool_type::entity_type; struct ViewIterator { @@ -191,16 +191,17 @@ template struct Registry; -template class Pool, typename... Components> -struct Registry> final { +template class Pool, typename Entity, typename... Components> +class Registry> final { + using pool_type = Pool; + +public: static_assert(sizeof...(Components) > 1, "!"); - using entity_type = typename Pool::entity_type; + using entity_type = typename pool_type::entity_type; using size_type = std::size_t; private: - using pool_type = Pool; - template void destroy(entity_type entity) { if(pool.template has(entity)) { @@ -362,8 +363,12 @@ private: }; +template +using StandardRegistry = Registry>; + + template -using DefaultRegistry = Registry>; +using DefaultRegistry = Registry>; } diff --git a/test/component_pool.cpp b/test/component_pool.cpp index 0f21f74e9..8856a0792 100644 --- a/test/component_pool.cpp +++ b/test/component_pool.cpp @@ -1,8 +1,9 @@ +#include #include #include TEST(ComponentPool, Functionalities) { - using pool_type = entt::ComponentPool; + using pool_type = entt::ComponentPool; pool_type pool{0}; @@ -19,7 +20,7 @@ TEST(ComponentPool, Functionalities) { } TEST(ComponentPool, ConstructDestroy) { - using pool_type = entt::ComponentPool; + using pool_type = entt::ComponentPool; pool_type pool{4}; @@ -107,7 +108,7 @@ TEST(ComponentPool, ConstructDestroy) { } TEST(ComponentPool, HasGet) { - using pool_type = entt::ComponentPool; + using pool_type = entt::ComponentPool; pool_type pool; const pool_type &cpool = pool; @@ -126,7 +127,7 @@ TEST(ComponentPool, HasGet) { } TEST(ComponentPool, EntitiesReset) { - using pool_type = entt::ComponentPool; + using pool_type = entt::ComponentPool; pool_type pool{2};