diff --git a/src/entt/signal/emitter.hpp b/src/entt/signal/emitter.hpp index 99d582273..3e832044d 100644 --- a/src/entt/signal/emitter.hpp +++ b/src/entt/signal/emitter.hpp @@ -2,7 +2,6 @@ #define ENTT_SIGNAL_EMITTER_HPP #include -#include #include #include #include "../container/dense_map.hpp" @@ -33,26 +32,7 @@ namespace entt { */ template class emitter { - template - using function_type = std::function; - - template - [[nodiscard]] function_type &assure() { - static_assert(std::is_same_v>, "Non-decayed types not allowed"); - auto &&ptr = handlers[type_hash::value()]; - - if(!ptr) { - ptr = std::make_shared>(); - } - - return *static_cast *>(ptr.get()); - } - - template - [[nodiscard]] const function_type *assure() const { - const auto it = handlers.find(type_hash::value()); - return (it == handlers.cend()) ? nullptr : static_cast *>(it->second.get()); - } + using function_type = std::function; public: /*! @brief Default constructor. */ @@ -65,13 +45,13 @@ public: } /*! @brief Default move constructor. */ - emitter(emitter &&) = default; + emitter(emitter &&) noexcept = default; /** * @brief Default move assignment operator. * @return This emitter. */ - emitter &operator=(emitter &&) = default; + emitter &operator=(emitter &&) noexcept = default; /** * @brief Publishes a given event. @@ -80,8 +60,8 @@ public: */ template void publish(Type &&value) { - if(auto &handler = assure>>(); handler) { - handler(value, *static_cast(this)); + if(const auto id = type_id().hash(); handlers.contains(id)) { + handlers[id](&value); } } @@ -92,7 +72,9 @@ public: */ template void on(std::function func) { - assure() = std::move(func); + handlers.insert_or_assign(type_id().hash(), [func = std::move(func), this](void *value) { + func(*static_cast(value), static_cast(*this)); + }); } /** @@ -128,7 +110,7 @@ public: } private: - dense_map, identity> handlers{}; + dense_map handlers{}; }; } // namespace entt