1 | // Boost.Signals library |
---|
2 | |
---|
3 | // Copyright Douglas Gregor 2001-2003. Use, modification and |
---|
4 | // distribution is subject to the Boost Software License, Version |
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | |
---|
8 | // For more information, see http://www.boost.org |
---|
9 | |
---|
10 | #include <iostream> |
---|
11 | #include <boost/signals/signal2.hpp> |
---|
12 | #include <cassert> |
---|
13 | |
---|
14 | struct print_sum { |
---|
15 | void operator()(int x, int y) const { std::cout << x+y << std::endl; } |
---|
16 | }; |
---|
17 | |
---|
18 | struct print_product { |
---|
19 | void operator()(int x, int y) const { std::cout << x*y << std::endl; } |
---|
20 | }; |
---|
21 | |
---|
22 | struct print_difference { |
---|
23 | void operator()(int x, int y) const { std::cout << x-y << std::endl; } |
---|
24 | }; |
---|
25 | |
---|
26 | struct print_quotient { |
---|
27 | void operator()(int x, int y) const { std::cout << x/-y << std::endl; } |
---|
28 | }; |
---|
29 | |
---|
30 | |
---|
31 | int main() |
---|
32 | { |
---|
33 | boost::signal2<void, int, int, boost::last_value<void>, std::string> sig; |
---|
34 | |
---|
35 | sig.connect(print_sum()); |
---|
36 | sig.connect(print_product()); |
---|
37 | |
---|
38 | sig(3, 5); |
---|
39 | |
---|
40 | boost::signals::connection print_diff_con = sig.connect(print_difference()); |
---|
41 | |
---|
42 | // sig is still connected to print_diff_con |
---|
43 | assert(print_diff_con.connected()); |
---|
44 | |
---|
45 | sig(5, 3); // prints 8, 15, and 2 |
---|
46 | |
---|
47 | print_diff_con.disconnect(); // disconnect the print_difference slot |
---|
48 | |
---|
49 | sig(5, 3); // now prints 8 and 15, but not the difference |
---|
50 | |
---|
51 | assert(!print_diff_con.connected()); // not connected any more |
---|
52 | |
---|
53 | { |
---|
54 | boost::signals::scoped_connection c = sig.connect(print_quotient()); |
---|
55 | sig(5, 3); // prints 8, 15, and 1 |
---|
56 | } // c falls out of scope, so sig and print_quotient are disconnected |
---|
57 | |
---|
58 | sig(5, 3); // prints 8 and 15 |
---|
59 | |
---|
60 | sig.connect("quotient", print_quotient()); |
---|
61 | sig(5, 3); // prints 8, 15, and 1 |
---|
62 | sig.disconnect("quotient"); |
---|
63 | sig(5, 3); // prints 8 and 15 |
---|
64 | |
---|
65 | return 0; |
---|
66 | } |
---|