2 * Copyright © 2013 Canonical Ltd.
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
18 #ifndef COM_UBUNTU_CONNECTION_H_
19 #define COM_UBUNTU_CONNECTION_H_
27 class ScopedConnection;
29 * @brief The Connection class models a signal-slot connection.
34 typedef std::function<void(const std::function<void()>&)> Dispatcher;
37 * @brief Checks if this instance corresponds to an active signal-slot connection.
38 * @return true iff the instance corresponds to an active signal-slot connection.
40 inline bool is_connected() const
45 return (d->disconnector ? true : false);
49 * @brief End a signal-slot connection.
51 inline void disconnect()
58 * @brief Installs a dispatcher for this signal-slot connection.
59 * @param dispatcher The dispatcher to be used for signal emissions.
61 inline void dispatch_via(const Dispatcher& dispatcher)
63 if (d && d->dispatcher_installer)
64 d->dispatcher_installer(dispatcher);
68 friend class ScopedConnection;
70 typedef std::function<void()> Disconnector;
71 typedef std::function<void(const Dispatcher&)> DispatcherInstaller;
73 template<typename ... Arguments> friend class Signal;
75 inline Connection(const Disconnector& disconnector,
76 const DispatcherInstaller& installer)
77 : d(std::make_shared<Private>(disconnector, installer))
81 inline bool operator<(const Connection& rhs) const
94 Private(const Connection::Disconnector& disconnector,
95 const Connection::DispatcherInstaller& dispatcher_installer)
96 : disconnector(disconnector),
97 dispatcher_installer(dispatcher_installer)
103 std::lock_guard<std::mutex> lg(guard);
107 inline void reset_locked()
109 static const Connection::Disconnector empty_disconnector{};
110 static const Connection::DispatcherInstaller empty_dispatcher_installer{};
112 disconnector = empty_disconnector;
113 dispatcher_installer = empty_dispatcher_installer;
116 inline void disconnect()
118 static const Connection::Disconnector empty_disconnector{};
120 std::lock_guard<std::mutex> lg(guard);
129 Connection::Disconnector disconnector;
130 Connection::DispatcherInstaller dispatcher_installer;
133 // The whole class is implicitly shared and we thus forward our complete
134 // shared state to a private structure that is lifetime-managed by a shared_ptr.
135 std::shared_ptr<Private> d;
139 * @brief Scoped helper class to map signal-slot connection mgmt. to RAII.
141 class ScopedConnection
145 * @brief Constructs an instance for an existing signal-slot connection.
146 * @param c The existing signal-slot connection.
148 inline ScopedConnection(const Connection& c) : connection(c)
152 inline ScopedConnection(ScopedConnection&& rhs) : connection(std::move(rhs.connection))
156 ScopedConnection(const ScopedConnection&) = delete;
159 * @brief Disconnects the signal-slot connection.
161 inline ~ScopedConnection() noexcept(true)
165 connection.disconnect();
171 inline ScopedConnection& operator=(ScopedConnection&& rhs)
173 connection = std::move(rhs.connection);
177 ScopedConnection& operator=(const ScopedConnection&) = delete;
178 bool operator==(const ScopedConnection&) = delete;
180 inline bool operator<(const ScopedConnection& rhs) const
182 return connection < rhs.connection;
186 Connection connection;
190 #endif // COM_UBUNTU_CONNECTION_H_