Bitcoin Core 22.99.0
P2P Digital Currency
handler.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2020 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
6
7
8#include <boost/signals2/connection.hpp>
9#include <utility>
10
11namespace interfaces {
12namespace {
13
14class HandlerImpl : public Handler
15{
16public:
17 explicit HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {}
18
19 void disconnect() override { m_connection.disconnect(); }
20
21 boost::signals2::scoped_connection m_connection;
22};
23
24class CleanupHandler : public Handler
25{
26public:
27 explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {}
28 ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
29 void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
30 std::function<void()> m_cleanup;
31};
32
33} // namespace
34
35std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection)
36{
37 return std::make_unique<HandlerImpl>(std::move(connection));
38}
39
40std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup)
41{
42 return std::make_unique<CleanupHandler>(std::move(cleanup));
43}
44
45} // namespace interfaces
std::function< void()> m_cleanup
Definition: handler.cpp:30
boost::signals2::scoped_connection m_connection
Definition: handler.cpp:21
std::unique_ptr< Handler > MakeHandler(boost::signals2::connection connection)
Return handler wrapping a boost signal connection.
Definition: handler.cpp:35