Procházet zdrojové kódy

configurable entity type

Michele Caini před 9 roky
rodič
revize
92bc1ddb14
4 změnil soubory, kde provedl 63 přidání a 42 odebrání
  1. 16 5
      README.md
  2. 25 21
      src/component_pool.hpp
  3. 17 12
      src/registry.hpp
  4. 5 4
      test/component_pool.cpp

+ 16 - 5
README.md

@@ -132,7 +132,7 @@ long as it offers the expected interface.
 
 
 #### The Registry
 #### 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:
 * 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.
   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<std::uint16_t, Components...>{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.<br/>
+  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:
 * By using your own pool:
 
 
     ```
     ```
-    auto registry = entt::Registry<YourOwnPool<Components...>{args...};
+    auto registry = entt::Registry<DesiredEntityType, YourOwnPool<Components...>>{args...};
     ```
     ```
 
 
   Note that the registry expects a class template where the template parameters are the components to be managed.
   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<>
 template<>
-struct ComponentPool<MyComponent> final {
+struct ComponentPool<Entity, MyComponent> 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:
 A custom pool should expose at least the following member functions:
 
 
 * `bool empty() const noexcept;`
 * `bool empty() const noexcept;`
@@ -262,10 +273,10 @@ In other terms, `entt::Registry` has a template template parameter that can be u
 components:
 components:
 
 
 ```
 ```
-auto registry = entt::Registry<MyCustomPool<Component1, Component2>>{};
+auto registry = entt::Registry<Entity, MyCustomPool<Component1, Component2>>{};
 ```
 ```
 
 
-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.<br/>
 specific actions (like `destroy` or `copy`). That's why they must be explicitly specified.<br/>
 A generic pool should expose at least the following memeber functions:
 A generic pool should expose at least the following memeber functions:
 
 

+ 25 - 21
src/component_pool.hpp

@@ -15,15 +15,15 @@
 namespace entt {
 namespace entt {
 
 
 
 
-template<typename, typename...>
+template<typename, typename, typename...>
 struct ComponentPool;
 struct ComponentPool;
 
 
 
 
-template<typename Component>
-struct ComponentPool<Component> final {
+template<typename Entity, typename Component>
+struct ComponentPool<Entity, Component> final {
     using component_type = Component;
     using component_type = Component;
     using size_type = std::uint32_t;
     using size_type = std::uint32_t;
-    using entity_type = std::uint32_t;
+    using entity_type = Entity;
 
 
 private:
 private:
     bool valid(entity_type entity) const noexcept {
     bool valid(entity_type entity) const noexcept {
@@ -114,13 +114,17 @@ private:
 };
 };
 
 
 
 
-template<typename Component, typename... Components>
-struct  ComponentPool final {
-    using size_type = typename ComponentPool<Component>::size_type;
-    using entity_type = typename ComponentPool<Component>::entity_type;
+template<typename Entity, typename Component, typename... Components>
+class  ComponentPool final {
+    template<typename Comp>
+    using Pool = ComponentPool<Entity, Comp>;
+
+public:
+    using size_type = typename Pool<Component>::size_type;
+    using entity_type = typename Pool<Component>::entity_type;
 
 
     explicit ComponentPool(size_type dim = 4098) noexcept
     explicit ComponentPool(size_type dim = 4098) noexcept
-        : pools{ComponentPool<Component>{dim}, ComponentPool<Components>{dim}...}
+        : pools{Pool<Component>{dim}, Pool<Components>{dim}...}
     {
     {
         assert(!(dim < 0));
         assert(!(dim < 0));
     }
     }
@@ -133,32 +137,32 @@ struct  ComponentPool final {
 
 
     template<typename Comp>
     template<typename Comp>
     bool empty() const noexcept {
     bool empty() const noexcept {
-        return std::get<ComponentPool<Comp>>(pools).empty();
+        return std::get<Pool<Comp>>(pools).empty();
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     size_type capacity() const noexcept {
     size_type capacity() const noexcept {
-        return std::get<ComponentPool<Comp>>(pools).capacity();
+        return std::get<Pool<Comp>>(pools).capacity();
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     size_type size() const noexcept {
     size_type size() const noexcept {
-        return std::get<ComponentPool<Comp>>(pools).size();
+        return std::get<Pool<Comp>>(pools).size();
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     const entity_type * entities() const noexcept {
     const entity_type * entities() const noexcept {
-        return std::get<ComponentPool<Comp>>(pools).entities();
+        return std::get<Pool<Comp>>(pools).entities();
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     bool has(entity_type entity) const noexcept {
     bool has(entity_type entity) const noexcept {
-        return std::get<ComponentPool<Comp>>(pools).has(entity);
+        return std::get<Pool<Comp>>(pools).has(entity);
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     const Comp & get(entity_type entity) const noexcept {
     const Comp & get(entity_type entity) const noexcept {
-        return std::get<ComponentPool<Comp>>(pools).get(entity);
+        return std::get<Pool<Comp>>(pools).get(entity);
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
@@ -168,28 +172,28 @@ struct  ComponentPool final {
 
 
     template<typename Comp, typename... Args>
     template<typename Comp, typename... Args>
     Comp & construct(entity_type entity, Args... args) {
     Comp & construct(entity_type entity, Args... args) {
-        return std::get<ComponentPool<Comp>>(pools).construct(entity, args...);
+        return std::get<Pool<Comp>>(pools).construct(entity, args...);
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     void destroy(entity_type entity) {
     void destroy(entity_type entity) {
-        std::get<ComponentPool<Comp>>(pools).destroy(entity);
+        std::get<Pool<Comp>>(pools).destroy(entity);
     }
     }
 
 
     template<typename Comp>
     template<typename Comp>
     void reset() {
     void reset() {
-        std::get<ComponentPool<Comp>>(pools).reset();
+        std::get<Pool<Comp>>(pools).reset();
     }
     }
 
 
     void reset() {
     void reset() {
         using accumulator_type = int[];
         using accumulator_type = int[];
-        std::get<ComponentPool<Component>>(pools).reset();
-        accumulator_type accumulator = { (std::get<ComponentPool<Components>>(pools).reset(), 0)... };
+        std::get<Pool<Component>>(pools).reset();
+        accumulator_type accumulator = { (std::get<Pool<Components>>(pools).reset(), 0)... };
         (void)accumulator;
         (void)accumulator;
     }
     }
 
 
 private:
 private:
-    std::tuple<ComponentPool<Component>, ComponentPool<Components>...> pools;
+    std::tuple<Pool<Component>, Pool<Components>...> pools;
 };
 };
 
 
 
 

+ 17 - 12
src/registry.hpp

@@ -19,9 +19,9 @@ template<typename...>
 class View;
 class View;
 
 
 
 
-template<template<typename...> class Pool, typename... Components, typename Type, typename... Types>
-class View<Pool<Components...>, Type, Types...> final {
-    using pool_type = Pool<Components...>;
+template<template<typename...> class Pool, typename Entity, typename... Components, typename Type, typename... Types>
+class View<Pool<Entity, Components...>, Type, Types...> final {
+    using pool_type = Pool<Entity, Components...>;
     using entity_type = typename pool_type::entity_type;
     using entity_type = typename pool_type::entity_type;
 
 
     class ViewIterator {
     class ViewIterator {
@@ -121,9 +121,9 @@ private:
 };
 };
 
 
 
 
-template<template<typename...> class Pool, typename... Components, typename Type>
-class View<Pool<Components...>, Type> final {
-    using pool_type = Pool<Components...>;
+template<template<typename...> class Pool, typename Entity, typename... Components, typename Type>
+class View<Pool<Entity, Components...>, Type> final {
+    using pool_type = Pool<Entity, Components...>;
     using entity_type = typename pool_type::entity_type;
     using entity_type = typename pool_type::entity_type;
 
 
     struct ViewIterator {
     struct ViewIterator {
@@ -191,16 +191,17 @@ template<typename>
 struct Registry;
 struct Registry;
 
 
 
 
-template<template<typename...> class Pool, typename... Components>
-struct Registry<Pool<Components...>> final {
+template<template<typename...> class Pool, typename Entity, typename... Components>
+class Registry<Pool<Entity, Components...>> final {
+    using pool_type = Pool<Entity, Components...>;
+
+public:
     static_assert(sizeof...(Components) > 1, "!");
     static_assert(sizeof...(Components) > 1, "!");
 
 
-    using entity_type = typename Pool<Components...>::entity_type;
+    using entity_type = typename pool_type::entity_type;
     using size_type = std::size_t;
     using size_type = std::size_t;
 
 
 private:
 private:
-    using pool_type = Pool<Components...>;
-
     template<typename Comp>
     template<typename Comp>
     void destroy(entity_type entity) {
     void destroy(entity_type entity) {
         if(pool.template has<Comp>(entity)) {
         if(pool.template has<Comp>(entity)) {
@@ -362,8 +363,12 @@ private:
 };
 };
 
 
 
 
+template<typename Entity, typename... Components>
+using StandardRegistry = Registry<ComponentPool<Entity, Components...>>;
+
+
 template<typename... Components>
 template<typename... Components>
-using DefaultRegistry = Registry<ComponentPool<Components...>>;
+using DefaultRegistry = Registry<ComponentPool<std::uint32_t, Components...>>;
 
 
 
 
 }
 }

+ 5 - 4
test/component_pool.cpp

@@ -1,8 +1,9 @@
+#include <cstddef>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 #include <component_pool.hpp>
 #include <component_pool.hpp>
 
 
 TEST(ComponentPool, Functionalities) {
 TEST(ComponentPool, Functionalities) {
-    using pool_type = entt::ComponentPool<int, double>;
+    using pool_type = entt::ComponentPool<std::uint8_t, int, double>;
 
 
     pool_type pool{0};
     pool_type pool{0};
 
 
@@ -19,7 +20,7 @@ TEST(ComponentPool, Functionalities) {
 }
 }
 
 
 TEST(ComponentPool, ConstructDestroy) {
 TEST(ComponentPool, ConstructDestroy) {
-    using pool_type = entt::ComponentPool<double, int>;
+    using pool_type = entt::ComponentPool<std::uint8_t, double, int>;
 
 
     pool_type pool{4};
     pool_type pool{4};
 
 
@@ -107,7 +108,7 @@ TEST(ComponentPool, ConstructDestroy) {
 }
 }
 
 
 TEST(ComponentPool, HasGet) {
 TEST(ComponentPool, HasGet) {
-    using pool_type = entt::ComponentPool<int, char>;
+    using pool_type = entt::ComponentPool<std::uint8_t, int, char>;
 
 
     pool_type pool;
     pool_type pool;
     const pool_type &cpool = pool;
     const pool_type &cpool = pool;
@@ -126,7 +127,7 @@ TEST(ComponentPool, HasGet) {
 }
 }
 
 
 TEST(ComponentPool, EntitiesReset) {
 TEST(ComponentPool, EntitiesReset) {
-    using pool_type = entt::ComponentPool<int, char>;
+    using pool_type = entt::ComponentPool<std::uint8_t, int, char>;
 
 
     pool_type pool{2};
     pool_type pool{2};