/tmp/bitcoin/src/common/interfaces.cpp
Line | Count | Source |
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 | | |
13 | | namespace common { |
14 | | namespace { |
15 | | class CleanupHandler : public interfaces::Handler |
16 | | { |
17 | | public: |
18 | 10 | explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {} |
19 | 10 | ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } |
20 | 0 | void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } |
21 | | std::function<void()> m_cleanup; |
22 | | }; |
23 | | |
24 | | class SignalHandler : public interfaces::Handler |
25 | | { |
26 | | public: |
27 | 0 | explicit SignalHandler(btcsignals::connection connection) : m_connection(std::move(connection)) {} |
28 | | |
29 | 0 | void disconnect() override { m_connection.disconnect(); } |
30 | | |
31 | | btcsignals::scoped_connection m_connection; |
32 | | }; |
33 | | |
34 | | class EchoImpl : public interfaces::Echo |
35 | | { |
36 | | public: |
37 | 7 | std::string echo(const std::string& echo) override { return echo; } |
38 | | }; |
39 | | } // namespace |
40 | | } // namespace common |
41 | | |
42 | | namespace interfaces { |
43 | | std::unique_ptr<Handler> MakeCleanupHandler(std::function<void()> cleanup) |
44 | 10 | { |
45 | 10 | return std::make_unique<common::CleanupHandler>(std::move(cleanup)); |
46 | 10 | } |
47 | | |
48 | | std::unique_ptr<Handler> MakeSignalHandler(btcsignals::connection connection) |
49 | 0 | { |
50 | 0 | return std::make_unique<common::SignalHandler>(std::move(connection)); |
51 | 0 | } |
52 | | |
53 | 7 | std::unique_ptr<Echo> MakeEcho() { return std::make_unique<common::EchoImpl>(); } |
54 | | } // namespace interfaces |