Browse Source

observer: no need to inherit from storage here

Michele Caini 1 year ago
parent
commit
f4ff91143f
1 changed files with 25 additions and 24 deletions
  1. 25 24
      src/entt/entity/observer.hpp

+ 25 - 24
src/entt/entity/observer.hpp

@@ -159,8 +159,8 @@ inline constexpr basic_collector<> collector{};
  * @tparam Allocator Type of allocator used to manage memory and elements.
  * @tparam Allocator Type of allocator used to manage memory and elements.
  */
  */
 template<typename Registry, typename Mask, typename Allocator>
 template<typename Registry, typename Mask, typename Allocator>
-class basic_observer: private basic_storage<Mask, typename Registry::entity_type, Allocator> {
-    using base_type = basic_storage<Mask, typename Registry::entity_type, Allocator>;
+class basic_observer {
+    using storage_type = basic_storage<Mask, typename Registry::entity_type, Allocator>;
 
 
     template<typename>
     template<typename>
     struct matcher_handler;
     struct matcher_handler;
@@ -168,7 +168,7 @@ class basic_observer: private basic_storage<Mask, typename Registry::entity_type
     template<typename... Reject, typename... Require, typename AnyOf>
     template<typename... Reject, typename... Require, typename AnyOf>
     struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, AnyOf>> {
     struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, AnyOf>> {
         template<std::size_t Index>
         template<std::size_t Index>
-        static void maybe_valid_if(base_type &obs, Registry &reg, const typename Registry::entity_type entt) {
+        static void maybe_valid_if(storage_type &obs, Registry &reg, const typename Registry::entity_type entt) {
             if(reg.template all_of<Require...>(entt) && !reg.template any_of<Reject...>(entt)) {
             if(reg.template all_of<Require...>(entt) && !reg.template any_of<Reject...>(entt)) {
                 if(!obs.contains(entt)) {
                 if(!obs.contains(entt)) {
                     obs.emplace(entt);
                     obs.emplace(entt);
@@ -179,21 +179,21 @@ class basic_observer: private basic_storage<Mask, typename Registry::entity_type
         }
         }
 
 
         template<std::size_t Index>
         template<std::size_t Index>
-        static void discard_if(base_type &obs, Registry &, const typename Registry::entity_type entt) {
+        static void discard_if(storage_type &obs, Registry &, const typename Registry::entity_type entt) {
             if(obs.contains(entt) && !(obs.get(entt) &= (~(1 << Index)))) {
             if(obs.contains(entt) && !(obs.get(entt) &= (~(1 << Index)))) {
                 obs.erase(entt);
                 obs.erase(entt);
             }
             }
         }
         }
 
 
         template<std::size_t Index>
         template<std::size_t Index>
-        static void connect(base_type &obs, Registry &reg) {
+        static void connect(storage_type &obs, Registry &reg) {
             (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
             reg.template on_update<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
             reg.template on_update<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
             reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
             reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
         }
         }
 
 
-        static void disconnect(base_type &obs, Registry &reg) {
+        static void disconnect(storage_type &obs, Registry &reg) {
             (reg.template on_destroy<Require>().disconnect(&obs), ...);
             (reg.template on_destroy<Require>().disconnect(&obs), ...);
             (reg.template on_construct<Reject>().disconnect(&obs), ...);
             (reg.template on_construct<Reject>().disconnect(&obs), ...);
             reg.template on_update<AnyOf>().disconnect(&obs);
             reg.template on_update<AnyOf>().disconnect(&obs);
@@ -204,7 +204,7 @@ class basic_observer: private basic_storage<Mask, typename Registry::entity_type
     template<typename... Reject, typename... Require, typename... NoneOf, typename... AllOf>
     template<typename... Reject, typename... Require, typename... NoneOf, typename... AllOf>
     struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, type_list<NoneOf...>, AllOf...>> {
     struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, type_list<NoneOf...>, AllOf...>> {
         template<std::size_t Index, typename... Ignore>
         template<std::size_t Index, typename... Ignore>
-        static void maybe_valid_if(base_type &obs, Registry &reg, const typename Registry::entity_type entt) {
+        static void maybe_valid_if(storage_type &obs, Registry &reg, const typename Registry::entity_type entt) {
             auto condition = [&reg, entt]() {
             auto condition = [&reg, entt]() {
                 if constexpr(sizeof...(Ignore) == 0) {
                 if constexpr(sizeof...(Ignore) == 0) {
                     return reg.template all_of<AllOf..., Require...>(entt) && !reg.template any_of<NoneOf..., Reject...>(entt);
                     return reg.template all_of<AllOf..., Require...>(entt) && !reg.template any_of<NoneOf..., Reject...>(entt);
@@ -223,14 +223,14 @@ class basic_observer: private basic_storage<Mask, typename Registry::entity_type
         }
         }
 
 
         template<std::size_t Index>
         template<std::size_t Index>
-        static void discard_if(base_type &obs, Registry &, const typename Registry::entity_type entt) {
+        static void discard_if(storage_type &obs, Registry &, const typename Registry::entity_type entt) {
             if(obs.contains(entt) && !(obs.get(entt) &= (~(1 << Index)))) {
             if(obs.contains(entt) && !(obs.get(entt) &= (~(1 << Index)))) {
                 obs.erase(entt);
                 obs.erase(entt);
             }
             }
         }
         }
 
 
         template<std::size_t Index>
         template<std::size_t Index>
-        static void connect(base_type &obs, Registry &reg) {
+        static void connect(storage_type &obs, Registry &reg) {
             (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<AllOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
             (reg.template on_construct<AllOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
@@ -239,7 +239,7 @@ class basic_observer: private basic_storage<Mask, typename Registry::entity_type
             (reg.template on_construct<NoneOf>().template connect<&discard_if<Index>>(obs), ...);
             (reg.template on_construct<NoneOf>().template connect<&discard_if<Index>>(obs), ...);
         }
         }
 
 
-        static void disconnect(base_type &obs, Registry &reg) {
+        static void disconnect(storage_type &obs, Registry &reg) {
             (reg.template on_destroy<Require>().disconnect(&obs), ...);
             (reg.template on_destroy<Require>().disconnect(&obs), ...);
             (reg.template on_construct<Reject>().disconnect(&obs), ...);
             (reg.template on_construct<Reject>().disconnect(&obs), ...);
             (reg.template on_construct<AllOf>().disconnect(&obs), ...);
             (reg.template on_construct<AllOf>().disconnect(&obs), ...);
@@ -250,14 +250,14 @@ class basic_observer: private basic_storage<Mask, typename Registry::entity_type
     };
     };
 
 
     template<typename... Matcher>
     template<typename... Matcher>
-    static void disconnect(Registry &reg, base_type &obs) {
+    static void disconnect(Registry &reg, storage_type &obs) {
         (matcher_handler<Matcher>::disconnect(obs, reg), ...);
         (matcher_handler<Matcher>::disconnect(obs, reg), ...);
     }
     }
 
 
     template<typename... Matcher, std::size_t... Index>
     template<typename... Matcher, std::size_t... Index>
     void connect(Registry &reg, std::index_sequence<Index...>) {
     void connect(Registry &reg, std::index_sequence<Index...>) {
         static_assert(sizeof...(Matcher) < std::numeric_limits<Mask>::digits, "Too many matchers");
         static_assert(sizeof...(Matcher) < std::numeric_limits<Mask>::digits, "Too many matchers");
-        (matcher_handler<Matcher>::template connect<Index>(static_cast<base_type &>(*this), reg), ...);
+        (matcher_handler<Matcher>::template connect<Index>(storage, reg), ...);
         release.template connect<&basic_observer::disconnect<Matcher...>>(reg);
         release.template connect<&basic_observer::disconnect<Matcher...>>(reg);
     }
     }
 
 
@@ -282,8 +282,8 @@ public:
      * @param allocator The allocator to use.
      * @param allocator The allocator to use.
      */
      */
     explicit basic_observer(const allocator_type &allocator)
     explicit basic_observer(const allocator_type &allocator)
-        : base_type{allocator},
-          release{} {}
+        : release{},
+          storage{allocator} {}
 
 
     /*! @brief Default copy constructor, deleted on purpose. */
     /*! @brief Default copy constructor, deleted on purpose. */
     basic_observer(const basic_observer &) = delete;
     basic_observer(const basic_observer &) = delete;
@@ -304,7 +304,7 @@ public:
     }
     }
 
 
     /*! @brief Default destructor. */
     /*! @brief Default destructor. */
-    ~basic_observer() noexcept override = default;
+    ~basic_observer() noexcept = default;
 
 
     /**
     /**
      * @brief Default copy assignment operator, deleted on purpose.
      * @brief Default copy assignment operator, deleted on purpose.
@@ -327,13 +327,13 @@ public:
     void connect(registry_type &reg, basic_collector<Matcher...>) {
     void connect(registry_type &reg, basic_collector<Matcher...>) {
         disconnect();
         disconnect();
         connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
         connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
-        base_type::clear();
+        storage.clear();
     }
     }
 
 
     /*! @brief Disconnects an observer from the registry it keeps track of. */
     /*! @brief Disconnects an observer from the registry it keeps track of. */
     void disconnect() {
     void disconnect() {
         if(release) {
         if(release) {
-            release(static_cast<base_type &>(*this));
+            release(storage);
             release.reset();
             release.reset();
         }
         }
     }
     }
@@ -343,7 +343,7 @@ public:
      * @return Number of elements.
      * @return Number of elements.
      */
      */
     [[nodiscard]] size_type size() const noexcept {
     [[nodiscard]] size_type size() const noexcept {
-        return base_type::size();
+        return storage.size();
     }
     }
 
 
     /**
     /**
@@ -351,7 +351,7 @@ public:
      * @return True if the observer is empty, false otherwise.
      * @return True if the observer is empty, false otherwise.
      */
      */
     [[nodiscard]] bool empty() const noexcept {
     [[nodiscard]] bool empty() const noexcept {
-        return base_type::empty();
+        return storage.empty();
     }
     }
 
 
     /**
     /**
@@ -367,7 +367,7 @@ public:
      * @return A pointer to the array of entities.
      * @return A pointer to the array of entities.
      */
      */
     [[nodiscard]] const entity_type *data() const noexcept {
     [[nodiscard]] const entity_type *data() const noexcept {
-        return base_type::data();
+        return storage.data();
     }
     }
 
 
     /**
     /**
@@ -378,7 +378,7 @@ public:
      * @return An iterator to the first entity of the observer.
      * @return An iterator to the first entity of the observer.
      */
      */
     [[nodiscard]] iterator begin() const noexcept {
     [[nodiscard]] iterator begin() const noexcept {
-        return base_type::base_type::begin();
+        return storage.storage_type::base_type::begin();
     }
     }
 
 
     /**
     /**
@@ -387,12 +387,12 @@ public:
      * observer.
      * observer.
      */
      */
     [[nodiscard]] iterator end() const noexcept {
     [[nodiscard]] iterator end() const noexcept {
-        return base_type::base_type::end();
+        return storage.storage_type::base_type::end();
     }
     }
 
 
     /*! @brief Clears the underlying container. */
     /*! @brief Clears the underlying container. */
     void clear() noexcept {
     void clear() noexcept {
-        base_type::clear();
+        storage.clear();
     }
     }
 
 
     /**
     /**
@@ -431,7 +431,8 @@ public:
     }
     }
 
 
 private:
 private:
-    delegate<void(base_type &)> release;
+    delegate<void(storage_type &)> release;
+    storage_type storage;
 };
 };
 
 
 } // namespace entt
 } // namespace entt