[libsigcplusplus] signal::connect(): Return a sigc::connection.



commit e427deed4c62f73ff28a5f4a7adf4ac713bb3a3a
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Apr 21 16:16:31 2016 +0200

    signal::connect(): Return a sigc::connection.
    
    Instead of an iterator aliases to a signal<>::connection.

 examples/member_method.cc |    4 ++--
 sigc++/connection.h       |    5 ++++-
 sigc++/signal.h           |   12 +++++++++---
 tests/benchmark.cc        |    4 ++--
 tests/test_disconnect.cc  |    8 ++++----
 tests/test_size.cc        |    2 --
 6 files changed, 21 insertions(+), 14 deletions(-)
---
diff --git a/examples/member_method.cc b/examples/member_method.cc
index add00a1..ab82525 100644
--- a/examples/member_method.cc
+++ b/examples/member_method.cc
@@ -23,12 +23,12 @@ protected:
 
 Something::Something()
 {
-  auto iter = signal_print.connect(sigc::mem_fun(*this, &Something::on_print));
+  auto connection = signal_print.connect(sigc::mem_fun(*this, &Something::on_print));
 
   signal_print.emit(2);
 
   // This isn't necessary - it's just to demonstrate how to disconnect:
-  iter->disconnect();
+  connection.disconnect();
   signal_print.emit(3); // Prove that it is no longer connected.
 }
 
diff --git a/sigc++/connection.h b/sigc++/connection.h
index 58cfd9e..cfcead2 100644
--- a/sigc++/connection.h
+++ b/sigc++/connection.h
@@ -19,11 +19,14 @@
 #ifndef SIGC_CONNECTION_HPP
 #define SIGC_CONNECTION_HPP
 #include <sigc++config.h>
-#include <sigc++/signal.h>
+#include <sigc++/functors/slot_base.h>
 
 namespace sigc
 {
 
+template <typename T_slot>
+struct slot_iterator;
+
 /** Convinience class for safe disconnection.
  * Iterators must not be used beyond the lifetime of the list
  * they work on. A connection object can be created from a
diff --git a/sigc++/signal.h b/sigc++/signal.h
index d7280c7..70748f8 100644
--- a/sigc++/signal.h
+++ b/sigc++/signal.h
@@ -21,6 +21,7 @@
 #define SIGC_SIGNAL_H
 
 #include <list>
+#include <sigc++/connection.h>
 #include <sigc++/signal_base.h>
 #include <sigc++/type_traits.h>
 #include <sigc++/trackable.h>
@@ -469,7 +470,12 @@ class signal_with_accumulator : public signal_base
 {
 public:
   using slot_type = slot<T_return(T_arg...)>;
-  using connection = slot_iterator<slot_type>;
+
+private:
+  using iterator = slot_iterator<slot_type>;
+
+public:
+
 
   /** Add a slot to the list of slots.
    * Any functor or slot may be passed into connect().
@@ -494,7 +500,7 @@ public:
    */
   connection connect(const slot_type& slot_)
   {
-    return connection(signal_base::connect(slot_));
+    return connection(iterator(signal_base::connect(slot_)));
   }
 
   /** Add a slot to the list of slots.
@@ -504,7 +510,7 @@ public:
    */
   connection connect(slot_type&& slot_)
   {
-    return connection(signal_base::connect(std::move(slot_)));
+    return connection(iterator(signal_base::connect(std::move(slot_))));
   }
 
   /** Triggers the emission of the signal.
diff --git a/tests/benchmark.cc b/tests/benchmark.cc
index 7c42451..19aa9dd 100644
--- a/tests/benchmark.cc
+++ b/tests/benchmark.cc
@@ -82,7 +82,7 @@ void test_connect_disconnect()
 {
   foo foobar1;
   sigc::signal<int(int)> emitter;
-  sigc::signal<int(int)>::connection conn;
+  sigc::connection conn;
 
   std::cout << "elapsed time for " << COUNT << " connections/disconnections:" << std::endl;
   boost::timer::auto_cpu_timer timer;
@@ -90,7 +90,7 @@ void test_connect_disconnect()
   for (int i=0; i < COUNT; ++i)
     {
       conn = emitter.connect(mem_fun(foobar1, &foo::bar));
-      conn->disconnect();
+      conn.disconnect();
     }
 }
 
diff --git a/tests/test_disconnect.cc b/tests/test_disconnect.cc
index caadfaf..c50ffd7 100644
--- a/tests/test_disconnect.cc
+++ b/tests/test_disconnect.cc
@@ -81,8 +81,8 @@ main(int argc, char* argv[])
     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
 
   sigc::signal<int(int)> sig;
-  sigc::signal<int(int)>::connection confoo;
-  sigc::signal<int(int)>::connection conbar;
+  sigc::connection confoo;
+  sigc::connection conbar;
   sigc::connection cona; // connection objects are safe to use beyond the life time of a signal.
 
   {
@@ -107,12 +107,12 @@ main(int argc, char* argv[])
   util->check_result(
     result_stream, "sig is connected to foo, A::foo, bar (size=3): foo(3) bar(3) A::foo(3) ");
 
-  conbar->disconnect(); // manual disconnection
+  conbar.disconnect(); // manual disconnection
   result_stream << "sig is connected to foo, A::foo (size=" << sig.size() << "): ";
   sig(4);
   util->check_result(result_stream, "sig is connected to foo, A::foo (size=2): foo(4) A::foo(4) ");
 
-  confoo->disconnect(); // manual disconnection
+  confoo.disconnect(); // manual disconnection
   result_stream << "sig is connected to A::foo (size=" << sig.size() << "): ";
   sig(5);
   util->check_result(result_stream, "sig is connected to A::foo (size=1): A::foo(5) ");
diff --git a/tests/test_size.cc b/tests/test_size.cc
index 414d6ec..8c84ccb 100644
--- a/tests/test_size.cc
+++ b/tests/test_size.cc
@@ -35,8 +35,6 @@ main(int argc, char* argv[])
     std::cout << "  trackable:               " << sizeof(sigc::trackable) << std::endl;
     std::cout << "  slot<void()>:              " << sizeof(sigc::slot<void()>) << std::endl;
     std::cout << "  signal<void()>:            " << sizeof(sigc::signal<void()>) << std::endl;
-    std::cout << "  signal<void()>::connection:  " << sizeof(sigc::signal<void()>::connection)
-              << std::endl;
     std::cout << "  connection:              " << sizeof(sigc::connection) << std::endl;
 
     std::cout << std::endl << "sizes of internal classes:" << std::endl;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]