]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/test/trackable_test.cpp
1 // Boost.Signals2 library
3 // Copyright Douglas Gregor 2001-2006.
4 // Copyright Frank Mori Hess 2009.
5 // Use, modification and
6 // distribution is subject to the Boost Software License, Version
7 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see http://www.boost.org
12 #include <boost/test/minimal.hpp>
13 #include <boost/signals2/signal.hpp>
14 #include <boost/signals2/trackable.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/ref.hpp>
17 #include <boost/weak_ptr.hpp>
19 struct short_lived
: public boost::signals2::trackable
{
24 typedef int result_type
;
25 template<typename T
> int operator()(const T
*, int i
) { return i
; }
26 template<typename T
> int operator()(T
&, int i
) { return i
; }
27 template<typename T
> int operator()(boost::weak_ptr
<T
>, int i
) { return i
; }
31 struct max_or_default
{
32 typedef T result_type
;
34 template<typename InputIterator
>
35 T
operator()(InputIterator first
, InputIterator last
) const
41 for (; first
!= last
; ++first
)
42 max
= (*first
> max
)? *first
: max
;
48 struct self_deleting
: public boost::signals2::trackable
{
49 void delete_myself(boost::signals2::connection connection
)
51 BOOST_CHECK(connection
.connected());
53 BOOST_CHECK(connection
.connected() == false);
57 // test that slot assocated with signals2::trackable
58 // gets disconnected immediately upon deletion of the
59 // signals2::trackable, even when a signal invocation
61 void test_immediate_disconnect_on_delete()
63 boost::signals2::signal
<void () > sig
;
64 self_deleting
*obj
= new self_deleting();
65 sig
.connect_extended(boost::bind(&self_deleting::delete_myself
, obj
, _1
));
69 int test_main(int, char*[])
71 typedef boost::signals2::signal
<int (int), max_or_default
<int> > sig_type
;
74 // Test auto-disconnection
75 BOOST_CHECK(s1(5) == 0);
78 s1
.connect(boost::bind
<int>(swallow(), &shorty
, _1
));
79 BOOST_CHECK(s1(5) == 5);
81 BOOST_CHECK(s1(5) == 0);
82 // Test auto-disconnection of trackable inside reference_wrapper
85 s1
.connect(boost::bind
<int>(swallow(), boost::ref(shorty
), _1
));
86 BOOST_CHECK(s1(5) == 5);
88 BOOST_CHECK(s1(5) == 0);
90 // Test multiple arg slot constructor
93 s1
.connect(sig_type::slot_type(swallow(), &shorty
, _1
));
94 BOOST_CHECK(s1(5) == 5);
96 BOOST_CHECK(s1(5) == 0);
98 // Test auto-disconnection of slot before signal connection
100 short_lived
* shorty
= new short_lived();
102 sig_type::slot_type
slot(boost::bind
<int>(swallow(), shorty
, _1
));
105 BOOST_CHECK(s1(5) == 0);
108 test_immediate_disconnect_on_delete();