Skip to content

Commit e61a2fa

Browse files
committed
scoped_connection: new wrapper to auto-disconnect…
…a contained sigc::connection, when the scoped_connection is destructed. #87
1 parent 5c9d60c commit e61a2fa

File tree

6 files changed

+474
-0
lines changed

6 files changed

+474
-0
lines changed

sigc++/connection.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717
*
1818
*/
19+
1920
#ifndef SIGC_CONNECTION_HPP
2021
#define SIGC_CONNECTION_HPP
22+
2123
#include <sigc++config.h>
2224
#include <sigc++/functors/slot_base.h>
2325
#include <sigc++/weak_raw_ptr.h>
@@ -30,13 +32,20 @@ namespace sigc
3032
* This may be used to disconnect the referred slot at any time (disconnect()).
3133
* @ref sigc::signal_with_accumulator::connect() "sigc::signal::connect()"
3234
* returns a %sigc::connection.
35+
*
3336
* @code
3437
* sigc::connection conn = sig.connect(sigc::mem_fun(a, &A::foo));
3538
* @endcode
39+
*
3640
* If the slot has already been destroyed, disconnect() does nothing. empty() or
3741
* operator bool() can be used to test whether the connection is
3842
* still active. The connection can be blocked (block(), unblock()).
3943
*
44+
* sigc::connection doesnʼt disconnect the slot automatically upon destruction.
45+
* You do not need to keep the sigc::connection object to retain the connection
46+
* of the slot to the signal. See also @ref sigc::scoped_connection, which does
47+
* diconnect automatically when the connection object is destroyed or replaced.
48+
*
4049
* @ingroup signal
4150
*/
4251
struct SIGC_API connection

sigc++/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
source_cc_files = [
1010
'connection.cc',
11+
'scoped_connection.cc',
1112
'signal_base.cc',
1213
'trackable.cc',
1314
'functors' / 'slot_base.cc',
@@ -21,6 +22,7 @@ sigc_h_files = [
2122
'member_method_trait.h',
2223
'reference_wrapper.h',
2324
'retype_return.h',
25+
'scoped_connection.h',
2426
'signal.h',
2527
'signal_base.h',
2628
'slot.h',

sigc++/scoped_connection.cc

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2023, The libsigc++ Development Team
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*
18+
*/
19+
20+
#include <sigc++/scoped_connection.h>
21+
#include <utility>
22+
23+
namespace sigc
24+
{
25+
26+
// connection() might not be noexcept, but this one can be.
27+
scoped_connection::scoped_connection(connection c) noexcept
28+
: conn_(std::move(c))
29+
{
30+
}
31+
32+
scoped_connection&
33+
scoped_connection::operator=(connection c)
34+
{
35+
conn_.disconnect();
36+
conn_ = std::move(c);
37+
return *this;
38+
}
39+
40+
// We do not implement move-ctor in terms of move-assign, so we can be noexcept,
41+
// as we do not need to call the maybe-throwing disconnect() for obvious reason.
42+
scoped_connection::scoped_connection(scoped_connection&& sc) noexcept
43+
: conn_(std::move(sc.conn_))
44+
{
45+
sc.conn_ = connection(); // Make moved-from scoped connection refer to no slot
46+
}
47+
48+
scoped_connection&
49+
scoped_connection::operator=(scoped_connection&& sc)
50+
{
51+
conn_.disconnect();
52+
conn_ = std::move(sc.conn_);
53+
sc.conn_ = connection(); // Make moved-from scoped connection refer to no slot
54+
return *this;
55+
}
56+
57+
scoped_connection::~scoped_connection()
58+
{
59+
conn_.disconnect();
60+
}
61+
62+
bool
63+
scoped_connection::empty() const noexcept
64+
{
65+
return conn_.empty();
66+
}
67+
68+
bool
69+
scoped_connection::connected() const noexcept
70+
{
71+
return conn_.connected();
72+
}
73+
74+
bool
75+
scoped_connection::blocked() const noexcept
76+
{
77+
return conn_.blocked();
78+
}
79+
80+
bool
81+
scoped_connection::block(bool should_block) noexcept
82+
{
83+
return conn_.block();
84+
}
85+
86+
bool
87+
scoped_connection::unblock() noexcept
88+
{
89+
return conn_.unblock();
90+
}
91+
92+
void
93+
scoped_connection::disconnect()
94+
{
95+
conn_.disconnect();
96+
}
97+
98+
scoped_connection::operator bool() const noexcept
99+
{
100+
return conn_.operator bool();
101+
}
102+
103+
// Swapping can be noexcept, as it does not need to disconnect either connection
104+
// because they will still stay alive, just in opposite instances post-swapping.
105+
void
106+
swap(scoped_connection &sca, scoped_connection &scb) noexcept
107+
{
108+
using std::swap;
109+
swap(sca.conn_, scb.conn_);
110+
}
111+
112+
} /* namespace sigc */

sigc++/scoped_connection.h

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright 2023, The libsigc++ Development Team
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*
18+
*/
19+
20+
#ifndef SIGC_SCOPED_CONNECTION_HPP
21+
#define SIGC_SCOPED_CONNECTION_HPP
22+
23+
#include <sigc++/connection.h>
24+
25+
namespace sigc
26+
{
27+
28+
/** Convenience class for safe disconnection, including automatic disconnection
29+
* upon destruction.
30+
*
31+
* This is a variant of @ref sigc::connection which also disconnect()s the slot
32+
* automatically when the scoped_connection is destructed or re-assigned. Refer
33+
* to @ref sigc::connection for full information about the common functionality.
34+
*
35+
* You will use sigc::scoped_connection by constructing it from a ‘normal’,
36+
* unscoped @ref sigc::connection, such as those returned by
37+
* @ref sigc::signal_with_accumulator::connect() "sigc::signal::connect()", thus
38+
* ‘wrapping’ the connection in a scoped_connection, adding auto-disconnection.
39+
* It can also be assigned from an unscoped connection, in which case, if there
40+
* was a previous slot referred to by the scoped connection, it is disconnected.
41+
*
42+
* Once a connection is scoped, it canʼt be copied as that would make it unclear
43+
* which of the copies would hold responsibility to auto-disconnect the slot. It
44+
* can, however, be moved, so itʼs usable in containers or so ‘ownership’ of the
45+
* connection/auto-disconnect can be moved to another instance. Moving from the
46+
* scoped_connection clears its reference to the slot so it wonʼt disconnect it.
47+
*
48+
* If you want a reference-counted scoped_connection, wrap in a std::shared_ptr.
49+
*
50+
* @code
51+
* // Automatic disconnection:
52+
* {
53+
* sigc::scoped_connection sconn = sig.connect(&some_function);
54+
* // Do stuff that requires the slot to be connected & called.
55+
* }
56+
* // The scoped_connection was destroyed, so the slot is no longer connected.
57+
*
58+
* // ***
59+
*
60+
* // Moving ownership:
61+
* {
62+
* sigc::scoped_connection sconn = sig.connect(&some_function);
63+
* // Do stuff that requires the slot to be connected & called.
64+
* take_ownership(std::move(sconn)); // Pass by rvalue.
65+
* }
66+
* // Now our `sconn` no longer referred to slot, so it did NOT auto-disconnect.
67+
*
68+
* // ***
69+
*
70+
* // Shared ownership:
71+
* {
72+
* auto shconn = std::make_shared<sigc::scoped_connection>(&some_function);
73+
* take_copy(shconn); // Pass by copy/value
74+
* // Now we AND take_copy() must destroy our shared_ptr to auto-disconnect().
75+
* }
76+
* // take_copy() may still hold a shared_ptr reference, keeping the slot alive.
77+
* @endcode
78+
*
79+
* @ingroup signal
80+
*/
81+
struct SIGC_API scoped_connection
82+
{
83+
/** Constructs an empty scoped connection object. */
84+
scoped_connection() noexcept = default;
85+
86+
/** Constructs a scoped connection object from an unscoped connection object.
87+
* The source connection still refers to the slot and can manually disconnect.
88+
* @param c The connection object to make a copy from, whose slot weʼll
89+
* automatically disconnect when the scoped_connection object is destroyed.
90+
*/
91+
scoped_connection(connection c) noexcept;
92+
93+
/** Overrides this scoped connection object copying an unscoped connection.
94+
* The current slot, if any, will be disconnect()ed before being replaced.
95+
* The source connection still refers to the slot and can manually disconnect.
96+
* @param c The connection object to make a copy from, whose slot weʼll
97+
* automatically disconnect when the scoped_connection object is destroyed.
98+
*/
99+
scoped_connection& operator=(connection c);
100+
101+
/// scoped_connection canʼt be copied as it would confuse ownership—see intro.
102+
scoped_connection& operator=(const scoped_connection&) = delete;
103+
/// scoped_connection canʼt be copied as it would confuse ownership—see intro.
104+
scoped_connection(const scoped_connection&) = delete;
105+
106+
/** Constructs a scoped connection object moving an existing one.
107+
* The current slot, if any, will be disconnect()ed before being replaced.
108+
* The source scoped connection will no longer refer to / disconnect the slot.
109+
* @param sc The scoped connection object to move from.
110+
*/
111+
scoped_connection(scoped_connection&& sc) noexcept;
112+
113+
/** Overrides this scoped connection object moving another one.
114+
* The current slot, if any, will be disconnect()ed before being replaced.
115+
* The source scoped connection will no longer refer to / disconnect the slot.
116+
* @param sc The scoped connection object to move from.
117+
*/
118+
scoped_connection& operator=(scoped_connection&& sc);
119+
120+
/// Swap two scoped connections.
121+
friend void swap(scoped_connection &sca, scoped_connection &scb) noexcept;
122+
123+
/// scoped_connection disconnects the referred slot, if any, upon destruction.
124+
~scoped_connection();
125+
126+
/** Returns whether the connection is still active.
127+
* @return @p false if the connection is still active.
128+
*/
129+
bool empty() const noexcept;
130+
131+
/** Returns whether the connection is still active.
132+
* @return @p true if the connection is still active.
133+
*/
134+
bool connected() const noexcept;
135+
136+
/** Returns whether the connection is blocked.
137+
* @return @p true if the connection is blocked.
138+
*/
139+
bool blocked() const noexcept;
140+
141+
/** Sets or unsets the blocking state of this connection.
142+
* See slot_base::block() for details.
143+
* @param should_block Indicates whether the blocking state should be set or unset.
144+
* @return @p true if the connection has been in blocking state before.
145+
*/
146+
bool block(bool should_block = true) noexcept;
147+
148+
/** Unsets the blocking state of this connection.
149+
* @return @p true if the connection has been in blocking state before.
150+
*/
151+
bool unblock() noexcept;
152+
153+
/// Disconnects the referred slot. This will also happen upon destruction.
154+
void disconnect();
155+
156+
/** Returns whether the connection is still active.
157+
* @return @p true if the connection is still active.
158+
*/
159+
explicit operator bool() const noexcept;
160+
161+
private:
162+
sigc::connection conn_;
163+
};
164+
165+
} /* namespace sigc */
166+
167+
#endif /* SIGC_SCOPED_CONNECTION_HPP */

tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test_programs = [
2929
[[], 'test_retype', ['test_retype.cc', 'testutilities.cc']],
3030
[[], 'test_retype_return', ['test_retype_return.cc', 'testutilities.cc']],
3131
[[], 'test_rvalue_ref', ['test_rvalue_ref.cc', 'testutilities.cc']],
32+
[[], 'test_scoped_connection', ['test_scoped_connection.cc', 'testutilities.cc']],
3233
[[], 'test_signal', ['test_signal.cc', 'testutilities.cc']],
3334
[[], 'test_signal_move', ['test_signal_move.cc', 'testutilities.cc']],
3435
[[], 'test_size', ['test_size.cc', 'testutilities.cc']],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy