From 957697c3833a13c68b7027787816bf4e2ec6f736 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 18 Jun 2018 17:10:31 +0200 Subject: [PATCH] review: delegate (see #101 and #102) --- README.md | 12 +++++++++--- src/entt/signal/delegate.hpp | 15 +++++++++++---- test/entt/signal/delegate.cpp | 11 +++++++---- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 709154d31..42c1b4ce4 100644 --- a/README.md +++ b/README.md @@ -1890,7 +1890,7 @@ It has member functions to query its internal data structures, like `empty` or ```cpp // checks if there are processes still running -bool empty = scheduler.empty(); +const auto empty = scheduler.empty(); // gets the number of processes still running Scheduler::size_type size = scheduler.size(); @@ -2038,10 +2038,10 @@ _organize_ it: ```cpp // gets the number of resources managed by a cache -auto size = cache.size(); +const auto size = cache.size(); // checks if a cache contains at least a valid resource -auto empty = cache.empty(); +const auto empty = cache.empty(); // clears a cache and discards its content cache.clear(); @@ -2353,6 +2353,12 @@ delegate.connect(&instance); It hasn't a `disconnect` counterpart. Instead, there exists a `reset` member function to clear it.
+The `empty` member function can be used to know if a delegate is empty: + +```cpp +const auto empty = delegate.empty(); +``` + Finally, to invoke a delegate, the function call operator is the way to go as usual: diff --git a/src/entt/signal/delegate.hpp b/src/entt/signal/delegate.hpp index 5465d392f..53de73e82 100644 --- a/src/entt/signal/delegate.hpp +++ b/src/entt/signal/delegate.hpp @@ -37,8 +37,6 @@ class Delegate final { using proto_type = Ret(*)(void *, Args...); using stub_type = std::pair; - static Ret fallback(void *, Args...) ENTT_NOEXCEPT { return {}; } - template static Ret proto(void *, Args... args) { return (Function)(args...); @@ -52,9 +50,18 @@ class Delegate final { public: /*! @brief Default constructor. */ Delegate() ENTT_NOEXCEPT - : stub{std::make_pair(nullptr, &fallback)} + : stub{std::make_pair(nullptr, proto_type{})} {} + /** + * @brief Checks whether a delegate actually stores a listener. + * @return True if the delegate is empty, false otherwise. + */ + bool empty() const ENTT_NOEXCEPT { + // no need to test also stub.first + return !stub.second; + } + /** * @brief Binds a free function to a delegate. * @tparam Function A valid free function pointer. @@ -86,7 +93,7 @@ public: * After a reset, a delegate can be safely invoked with no effect. */ void reset() ENTT_NOEXCEPT { - stub = std::make_pair(nullptr, &fallback); + stub = std::make_pair(nullptr, proto_type{}); } /** diff --git a/test/entt/signal/delegate.cpp b/test/entt/signal/delegate.cpp index 672f6c6b3..ae9ff1b95 100644 --- a/test/entt/signal/delegate.cpp +++ b/test/entt/signal/delegate.cpp @@ -16,20 +16,23 @@ TEST(Delegate, Functionalities) { entt::Delegate mfdel; DelegateFunctor functor; - ASSERT_EQ(ffdel(42), int{}); - ASSERT_EQ(mfdel(42), int{}); + ASSERT_TRUE(ffdel.empty()); + ASSERT_TRUE(mfdel.empty()); ffdel.connect<&delegateFunction>(); mfdel.connect(&functor); + ASSERT_FALSE(ffdel.empty()); + ASSERT_FALSE(mfdel.empty()); + ASSERT_EQ(ffdel(3), 9); ASSERT_EQ(mfdel(3), 6); ffdel.reset(); mfdel.reset(); - ASSERT_EQ(ffdel(42), int{}); - ASSERT_EQ(mfdel(42), int{}); + ASSERT_TRUE(ffdel.empty()); + ASSERT_TRUE(mfdel.empty()); } TEST(Delegate, Comparison) {