Bitcoin Core 31.99.0
P2P Digital Currency
interfaces.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <interfaces/echo.h> // IWYU pragma: associated
6#include <interfaces/handler.h> // IWYU pragma: associated
7
8#include <util/btcsignals.h>
9
10#include <memory>
11#include <utility>
12
13namespace common {
14namespace {
15class CleanupHandler : public interfaces::Handler
16{
17public:
18 explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {}
19 ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
20 void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
21 std::function<void()> m_cleanup;
22};
23
24class SignalHandler : public interfaces::Handler
25{
26public:
27 explicit SignalHandler(btcsignals::connection connection) : m_connection(std::move(connection)) {}
28
29 void disconnect() override { m_connection.disconnect(); }
30
32};
33
34class EchoImpl : public interfaces::Echo
35{
36public:
37 std::string echo(const std::string& echo) override { return echo; }
38};
39} // namespace
40} // namespace common
41
42namespace interfaces {
43std::unique_ptr<Handler> MakeCleanupHandler(std::function<void()> cleanup)
44{
45 return std::make_unique<common::CleanupHandler>(std::move(cleanup));
46}
47
48std::unique_ptr<Handler> MakeSignalHandler(btcsignals::connection connection)
49{
50 return std::make_unique<common::SignalHandler>(std::move(connection));
51}
52
53std::unique_ptr<Echo> MakeEcho() { return std::make_unique<common::EchoImpl>(); }
54} // namespace interfaces
Simple string echoing interface for testing.
Definition: echo.h:14
Generic interface for managing an event handler or callback function registered with another interfac...
Definition: handler.h:21
std::function< void()> m_cleanup
Definition: interfaces.cpp:21
btcsignals::scoped_connection m_connection
Definition: interfaces.cpp:31
Definition: init.cpp:17
std::unique_ptr< Echo > MakeEcho()
Return implementation of Echo interface.
Definition: interfaces.cpp:53
std::unique_ptr< Handler > MakeCleanupHandler(std::function< void()> cleanup)
Return handler wrapping a cleanup function.
Definition: interfaces.cpp:43
std::unique_ptr< Handler > MakeSignalHandler(btcsignals::connection connection)
Return handler wrapping a btcsignals connection.
Definition: interfaces.cpp:48
static RPCMethod echo(const std::string &name)
Definition: node.cpp:272