]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/example/disconnect_and_block.cpp
1 // Various simple examples involving disconnecting and blocking slots.
2 // Copyright Douglas Gregor 2001-2004.
3 // 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 // For more information, see http://www.boost.org
12 #include <boost/signals2/signal.hpp>
13 #include <boost/signals2/shared_connection_block.hpp>
17 void operator()() const
19 std::cout
<< "Hello, World!" << std::endl
;
23 void disconnect_example()
25 boost::signals2::signal
<void ()> sig
;
27 //[ disconnect_code_snippet
28 boost::signals2::connection c
= sig
.connect(HelloWorld());
29 std::cout
<< "c is connected\n";
30 sig(); // Prints "Hello, World!"
32 c
.disconnect(); // Disconnect the HelloWorld object
33 std::cout
<< "c is disconnected\n";
34 sig(); // Does nothing: there are no connected slots
40 boost::signals2::signal
<void ()> sig
;
42 //[ block_code_snippet
43 boost::signals2::connection c
= sig
.connect(HelloWorld());
44 std::cout
<< "c is not blocked.\n";
45 sig(); // Prints "Hello, World!"
48 boost::signals2::shared_connection_block
block(c
); // block the slot
49 std::cout
<< "c is blocked.\n";
50 sig(); // No output: the slot is blocked
51 } // shared_connection_block going out of scope unblocks the slot
52 std::cout
<< "c is not blocked.\n";
53 sig(); // Prints "Hello, World!"}
59 void operator()() const
61 std::cout
<< "Life is short!" << std::endl
;
65 void scoped_connection_example()
67 boost::signals2::signal
<void ()> sig
;
69 //[ scoped_connection_code_snippet
71 boost::signals2::scoped_connection
c(sig
.connect(ShortLived()));
72 sig(); // will call ShortLived function object
73 } // scoped_connection goes out of scope and disconnects
75 sig(); // ShortLived function object no longer connected to sig
79 //[ disconnect_by_slot_def_code_snippet
80 void foo() { std::cout
<< "foo"; }
81 void bar() { std::cout
<< "bar\n"; }
84 void disconnect_by_slot_example()
86 boost::signals2::signal
<void()> sig
;
88 //[ disconnect_by_slot_usage_code_snippet
93 // disconnects foo, but not bar
101 std::cout
<< "Disconnect example:\n";
102 disconnect_example();
104 std::cout
<< "\nBlock example:\n";
107 std::cout
<< "\nScoped connection example:\n";
108 scoped_connection_example();
110 std::cout
<< "\nDisconnect by slot example:\n";
111 disconnect_by_slot_example();