Просмотр исходного кода

delegate: empty -> operator bool

Michele Caini 7 лет назад
Родитель
Сommit
b0651fcaed
3 измененных файлов с 25 добавлено и 22 удалено
  1. 7 4
      docs/signal.md
  2. 9 9
      src/entt/signal/delegate.hpp
  3. 9 9
      test/entt/signal/delegate.cpp

+ 7 - 4
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.<br/>
+results in undefined behavior or most likely a crash. Before to use a delegate,
+it must be initialized.<br/>
 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.<br/>
-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

+ 9 - 9
src/entt/signal/delegate.hpp

@@ -89,15 +89,6 @@ public:
         connect<Member>(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.
      *

+ 9 - 9
test/entt/signal/delegate.cpp

@@ -24,23 +24,23 @@ TEST(Delegate, Functionalities) {
     entt::delegate<int(int)> 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<int(int)>{});
     ASSERT_NE(ffdel, mfdel);
@@ -83,7 +83,7 @@ TEST(Delegate, Constructors) {
     entt::delegate<int(int)> func{entt::connect_arg<&delegate_function>};
     entt::delegate<int(int)> 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);
 }