Browse Source

delegate: ::instance() -> ::data()

Michele Caini 4 years ago
parent
commit
0500f155e6
3 changed files with 16 additions and 16 deletions
  1. 13 13
      src/entt/signal/delegate.hpp
  2. 2 2
      src/entt/signal/sigh.hpp
  3. 1 1
      test/entt/signal/delegate.cpp

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

@@ -116,8 +116,8 @@ public:
 
     /*! @brief Default constructor. */
     delegate() ENTT_NOEXCEPT
-        : fn{nullptr},
-          data{nullptr} {}
+        : instance{nullptr},
+          fn{nullptr} {}
 
     /**
      * @brief Constructs a delegate and connects a free function or an unbound
@@ -157,7 +157,7 @@ public:
      */
     template<auto Candidate>
     void connect() ENTT_NOEXCEPT {
-        data = nullptr;
+        instance = nullptr;
 
         if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
             fn = [](const void *, Args... args) -> Ret {
@@ -187,7 +187,7 @@ public:
      */
     template<auto Candidate, typename Type>
     void connect(Type &value_or_instance) ENTT_NOEXCEPT {
-        data = &value_or_instance;
+        instance = &value_or_instance;
 
         if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
             fn = [](const void *payload, Args... args) -> Ret {
@@ -211,7 +211,7 @@ public:
      */
     template<auto Candidate, typename Type>
     void connect(Type *value_or_instance) ENTT_NOEXCEPT {
-        data = value_or_instance;
+        instance = value_or_instance;
 
         if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
             fn = [](const void *payload, Args... args) -> Ret {
@@ -237,8 +237,8 @@ public:
      * @param payload User defined arbitrary data.
      */
     void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
+        instance = payload;
         fn = function;
-        data = payload;
     }
 
     /**
@@ -247,16 +247,16 @@ public:
      * After a reset, a delegate cannot be invoked anymore.
      */
     void reset() ENTT_NOEXCEPT {
+        instance = nullptr;
         fn = nullptr;
-        data = nullptr;
     }
 
     /**
      * @brief Returns the instance or the payload linked to a delegate, if any.
      * @return An opaque pointer to the underlying data.
      */
-    [[nodiscard]] const void *instance() const ENTT_NOEXCEPT {
-        return data;
+    [[nodiscard]] const void *data() const ENTT_NOEXCEPT {
+        return instance;
     }
 
     /**
@@ -273,7 +273,7 @@ public:
      */
     Ret operator()(Args... args) const {
         ENTT_ASSERT(static_cast<bool>(*this), "Uninitialized delegate");
-        return fn(data, std::forward<Args>(args)...);
+        return fn(instance, std::forward<Args>(args)...);
     }
 
     /**
@@ -281,7 +281,7 @@ public:
      * @return False if the delegate is empty, true otherwise.
      */
     [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
-        // no need to test also data
+        // no need to also test instance
         return !(fn == nullptr);
     }
 
@@ -291,12 +291,12 @@ public:
      * @return False if the two contents differ, true otherwise.
      */
     [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
-        return fn == other.fn && data == other.data;
+        return fn == other.fn && instance == other.instance;
     }
 
 private:
+    const void *instance;
     function_type *fn;
-    const void *data;
 };
 
 /**

+ 2 - 2
src/entt/signal/sigh.hpp

@@ -453,7 +453,7 @@ public:
         if(value_or_instance) {
             const auto &calls = signal->calls;
             const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
-                return delegate.instance() == value_or_instance;
+                return delegate.data() == value_or_instance;
             });
 
             other.offset = std::distance(it, calls.cend());
@@ -572,7 +572,7 @@ public:
     void disconnect(Type *value_or_instance) {
         if(value_or_instance) {
             auto &calls = signal->calls;
-            auto predicate = [value_or_instance](const auto &delegate) { return delegate.instance() == value_or_instance; };
+            auto predicate = [value_or_instance](const auto &delegate) { return delegate.data() == value_or_instance; };
             calls.erase(std::remove_if(calls.begin(), calls.end(), std::move(predicate)), calls.end());
         }
     }

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

@@ -196,7 +196,7 @@ TEST(Delegate, Comparison) {
     lhs.connect<&delegate_functor::operator()>(other);
 
     ASSERT_EQ(lhs, (entt::delegate<int(int)>{entt::connect_arg<&delegate_functor::operator()>, other}));
-    ASSERT_NE(lhs.instance(), rhs.instance());
+    ASSERT_NE(lhs.data(), rhs.data());
     ASSERT_TRUE(lhs != rhs);
     ASSERT_FALSE(lhs == rhs);
     ASSERT_NE(lhs, rhs);