Line data Source code
1 : // Copyright (c) 2018-2021 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 <memory> 11 : #include <utility> 12 : 13 : namespace interfaces { 14 : namespace { 15 : 16 : class HandlerImpl : public Handler 17 : { 18 : public: 19 0 : explicit HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {} 20 : 21 0 : void disconnect() override { m_connection.disconnect(); } 22 : 23 : boost::signals2::scoped_connection m_connection; 24 : }; 25 : 26 : class CleanupHandler : public Handler 27 : { 28 : public: 29 2 : explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {} 30 3 : ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } 31 0 : void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; } 32 : std::function<void()> m_cleanup; 33 : }; 34 : 35 : } // namespace 36 : 37 0 : std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection) 38 : { 39 0 : return std::make_unique<HandlerImpl>(std::move(connection)); 40 : } 41 : 42 1 : std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup) 43 : { 44 1 : return std::make_unique<CleanupHandler>(std::move(cleanup)); 45 : } 46 : 47 : } // namespace interfaces