|
| 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 final |
| 82 | +{ |
| 83 | + /** Constructs an empty scoped connection object. */ |
| 84 | + [[nodiscard]] 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 | + [[nodiscard]] 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 | + [[nodiscard]] 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 | + [[nodiscard]] bool connected() const noexcept; |
| 135 | + |
| 136 | + /** Returns whether the connection is blocked. |
| 137 | + * @return @p true if the connection is blocked. |
| 138 | + */ |
| 139 | + [[nodiscard]] 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 | + [[nodiscard]] explicit operator bool() const noexcept; |
| 160 | + |
| 161 | + /** Releases the connection from a scoped connection object. |
| 162 | + * The scoped connection will no longer refer to / disconnect the slot. |
| 163 | + * @return An unscoped connection object referring to the same slot. |
| 164 | + */ |
| 165 | + [[nodiscard]] connection release() noexcept; |
| 166 | + |
| 167 | +private: |
| 168 | + sigc::connection conn_; |
| 169 | +}; |
| 170 | + |
| 171 | +} /* namespace sigc */ |
| 172 | + |
| 173 | +#endif /* SIGC_SCOPED_CONNECTION_HPP */ |
0 commit comments