connection.h
Go to the documentation of this file.
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef COM_UBUNTU_CONNECTION_H_
#define COM_UBUNTU_CONNECTION_H_
#include <functional>
#include <memory>
#include <mutex>
namespace core
{
class ScopedConnection;
class Connection
{
public:
typedef std::function<void(const std::function<void()>&)> Dispatcher;
inline bool is_connected() const
{
if (!d)
return false;
return (d->disconnector ? true : false);
}
inline void disconnect()
{
if (d)
d->disconnect();
}
inline void dispatch_via(const Dispatcher& dispatcher)
{
if (d && d->dispatcher_installer)
d->dispatcher_installer(dispatcher);
}
private:
friend class ScopedConnection;
typedef std::function<void()> Disconnector;
typedef std::function<void(const Dispatcher&)> DispatcherInstaller;
template<typename ... Arguments> friend class Signal;
inline Connection(const Disconnector& disconnector,
const DispatcherInstaller& installer)
: d(std::make_shared<Private>(disconnector, installer))
{
}
inline bool operator<(const Connection& rhs) const
{
return d < rhs.d;
}
inline void reset()
{
if (d)
d->reset();
}
struct Private
{
Private(const Connection::Disconnector& disconnector,
const Connection::DispatcherInstaller& dispatcher_installer)
: disconnector(disconnector),
dispatcher_installer(dispatcher_installer)
{
}
inline void reset()
{
std::lock_guard<std::mutex> lg(guard);
reset_locked();
}
inline void reset_locked()
{
static const Connection::Disconnector empty_disconnector{};
static const Connection::DispatcherInstaller empty_dispatcher_installer{};
disconnector = empty_disconnector;
dispatcher_installer = empty_dispatcher_installer;
}
inline void disconnect()
{
static const Connection::Disconnector empty_disconnector{};
std::lock_guard<std::mutex> lg(guard);
if (disconnector)
disconnector();
reset_locked();
}
std::mutex guard;
Connection::Disconnector disconnector;
Connection::DispatcherInstaller dispatcher_installer;
};
// The whole class is implicitly shared and we thus forward our complete
// shared state to a private structure that is lifetime-managed by a shared_ptr.
std::shared_ptr<Private> d;
};
class ScopedConnection
{
public:
inline ScopedConnection(const Connection& c) : connection(c)
{
}
inline ScopedConnection(ScopedConnection&& rhs) : connection(std::move(rhs.connection))
{
}
ScopedConnection(const ScopedConnection&) = delete;
inline ~ScopedConnection() noexcept(true)
{
try
{
connection.disconnect();
} catch(...)
{
}
}
inline ScopedConnection& operator=(ScopedConnection&& rhs)
{
connection = std::move(rhs.connection);
return *this;
}
ScopedConnection& operator=(const ScopedConnection&) = delete;
bool operator==(const ScopedConnection&) = delete;
inline bool operator<(const ScopedConnection& rhs) const
{
return connection < rhs.connection;
}
private:
Connection connection;
};
}
#endif // COM_UBUNTU_CONNECTION_H_
core::Connection
The Connection class models a signal-slot connection.
Definition connection.h:32
core::Connection::dispatch_via
void dispatch_via(const Dispatcher &dispatcher)
Installs a dispatcher for this signal-slot connection.
Definition connection.h:61
core::Connection::disconnect
void disconnect()
End a signal-slot connection.
Definition connection.h:51
core::Connection::is_connected
bool is_connected() const
Checks if this instance corresponds to an active signal-slot connection.
Definition connection.h:40
core::Connection::Dispatcher
std::function< void(const std::function< void()> &)> Dispatcher
Definition connection.h:34
core::ScopedConnection
Scoped helper class to map signal-slot connection mgmt. to RAII.
Definition connection.h:142
core::ScopedConnection::ScopedConnection
ScopedConnection(const ScopedConnection &)=delete
core::ScopedConnection::ScopedConnection
ScopedConnection(const Connection &c)
Constructs an instance for an existing signal-slot connection.
Definition connection.h:148
core::ScopedConnection::operator=
ScopedConnection & operator=(const ScopedConnection &)=delete
core::ScopedConnection::operator==
bool operator==(const ScopedConnection &)=delete
core::ScopedConnection::~ScopedConnection
~ScopedConnection() noexcept(true)
Disconnects the signal-slot connection.
Definition connection.h:161
core::ScopedConnection::operator<
bool operator<(const ScopedConnection &rhs) const
Definition connection.h:180
core::ScopedConnection::operator=
ScopedConnection & operator=(ScopedConnection &&rhs)
Definition connection.h:171
core::ScopedConnection::ScopedConnection
ScopedConnection(ScopedConnection &&rhs)
Definition connection.h:152
core::Signal
A signal class that observers can subscribe to.
Definition signal.h:37
core
Definition connection.h:26