| 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 | int main() |
|---|
| 27 | { |
|---|
| 28 | boost::signal2<void, int, int> sig; |
|---|
| 29 | |
|---|
| 30 | sig.connect(print_sum()); |
|---|
| 31 | sig.connect(print_product()); |
|---|
| 32 | |
|---|
| 33 | sig(3, 5); |
|---|
| 34 | |
|---|
| 35 | boost::signals::connection print_diff_con = sig.connect(print_difference()); |
|---|
| 36 | |
|---|
| 37 | // sig is still connected to print_diff_con |
|---|
| 38 | assert(print_diff_con.connected()); |
|---|
| 39 | |
|---|
| 40 | sig(5, 3); // prints 8, 15, and 2 |
|---|
| 41 | |
|---|
| 42 | print_diff_con.disconnect(); // disconnect the print_difference slot |
|---|
| 43 | |
|---|
| 44 | sig(5, 3); // now prints 8 and 15, but not the difference |
|---|
| 45 | |
|---|
| 46 | assert(!print_diff_con.connected()); // not connected any more |
|---|
| 47 | return 0; |
|---|
| 48 | } |
|---|