]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/test/track_test.cpp
1 // Boost.Signals library
3 // Copyright Douglas Gregor 2001-2006
4 // Copyright Frank Mori Hess 2007
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
13 #include <boost/optional.hpp>
14 #include <boost/ref.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/test/minimal.hpp>
17 #include <boost/signals2.hpp>
18 #include <boost/bind.hpp>
21 typedef int result_type
;
22 template<typename T
> result_type
operator()(const T
*, int i
) { return i
; }
26 struct max_or_default
{
27 typedef T result_type
;
29 template<typename InputIterator
>
30 T
operator()(InputIterator first
, InputIterator last
) const
32 boost::optional
<T
> max
;
33 for(; first
!= last
; ++first
)
39 }else if(value
> *max
)
49 static int myfunc(int i
, double z
)
54 int test_main(int, char*[])
56 typedef boost::signals2::signal
<int (int), max_or_default
<int> > sig_type
;
58 boost::signals2::connection connection
;
60 // Test auto-disconnection
61 BOOST_CHECK(s1(5) == 0);
63 boost::shared_ptr
<int> shorty(new int());
64 s1
.connect(sig_type::slot_type(swallow(), shorty
.get(), _1
).track(shorty
));
65 BOOST_CHECK(s1(5) == 5);
67 BOOST_CHECK(s1(5) == 0);
69 // Test auto-disconnection of slot before signal connection
71 boost::shared_ptr
<int> shorty(new int(1));
72 // doesn't work on gcc 3.3.5, it says: error: type specifier omitted for parameter `shorty'
73 // does work on gcc 4.1.2
74 // sig_type::slot_type slot(swallow(), shorty.get(), _1);
76 sig_type::slot_type
slot(myswallow
, shorty
.get(), _1
);
81 BOOST_CHECK(s1(5) == 0);
84 // Test binding of a slot to another slot
86 boost::shared_ptr
<int> shorty(new int(2));
87 boost::signals2::slot
<int (double)> other_slot(&myfunc
, boost::cref(*shorty
.get()), _1
);
88 other_slot
.track(shorty
);
89 connection
= s1
.connect(sig_type::slot_type(other_slot
, 0.5).track(other_slot
));
90 BOOST_CHECK(s1(3) == 2);
92 BOOST_CHECK(connection
.connected() == false);
93 BOOST_CHECK(s1(3) == 0);
95 // Test binding of a signal as a slot
99 s2
.connect(sig_type::slot_type(&myfunc
, _1
, 0.7));
100 BOOST_CHECK(s1(4) == 4);
102 BOOST_CHECK(s1(4) == 0);
104 // Test tracking of null but not empty shared_ptr
105 BOOST_CHECK(s1(2) == 0);
107 boost::shared_ptr
<int> shorty((int*)(0));
108 s1
.connect(sig_type::slot_type(swallow(), shorty
.get(), _1
).track(shorty
));
109 BOOST_CHECK(s1(2) == 2);
111 BOOST_CHECK(s1(2) == 0);
113 #ifndef BOOST_NO_CXX11_SMART_PTR
114 // Test tracking through std::shared_ptr/weak_ptr
115 BOOST_CHECK(s1(5) == 0);
117 std::shared_ptr
<int> shorty(new int());
118 s1
.connect(sig_type::slot_type(swallow(), shorty
.get(), _1
).track_foreign(shorty
));
119 BOOST_CHECK(s1(5) == 5);
121 BOOST_CHECK(s1(5) == 0);
123 std::shared_ptr
<int> shorty(new int());
133 std::weak_ptr
<int>(shorty
)
136 BOOST_CHECK(s1(5) == 5);
138 BOOST_CHECK(s1(5) == 0);
139 // make sure tracking foreign shared_ptr<const void> works
141 std::shared_ptr
<const void> shorty(new int());
142 s1
.connect(sig_type::slot_type(swallow(), shorty
.get(), _1
).track_foreign(shorty
));
143 BOOST_CHECK(s1(5) == 5);
146 std::shared_ptr
<int> shorty(new int());
156 std::weak_ptr
<const void>(shorty
)
159 BOOST_CHECK(s1(5) == 5);
161 BOOST_CHECK(s1(5) == 0);
162 BOOST_CHECK(s1(5) == 0);