From b0651fcaed41dc6e2e5b94fbadc4dd2f2358d87a Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sun, 21 Oct 2018 14:59:19 +0200 Subject: [PATCH] delegate: empty -> operator bool --- docs/signal.md | 11 +++++++---- src/entt/signal/delegate.hpp | 18 +++++++++--------- test/entt/signal/delegate.cpp | 18 +++++++++--------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/docs/signal.md b/docs/signal.md index 5c9355e25..6278c642e 100644 --- a/docs/signal.md +++ b/docs/signal.md @@ -167,8 +167,8 @@ the delegate will _contain_, that is the signature of the free function or the member function one wants to assign to it. Attempting to use an empty delegate by invoking its function call operator -results in undefined behavior, most likely a crash actually. Before to use a -delegate, it must be initialized.
+results in undefined behavior or most likely a crash. Before to use a delegate, +it must be initialized.
There exist two functions to do that, both named `connect`: ```cpp @@ -188,10 +188,13 @@ delegate.connect<&my_struct::f>(&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: +To know if a delegate is empty, it can be used explicitly in every conditional +statement: ```cpp -const auto empty = delegate.empty(); +if(delegate) { + // ... +} ``` Finally, to invoke a delegate, the function call operator is the way to go as diff --git a/src/entt/signal/delegate.hpp b/src/entt/signal/delegate.hpp index 61bbc9ac2..e5578ce89 100644 --- a/src/entt/signal/delegate.hpp +++ b/src/entt/signal/delegate.hpp @@ -89,15 +89,6 @@ public: connect(instance); } - /** - * @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. @@ -160,6 +151,15 @@ public: return stub.second(stub.first, args...); } + /** + * @brief Checks whether a delegate actually stores a listener. + * @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; + } + /** * @brief Checks if the contents of the two delegates are different. * diff --git a/test/entt/signal/delegate.cpp b/test/entt/signal/delegate.cpp index 37db85296..f6dc3d62c 100644 --- a/test/entt/signal/delegate.cpp +++ b/test/entt/signal/delegate.cpp @@ -24,23 +24,23 @@ TEST(Delegate, Functionalities) { entt::delegate mfdel; delegate_functor functor; - ASSERT_TRUE(ffdel.empty()); - ASSERT_TRUE(mfdel.empty()); + ASSERT_FALSE(ffdel); + ASSERT_FALSE(mfdel); ASSERT_EQ(ffdel, mfdel); ffdel.connect<&delegate_function>(); mfdel.connect<&delegate_functor::operator()>(&functor); - ASSERT_FALSE(ffdel.empty()); - ASSERT_FALSE(mfdel.empty()); + ASSERT_TRUE(ffdel); + ASSERT_TRUE(mfdel); ASSERT_EQ(ffdel(3), 9); ASSERT_EQ(mfdel(3), 6); ffdel.reset(); - ASSERT_TRUE(ffdel.empty()); - ASSERT_FALSE(mfdel.empty()); + ASSERT_FALSE(ffdel); + ASSERT_TRUE(mfdel); ASSERT_EQ(ffdel, entt::delegate{}); ASSERT_NE(ffdel, mfdel); @@ -83,7 +83,7 @@ TEST(Delegate, Constructors) { entt::delegate func{entt::connect_arg<&delegate_function>}; entt::delegate member{entt::connect_arg<&delegate_functor::operator()>, &functor}; - ASSERT_TRUE(empty.empty()); - ASSERT_FALSE(func.empty()); - ASSERT_FALSE(member.empty()); + ASSERT_FALSE(empty); + ASSERT_TRUE(func); + ASSERT_TRUE(member); }