From 58ceb454dd6be180b964c3907aca0abda9e186cc Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 30 Nov 2018 10:34:49 +0100 Subject: [PATCH] review: delegate --- src/entt/signal/delegate.hpp | 54 +++++++++++++++++------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/entt/signal/delegate.hpp b/src/entt/signal/delegate.hpp index 322735659..b3ea19327 100644 --- a/src/entt/signal/delegate.hpp +++ b/src/entt/signal/delegate.hpp @@ -2,7 +2,6 @@ #define ENTT_SIGNAL_DELEGATE_HPP -#include #include #include #include "../config/config.h" @@ -81,21 +80,6 @@ class delegate; template class delegate final { using proto_fn_type = Ret(const void *, Args...); - using stub_type = std::pair; - - template - static Ret proto(const void *, Args... args) { - return std::invoke(Function, args...); - } - - template - static Ret proto(const void *instance, Args... args) { - if constexpr(Const) { - return std::invoke(Member, static_cast(instance), args...); - } else { - return std::invoke(Member, const_cast(static_cast(instance)), args...); - } - } public: /*! @brief Function type of the delegate. */ @@ -103,7 +87,7 @@ public: /*! @brief Default constructor. */ delegate() ENTT_NOEXCEPT - : stub{} + : fn{nullptr}, ref{nullptr} {} /** @@ -112,7 +96,7 @@ public: */ template delegate(connect_arg_t) ENTT_NOEXCEPT - : stub{} + : delegate{} { connect(); } @@ -125,7 +109,7 @@ public: */ template delegate(connect_arg_t, Class *instance) ENTT_NOEXCEPT - : stub{} + : delegate{} { connect(instance); } @@ -137,7 +121,11 @@ public: template void connect() ENTT_NOEXCEPT { static_assert(std::is_invocable_r_v); - stub = std::make_pair(nullptr, &proto); + ref = nullptr; + + fn = [](const void *, Args... args) -> Ret { + return std::invoke(Function, args...); + }; } /** @@ -154,7 +142,15 @@ public: template void connect(Class *instance) ENTT_NOEXCEPT { static_assert(std::is_invocable_r_v); - stub = std::make_pair(instance, &proto, std::remove_const_t, Member>); + ref = instance; + + fn = [](const void *instance, Args... args) -> Ret { + if constexpr(std::is_const_v) { + return std::invoke(Member, static_cast(instance), args...); + } else { + return std::invoke(Member, static_cast(const_cast(instance)), args...); + } + }; } /** @@ -163,7 +159,8 @@ public: * After a reset, a delegate can be safely invoked with no effect. */ void reset() ENTT_NOEXCEPT { - stub = std::make_pair(nullptr, nullptr); + ref = nullptr; + fn = nullptr; } /** @@ -171,7 +168,7 @@ public: * @return An opaque pointer to the instance bound to the delegate, if any. */ const void * instance() const ENTT_NOEXCEPT { - return stub.first; + return ref; } /** @@ -189,7 +186,7 @@ public: * @return The value returned by the underlying function. */ Ret operator()(Args... args) const { - return stub.second(stub.first, args...); + return fn(ref, args...); } /** @@ -197,8 +194,8 @@ public: * @return False if the delegate is empty, true otherwise. */ explicit operator bool() const ENTT_NOEXCEPT { - // no need to test also stub.first - return stub.second; + // no need to test also data + return fn; } /** @@ -210,11 +207,12 @@ public: * @return True if the two delegates are identical, false otherwise. */ bool operator==(const delegate &other) const ENTT_NOEXCEPT { - return stub.first == other.stub.first && stub.second == other.stub.second; + return ref == other.ref && fn == other.fn; } private: - stub_type stub{}; + proto_fn_type *fn; + const void *ref; };