2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2018-2020 The Bitcoin Core developers
|
2017-04-17 13:55:43 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-04-07 03:42:02 -04:00
|
|
|
#include <interfaces/handler.h>
|
2017-04-17 13:55:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include <boost/signals2/connection.hpp>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-04-07 03:42:02 -04:00
|
|
|
namespace interfaces {
|
2017-04-17 13:55:43 -04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class HandlerImpl : public Handler
|
|
|
|
{
|
|
|
|
public:
|
2018-07-26 17:15:32 +02:00
|
|
|
explicit HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {}
|
2017-04-17 13:55:43 -04:00
|
|
|
|
|
|
|
void disconnect() override { m_connection.disconnect(); }
|
|
|
|
|
|
|
|
boost::signals2::scoped_connection m_connection;
|
|
|
|
};
|
|
|
|
|
2019-09-27 07:31:44 -04:00
|
|
|
class CleanupHandler : public Handler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {}
|
|
|
|
~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
|
|
|
|
void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
|
|
|
|
std::function<void()> m_cleanup;
|
|
|
|
};
|
|
|
|
|
2017-04-17 13:55:43 -04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection)
|
|
|
|
{
|
2021-03-10 17:28:08 +08:00
|
|
|
return std::make_unique<HandlerImpl>(std::move(connection));
|
2017-04-17 13:55:43 -04:00
|
|
|
}
|
|
|
|
|
2019-09-27 07:31:44 -04:00
|
|
|
std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup)
|
|
|
|
{
|
2021-03-10 17:28:08 +08:00
|
|
|
return std::make_unique<CleanupHandler>(std::move(cleanup));
|
2019-09-27 07:31:44 -04:00
|
|
|
}
|
|
|
|
|
2018-04-07 03:42:02 -04:00
|
|
|
} // namespace interfaces
|