diff --git a/include/libtorrent/alert.hpp b/include/libtorrent/alert.hpp index 9afef63deed..98b80e1579c 100644 --- a/include/libtorrent/alert.hpp +++ b/include/libtorrent/alert.hpp @@ -4,6 +4,7 @@ Copyright (c) 2003, Daniel Wallin Copyright (c) 2004-2005, 2008-2009, 2013-2020, 2022, Arvid Norberg Copyright (c) 2004, Magnus Jonsson Copyright (c) 2016, Alden Torres +Copyright (c) 2023, Joris Carrier All rights reserved. Redistribution and use in source and binary forms, with or without @@ -37,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_ALERT_HPP_INCLUDED #include +#include // OVERVIEW // @@ -186,6 +188,24 @@ namespace alert_category { #include "libtorrent/aux_/disable_deprecation_warnings_push.hpp" + template + struct TORRENT_EXPORT callback_t { + using type = std::function; + + callback_t(type func) : m_callback(std::move(func)) {} + + bool has_callback() const { + return static_cast(m_callback); + } + + void callback() { + m_callback(static_cast(this)); + } + + private: + type m_callback; + }; + // The ``alert`` class is the base class that specific messages are derived from. // alert types are not copyable, and cannot be constructed by the client. The // pointers returned by libtorrent are short lived (the details are described diff --git a/include/libtorrent/aux_/alert_manager.hpp b/include/libtorrent/aux_/alert_manager.hpp index d5f6237e1e0..f600aad55e3 100644 --- a/include/libtorrent/aux_/alert_manager.hpp +++ b/include/libtorrent/aux_/alert_manager.hpp @@ -3,6 +3,7 @@ Copyright (c) 2003-2013, Daniel Wallin Copyright (c) 2013, 2015-2020, Arvid Norberg Copyright (c) 2016, 2018, 2020, Alden Torres +Copyright (c) 2023, Joris Carrier All rights reserved. Redistribution and use in source and binary forms, with or without @@ -88,6 +89,10 @@ namespace aux { T& alert = queue.emplace_back( m_allocations[m_generation], std::forward(args)...); + if constexpr (std::is_base_of, T>::value) { + if (alert.has_callback()) alert.callback(); + } + maybe_notify(&alert); } catch (std::bad_alloc const&)