]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/signals2/example/disconnect_and_block.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / signals2 / example / disconnect_and_block.cpp
CommitLineData
7c673cae
FG
1// Various simple examples involving disconnecting and blocking slots.
2// Copyright Douglas Gregor 2001-2004.
3// Copyright Frank Mori Hess 2009.
4//
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
10
11#include <iostream>
12#include <boost/signals2/signal.hpp>
13#include <boost/signals2/shared_connection_block.hpp>
14
15struct HelloWorld
16{
17 void operator()() const
18 {
19 std::cout << "Hello, World!" << std::endl;
20 }
21};
22
23void disconnect_example()
24{
25 boost::signals2::signal<void ()> sig;
26
27//[ disconnect_code_snippet
28 boost::signals2::connection c = sig.connect(HelloWorld());
29 std::cout << "c is connected\n";
30 sig(); // Prints "Hello, World!"
31
32 c.disconnect(); // Disconnect the HelloWorld object
33 std::cout << "c is disconnected\n";
34 sig(); // Does nothing: there are no connected slots
35//]
36}
37
38void block_example()
39{
40 boost::signals2::signal<void ()> sig;
41
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!"
46
47 {
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!"}
54//]
55}
56
57struct ShortLived
58{
59 void operator()() const
60 {
61 std::cout << "Life is short!" << std::endl;
62 }
63};
64
65void scoped_connection_example()
66{
67 boost::signals2::signal<void ()> sig;
68
69//[ scoped_connection_code_snippet
70 {
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
74
75 sig(); // ShortLived function object no longer connected to sig
76//]
77}
78
79//[ disconnect_by_slot_def_code_snippet
80void foo() { std::cout << "foo"; }
81void bar() { std::cout << "bar\n"; }
82//]
83
84void disconnect_by_slot_example()
85{
86 boost::signals2::signal<void()> sig;
87
88//[ disconnect_by_slot_usage_code_snippet
89 sig.connect(&foo);
90 sig.connect(&bar);
91 sig();
92
93 // disconnects foo, but not bar
94 sig.disconnect(&foo);
95 sig();
96//]
97}
98
99int main()
100{
101 std::cout << "Disconnect example:\n";
102 disconnect_example();
103
104 std::cout << "\nBlock example:\n";
105 block_example();
106
107 std::cout << "\nScoped connection example:\n";
108 scoped_connection_example();
109
110 std::cout << "\nDisconnect by slot example:\n";
111 disconnect_by_slot_example();
112
113 return 0;
114}