Bitcoin ABC  0.24.7
P2P Digital Currency
handler.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 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/system.h>
8 
9 #include <boost/signals2/connection.hpp>
10 #include <utility>
11 
12 namespace interfaces {
13 namespace {
14 
15  class HandlerImpl : public Handler {
16  public:
17  explicit HandlerImpl(boost::signals2::connection connection)
18  : 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  public:
27  explicit CleanupHandler(std::function<void()> cleanup)
28  : m_cleanup(std::move(cleanup)) {}
29  ~CleanupHandler() override {
30  if (!m_cleanup) {
31  return;
32  }
33  m_cleanup();
34  m_cleanup = nullptr;
35  }
36  void disconnect() override {
37  if (!m_cleanup) {
38  return;
39  }
40  m_cleanup();
41  m_cleanup = nullptr;
42  }
43  std::function<void()> m_cleanup;
44  };
45 
46 } // namespace
47 
48 std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection) {
49  return std::make_unique<HandlerImpl>(std::move(connection));
50 }
51 
52 std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup) {
53  return std::make_unique<CleanupHandler>(std::move(cleanup));
54 }
55 
56 } // namespace interfaces
interfaces
Definition: dummywallet.cpp:12
m_cleanup
std::function< void()> m_cleanup
Definition: handler.cpp:43
handler.h
system.h
m_connection
boost::signals2::scoped_connection m_connection
Definition: handler.cpp:22
interfaces::MakeHandler
std::unique_ptr< Handler > MakeHandler(boost::signals2::connection connection)
Return handler wrapping a boost signal connection.
Definition: handler.cpp:48