diff --git a/src/entt/signal/dispatcher.hpp b/src/entt/signal/dispatcher.hpp index 5c2508ca3..16ddaccbe 100644 --- a/src/entt/signal/dispatcher.hpp +++ b/src/entt/signal/dispatcher.hpp @@ -10,7 +10,6 @@ #include #include "../config/config.h" #include "../core/family.hpp" -#include "../core/type_traits.hpp" #include "sigh.hpp" @@ -33,17 +32,14 @@ namespace entt { class dispatcher { using event_family = family; - template - using instance_type = typename sigh::template instance_type; - - struct base_wrapper { - virtual ~base_wrapper() = default; + struct basic_pool { + virtual ~basic_pool() = default; virtual void publish() = 0; virtual void clear() ENTT_NOEXCEPT = 0; }; template - struct signal_wrapper: base_wrapper { + struct pool_handler: basic_pool { using signal_type = sigh; using sink_type = typename signal_type::sink_type; @@ -80,54 +76,25 @@ class dispatcher { std::vector events; }; - struct wrapper_data { - std::unique_ptr wrapper; - ENTT_ID_TYPE runtime_type; - }; - template - static auto type() ENTT_NOEXCEPT { - if constexpr(is_named_type_v) { - return named_type_traits_v; - } else { - return event_family::type>; - } - } + pool_handler & assure() { + const auto etype = event_family::type>; - template - signal_wrapper & assure() { - const auto wtype = type(); - wrapper_data *wdata = nullptr; - - if constexpr(is_named_type_v) { - const auto it = std::find_if(wrappers.begin(), wrappers.end(), [wtype](const auto &candidate) { - return candidate.wrapper && candidate.runtime_type == wtype; - }); - - wdata = (it == wrappers.cend() ? &wrappers.emplace_back() : &(*it)); - } else { - if(!(wtype < wrappers.size())) { - wrappers.resize(wtype+1); - } else if(wrappers[wtype].wrapper && wrappers[wtype].runtime_type != wtype) { - wrappers.emplace_back(); - std::swap(wrappers[wtype], wrappers.back()); - } - - wdata = &wrappers[wtype]; + if(!(etype < pools.size())) { + pools.resize(etype+1); } - if(!wdata->wrapper) { - wdata->wrapper = std::make_unique>(); - wdata->runtime_type = wtype; + if(!pools[etype]) { + pools[etype] = std::make_unique>(); } - return static_cast &>(*wdata->wrapper); + return static_cast &>(*pools[etype]); } public: /*! @brief Type of sink for the given event. */ template - using sink_type = typename signal_wrapper::sink_type; + using sink_type = typename pool_handler::sink_type; /** * @brief Returns a sink object for the given event. @@ -220,9 +187,9 @@ public: template void discard() ENTT_NOEXCEPT { if constexpr(sizeof...(Event) == 0) { - std::for_each(wrappers.begin(), wrappers.end(), [](auto &&wdata) { - if(wdata.wrapper) { - wdata.wrapper->clear(); + std::for_each(pools.begin(), pools.end(), [](auto &&cpool) { + if(cpool) { + cpool->clear(); } }); } else { @@ -252,15 +219,15 @@ public: * to reduce at a minimum the time spent in the bodies of the listeners. */ void update() const { - for(auto pos = wrappers.size(); pos; --pos) { - if(auto &wdata = wrappers[pos-1]; wdata.wrapper) { - wdata.wrapper->publish(); + for(auto pos = pools.size(); pos; --pos) { + if(auto &cpool = pools[pos-1]; cpool) { + cpool->publish(); } } } private: - std::vector wrappers; + std::vector> pools; };