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