]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/test/trackable_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / signals2 / test / trackable_test.cpp
1 // Boost.Signals2 library
2
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)
9
10 // For more information, see http://www.boost.org
11
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>
18
19 struct short_lived : public boost::signals2::trackable {
20 ~short_lived() {}
21 };
22
23 struct swallow {
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; }
28 };
29
30 template<typename T>
31 struct max_or_default {
32 typedef T result_type;
33
34 template<typename InputIterator>
35 T operator()(InputIterator first, InputIterator last) const
36 {
37 if (first == last)
38 return T();
39
40 T max = *first++;
41 for (; first != last; ++first)
42 max = (*first > max)? *first : max;
43
44 return max;
45 }
46 };
47
48 struct self_deleting : public boost::signals2::trackable {
49 void delete_myself(boost::signals2::connection connection)
50 {
51 BOOST_CHECK(connection.connected());
52 delete this;
53 BOOST_CHECK(connection.connected() == false);
54 }
55 };
56
57 // test that slot assocated with signals2::trackable
58 // gets disconnected immediately upon deletion of the
59 // signals2::trackable, even when a signal invocation
60 // is in progress.
61 void test_immediate_disconnect_on_delete()
62 {
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));
66 sig();
67 }
68
69 int test_main(int, char*[])
70 {
71 typedef boost::signals2::signal<int (int), max_or_default<int> > sig_type;
72 sig_type s1;
73
74 // Test auto-disconnection
75 BOOST_CHECK(s1(5) == 0);
76 {
77 short_lived shorty;
78 s1.connect(boost::bind<int>(swallow(), &shorty, _1));
79 BOOST_CHECK(s1(5) == 5);
80 }
81 BOOST_CHECK(s1(5) == 0);
82 // Test auto-disconnection of trackable inside reference_wrapper
83 {
84 short_lived shorty;
85 s1.connect(boost::bind<int>(swallow(), boost::ref(shorty), _1));
86 BOOST_CHECK(s1(5) == 5);
87 }
88 BOOST_CHECK(s1(5) == 0);
89
90 // Test multiple arg slot constructor
91 {
92 short_lived shorty;
93 s1.connect(sig_type::slot_type(swallow(), &shorty, _1));
94 BOOST_CHECK(s1(5) == 5);
95 }
96 BOOST_CHECK(s1(5) == 0);
97
98 // Test auto-disconnection of slot before signal connection
99 {
100 short_lived* shorty = new short_lived();
101
102 sig_type::slot_type slot(boost::bind<int>(swallow(), shorty, _1));
103 delete shorty;
104
105 BOOST_CHECK(s1(5) == 0);
106 }
107
108 test_immediate_disconnect_on_delete();
109
110 return 0;
111 }