2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2021-2022 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.
|
|
|
|
|
2022-10-11 23:35:28 -04:00
|
|
|
#include <interfaces/echo.h>
|
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>
|
2022-10-11 23:35:28 -04:00
|
|
|
#include <memory>
|
2017-04-17 13:55:43 -04:00
|
|
|
#include <utility>
|
|
|
|
|
2022-10-11 23:35:28 -04:00
|
|
|
namespace common {
|
2017-04-17 13:55:43 -04:00
|
|
|
namespace {
|
2022-10-11 23:35:28 -04:00
|
|
|
class CleanupHandler : public interfaces::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
|
|
|
|
2022-10-12 10:42:38 -04:00
|
|
|
class SignalHandler : public interfaces::Handler
|
2017-04-17 13:55:43 -04:00
|
|
|
{
|
|
|
|
public:
|
2022-10-12 10:42:38 -04:00
|
|
|
explicit SignalHandler(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;
|
|
|
|
};
|
|
|
|
|
2022-10-11 23:35:28 -04:00
|
|
|
class EchoImpl : public interfaces::Echo
|
2019-09-27 07:31:44 -04:00
|
|
|
{
|
|
|
|
public:
|
2022-10-11 23:35:28 -04:00
|
|
|
std::string echo(const std::string& echo) override { return echo; }
|
2019-09-27 07:31:44 -04:00
|
|
|
};
|
2017-04-17 13:55:43 -04:00
|
|
|
} // namespace
|
2022-10-11 23:35:28 -04:00
|
|
|
} // namespace common
|
2017-04-17 13:55:43 -04:00
|
|
|
|
2022-10-11 23:35:28 -04:00
|
|
|
namespace interfaces {
|
2022-10-12 10:42:38 -04:00
|
|
|
std::unique_ptr<Handler> MakeCleanupHandler(std::function<void()> cleanup)
|
2017-04-17 13:55:43 -04:00
|
|
|
{
|
2022-10-11 23:35:28 -04:00
|
|
|
return std::make_unique<common::CleanupHandler>(std::move(cleanup));
|
2017-04-17 13:55:43 -04:00
|
|
|
}
|
|
|
|
|
2022-10-12 10:42:38 -04:00
|
|
|
std::unique_ptr<Handler> MakeSignalHandler(boost::signals2::connection connection)
|
2019-09-27 07:31:44 -04:00
|
|
|
{
|
2022-10-12 10:42:38 -04:00
|
|
|
return std::make_unique<common::SignalHandler>(std::move(connection));
|
2019-09-27 07:31:44 -04:00
|
|
|
}
|
|
|
|
|
2022-10-11 23:35:28 -04:00
|
|
|
std::unique_ptr<Echo> MakeEcho() { return std::make_unique<common::EchoImpl>(); }
|
2018-04-07 03:42:02 -04:00
|
|
|
} // namespace interfaces
|