Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
6
7#include <common/system.h>
8
9#include <boost/signals2/connection.hpp>
10#include <utility>
11
12namespace interfaces {
13namespace {
14
15 class HandlerImpl : public Handler {
16 public:
17 explicit HandlerImpl(boost::signals2::connection 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
48std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection) {
49 return std::make_unique<HandlerImpl>(std::move(connection));
50}
51
52std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup) {
53 return std::make_unique<CleanupHandler>(std::move(cleanup));
54}
55
56} // namespace interfaces
std::function< void()> m_cleanup
Definition handler.cpp:43
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:48
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition rcu.h:259
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85