Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r51398 - in sandbox/thread_safe_signals/trunk: boost/signals2/detail libs/signals2/test
From: fmhess_at_[hidden]
Date: 2009-02-22 14:48:39


Author: fmhess
Date: 2009-02-22 14:48:39 EST (Sun, 22 Feb 2009)
New Revision: 51398
URL: http://svn.boost.org/trac/boost/changeset/51398

Log:
Added automatic tracking of weak_ptr<signals2::trackable> and
weak_ptr<signals2::signal> objects via visit_each.

Text files modified:
   sandbox/thread_safe_signals/trunk/boost/signals2/detail/tracked_objects_visitor.hpp | 23 +++++++++++++++++++++++
   sandbox/thread_safe_signals/trunk/libs/signals2/test/trackable_test.cpp | 23 +++++++++++++++++++++--
   2 files changed, 44 insertions(+), 2 deletions(-)

Modified: sandbox/thread_safe_signals/trunk/boost/signals2/detail/tracked_objects_visitor.hpp
==============================================================================
--- sandbox/thread_safe_signals/trunk/boost/signals2/detail/tracked_objects_visitor.hpp (original)
+++ sandbox/thread_safe_signals/trunk/boost/signals2/detail/tracked_objects_visitor.hpp 2009-02-22 14:48:39 EST (Sun, 22 Feb 2009)
@@ -37,6 +37,11 @@
         {
             m_visit_reference_wrapper(t, mpl::bool_<is_reference_wrapper<T>::value>());
         }
+ template<typename T>
+ void operator()(const weak_ptr<T> & wp) const
+ {
+ m_visit_weak_ptr(wp, mpl::bool_<is_base_of<signals2::trackable, T>::value>());
+ }
       private:
         template<typename T>
         void m_visit_reference_wrapper(const reference_wrapper<T> &t, const mpl::bool_<true> &) const
@@ -83,6 +88,24 @@
             slot_->_tracked_objects.push_back(trackable->get_shared_ptr());
         }
         void add_if_trackable(const void *trackable) const {}
+ template<typename T>
+ void m_visit_weak_ptr(const weak_ptr<T> &wp, const mpl::bool_<true> &) const
+ {
+ slot_->_tracked_objects.push_back(wp);
+ }
+ template<typename T>
+ void m_visit_weak_ptr(const weak_ptr<T> &wp, const mpl::bool_<false> &) const
+ {
+ m_visit_weak_ptr_to_signal(wp, mpl::bool_<is_signal<T>::value>());
+ }
+ template<typename T>
+ void m_visit_weak_ptr_to_signal(const weak_ptr<T> &wp, const mpl::bool_<true> &) const
+ {
+ slot_->_tracked_objects.push_back(wp);
+ }
+ template<typename T>
+ void m_visit_weak_ptr_to_signal(const weak_ptr<T> &wp, const mpl::bool_<false> &) const
+ {}
 
         mutable slot_base * slot_;
       };

Modified: sandbox/thread_safe_signals/trunk/libs/signals2/test/trackable_test.cpp
==============================================================================
--- sandbox/thread_safe_signals/trunk/libs/signals2/test/trackable_test.cpp (original)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/test/trackable_test.cpp 2009-02-22 14:48:39 EST (Sun, 22 Feb 2009)
@@ -13,6 +13,8 @@
 #include <boost/signals2/signal.hpp>
 #include <boost/signals2/trackable.hpp>
 #include <boost/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/weak_ptr.hpp>
 
 struct short_lived : public boost::signals2::trackable {
   ~short_lived() {}
@@ -21,6 +23,8 @@
 struct swallow {
   typedef int result_type;
   template<typename T> int operator()(const T*, int i) { return i; }
+ template<typename T> int operator()(T &, int i) { return i; }
+ template<typename T> int operator()(boost::weak_ptr<T>, int i) { return i; }
 };
 
 template<typename T>
@@ -53,13 +57,29 @@
     s1.connect(boost::bind<int>(swallow(), &shorty, _1));
     BOOST_CHECK(s1(5) == 5);
   }
- // Test multiple arg slot constructor
   BOOST_CHECK(s1(5) == 0);
+ // Test auto-disconnection of trackable inside reference_wrapper
+ {
+ short_lived shorty;
+ s1.connect(boost::bind<int>(swallow(), boost::ref(shorty), _1));
+ BOOST_CHECK(s1(5) == 5);
+ }
+ BOOST_CHECK(s1(5) == 0);
+ // Test auto-disconnection of trackable in weak_ptr
+ {
+ boost::shared_ptr<short_lived> shorty(new short_lived);
+ s1.connect(boost::bind<int>(swallow(), boost::weak_ptr<short_lived>(shorty), _1));
+ BOOST_CHECK(s1(5) == 5);
+ }
+ BOOST_CHECK(s1(5) == 0);
+
+ // Test multiple arg slot constructor
   {
     short_lived shorty;
     s1.connect(sig_type::slot_type(swallow(), &shorty, _1));
     BOOST_CHECK(s1(5) == 5);
   }
+ BOOST_CHECK(s1(5) == 0);
 
   // Test auto-disconnection of slot before signal connection
   {
@@ -70,6 +90,5 @@
 
     BOOST_CHECK(s1(5) == 0);
   }
-
   return 0;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk