Просмотр исходного кода

dispatcher/emitter: better assure

Michele Caini 6 лет назад
Родитель
Сommit
3853ff725f
3 измененных файлов с 22 добавлено и 29 удалено
  1. 1 1
      TODO
  2. 10 15
      src/entt/signal/dispatcher.hpp
  3. 11 13
      src/entt/signal/emitter.hpp

+ 1 - 1
TODO

@@ -26,8 +26,8 @@
 * Mission: get rid of named types
   - make it possible to use custom generators (eg for plugins)
     * dispatcher, emitter, registry
-    * dispatcher/emitter: id shouldn't be part of the class (upcoming feature discard will create a hole otherwise)
     * type_id_enabled and fallback on old-fashioned families otherwise
+    * add discard pool functionality (+ test)
   - reintroduce old-fashion family and add a new family-like thing with generators
   - families should be defined as out-of-class to guarantee the same identifiers for the same types
   - update doc: family, dispatcher, emitter, registry, meta, across boundaries

+ 10 - 15
src/entt/signal/dispatcher.hpp

@@ -6,7 +6,6 @@
 #include <memory>
 #include <cstddef>
 #include <utility>
-#include <iterator>
 #include <algorithm>
 #include <type_traits>
 #include "../config/config.h"
@@ -82,16 +81,16 @@ class dispatcher {
 
     template<typename Event>
     pool_handler<Event> & assure() {
-        static const auto index = std::distance(pools.cbegin(), std::find_if(pools.cbegin(), pools.cend(), [](auto &&curr) {
-            return curr && curr->id() == type_id_v<Event>;
-        }));
+        static std::size_t index{pools.size()};
 
-        if(!(index < pools.size())) {
-            pools.resize(index+1);
-        }
+        if(!(index < pools.size()) || pools[index]->id() != type_id_v<Event>) {
+            index = std::find_if(pools.cbegin(), pools.cend(), [](auto &&cpool) {
+                return cpool->id() == type_id_v<Event>;
+            }) - pools.begin();
 
-        if(!pools[index]) {
-            pools[index] = std::make_unique<pool_handler<Event>>();
+            if(index == pools.size()) {
+                pools.push_back(std::make_unique<pool_handler<Event>>());
+            }
         }
 
         return static_cast<pool_handler<Event> &>(*pools[index]);
@@ -190,9 +189,7 @@ public:
     void discard() ENTT_NOEXCEPT {
         if constexpr(sizeof...(Event) == 0) {
             std::for_each(pools.begin(), pools.end(), [](auto &&cpool) {
-                if(cpool) {
-                    cpool->clear();
-                }
+                cpool->clear();
             });
         } else {
             (assure<std::decay_t<Event>>().clear(), ...);
@@ -222,9 +219,7 @@ public:
      */
     void update() const {
         for(auto pos = pools.size(); pos; --pos) {
-            if(auto &cpool = pools[pos-1]; cpool) {
-                cpool->publish();
-            }
+            pools[pos-1]->publish();
         }
     }
 

+ 11 - 13
src/entt/signal/emitter.hpp

@@ -120,19 +120,19 @@ class emitter {
 
     template<typename Event>
     const pool_handler<Event> & assure() const {
-        static const auto index = std::distance(pools.cbegin(), std::find_if(pools.cbegin(), pools.cend(), [](auto &&curr) {
-            return curr && curr->id() == type_id_v<Event>;
-        }));
+        static std::size_t index{pools.size()};
 
-        if(!(index < pools.size())) {
-            pools.resize(index+1);
-        }
+        if(!(index < pools.size()) || pools[index]->id() != type_id_v<Event>) {
+            index = std::find_if(pools.cbegin(), pools.cend(), [](auto &&cpool) {
+                return cpool->id() == type_id_v<Event>;
+            }) - pools.begin();
 
-        if(!pools[index]) {
-            pools[index] = std::make_unique<pool_handler<Event>>();
+            if(index == pools.size()) {
+                pools.push_back(std::make_unique<pool_handler<Event>>());
+            }
         }
 
-        return static_cast<const pool_handler<Event> &>(*pools[index]);
+        return static_cast<pool_handler<Event> &>(*pools[index]);
     }
 
     template<typename Event>
@@ -286,9 +286,7 @@ public:
      */
     void clear() ENTT_NOEXCEPT {
         std::for_each(pools.begin(), pools.end(), [](auto &&cpool) {
-            if(cpool) {
-                cpool->clear();
-            }
+            cpool->clear();
         });
     }
 
@@ -308,7 +306,7 @@ public:
      */
     bool empty() const ENTT_NOEXCEPT {
         return std::all_of(pools.cbegin(), pools.cend(), [](auto &&cpool) {
-            return !cpool || cpool->empty();
+            return cpool->empty();
         });
     }