Browse Source

dispatcher: const assure

Michele Caini 3 years ago
parent
commit
5ab0f9e4c0
1 changed files with 13 additions and 5 deletions
  1. 13 5
      src/entt/signal/dispatcher.hpp

+ 13 - 5
src/entt/signal/dispatcher.hpp

@@ -138,6 +138,17 @@ class basic_dispatcher {
         return static_cast<handler_type<Type> &>(*ptr);
     }
 
+    template<typename Type>
+    [[nodiscard]] const handler_type<Type> *assure(const id_type id) const {
+        static_assert(std::is_same_v<Type, std::decay_t<Type>>, "Non-decayed types not allowed");
+
+        if(auto it = pools.first().find(id); it != pools.first().cend()) {
+            return static_cast<const handler_type<Type> *>(it->second.get());
+        }
+
+        return nullptr;
+    }
+
 public:
     /*! @brief Allocator type. */
     using allocator_type = Allocator;
@@ -205,11 +216,8 @@ public:
      */
     template<typename Type>
     size_type size(const id_type id = type_hash<Type>::value()) const noexcept {
-        if(auto it = pools.first().find(id); it != pools.first().cend()) {
-            return it->second->size();
-        }
-
-        return 0u;
+        const auto *cpool = assure<std::decay_t<Type>>(id);
+        return cpool ? cpool->size() : 0u;
     }
 
     /**