diff --git a/src/entt/signal/dispatcher.hpp b/src/entt/signal/dispatcher.hpp index 94c5894dc..c8a5b4427 100644 --- a/src/entt/signal/dispatcher.hpp +++ b/src/entt/signal/dispatcher.hpp @@ -6,11 +6,11 @@ #include #include #include +#include #include #include #include "../config/config.h" -#include "../core/attribute.h" -#include "../core/family.hpp" +#include "../core/type_info.hpp" #include "sigh.hpp" @@ -31,15 +31,11 @@ namespace entt { * crashes. */ class dispatcher { - struct ENTT_API dispatcher_event_family; - - template - using event_family = family; - struct basic_pool { virtual ~basic_pool() = default; virtual void publish() = 0; virtual void clear() ENTT_NOEXCEPT = 0; + virtual ENTT_ID_TYPE id() const ENTT_NOEXCEPT = 0; }; template @@ -75,6 +71,10 @@ class dispatcher { events.emplace_back(std::forward(args)...); } + ENTT_ID_TYPE id() const ENTT_NOEXCEPT override { + return type_id_v; + } + private: signal_type signal{}; std::vector events; @@ -82,17 +82,19 @@ class dispatcher { template pool_handler & assure() { - const auto etype = event_family::type(); + static const auto index = std::distance(pools.cbegin(), std::find_if(pools.cbegin(), pools.cend(), [](auto &&curr) { + return curr && curr->id() == type_id_v; + })); - if(!(etype < pools.size())) { - pools.resize(etype+1); + if(!(index < pools.size())) { + pools.resize(index+1); } - if(!pools[etype]) { - pools[etype] = std::make_unique>(); + if(!pools[index]) { + pools[index] = std::make_unique>(); } - return static_cast &>(*pools[etype]); + return static_cast &>(*pools[index]); } public: diff --git a/test/lib/dispatcher/lib.cpp b/test/lib/dispatcher/lib.cpp index 738a5e5bf..82c00e3c3 100644 --- a/test/lib/dispatcher/lib.cpp +++ b/test/lib/dispatcher/lib.cpp @@ -2,8 +2,6 @@ #include #include "types.h" -template struct entt::family; - ENTT_API void trigger(int value, entt::dispatcher &dispatcher) { dispatcher.trigger(value); } diff --git a/test/lib/dispatcher/types.h b/test/lib/dispatcher/types.h index 4dad5d551..ad2e78b9a 100644 --- a/test/lib/dispatcher/types.h +++ b/test/lib/dispatcher/types.h @@ -1,13 +1,11 @@ #ifndef ENTT_LIB_DISPATCHER_TYPES_H #define ENTT_LIB_DISPATCHER_TYPES_H -#include - -struct ENTT_API message { +struct message { int payload; }; -struct ENTT_API event { +struct event { int payload; }; diff --git a/test/plugin/dispatcher/main.cpp b/test/plugin/dispatcher/main.cpp index e69de29bb..f698af371 100644 --- a/test/plugin/dispatcher/main.cpp +++ b/test/plugin/dispatcher/main.cpp @@ -0,0 +1,33 @@ +#define CR_HOST + +#include +#include +#include +#include +#include "types.h" + +struct listener { + void on(event ev) { value = ev.payload; } + void on(message msg) { value = msg.payload; } + int value{}; +}; + +TEST(Lib, Dispatcher) { + entt::dispatcher dispatcher; + listener listener; + + dispatcher.sink().connect(&listener::on)>(listener); + dispatcher.sink().connect(&listener::on)>(listener); + dispatcher.trigger(3); + + ASSERT_EQ(listener.value, 3); + + cr_plugin ctx; + ctx.userdata = &dispatcher; + cr_plugin_load(ctx, PLUGIN); + cr_plugin_update(ctx); + + ASSERT_EQ(listener.value, 42); + + cr_plugin_close(ctx); +} diff --git a/test/plugin/dispatcher/plugin.cpp b/test/plugin/dispatcher/plugin.cpp index e69de29bb..6ab54f430 100644 --- a/test/plugin/dispatcher/plugin.cpp +++ b/test/plugin/dispatcher/plugin.cpp @@ -0,0 +1,18 @@ +#include +#include +#include "types.h" + +CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) { + switch (operation) { + case CR_STEP: + static_cast(ctx->userdata)->trigger(42); + break; + case CR_LOAD: + case CR_UNLOAD: + case CR_CLOSE: + // nothing to do here, this is only a test. + break; + } + + return 0; +} diff --git a/test/plugin/dispatcher/types.h b/test/plugin/dispatcher/types.h index e69de29bb..8bd52cd85 100644 --- a/test/plugin/dispatcher/types.h +++ b/test/plugin/dispatcher/types.h @@ -0,0 +1,12 @@ +#ifndef ENTT_PLUGIN_DISPATCHER_TYPES_H +#define ENTT_PLUGIN_DISPATCHER_TYPES_H + +struct message { + int payload; +}; + +struct event { + int payload; +}; + +#endif // ENTT_PLUGIN_DISPATCHER_TYPES_H