From bb65acb08281df1bf9c81378eed28b35674e29c1 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 14 Oct 2017 17:40:27 +0200 Subject: [PATCH] make feature_flags proper type-safe --- docs/upgrade_to_1.2.rst | 2 +- include/libtorrent/extensions.hpp | 38 ++++++++++++++++--------------- src/session.cpp | 10 ++++++++ src/session_impl.cpp | 2 +- test/test_direct_dht.cpp | 2 +- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/docs/upgrade_to_1.2.rst b/docs/upgrade_to_1.2.rst index ecab3a4f8..f2e9ce45a 100644 --- a/docs/upgrade_to_1.2.rst +++ b/docs/upgrade_to_1.2.rst @@ -120,7 +120,7 @@ plugins ======= libtorrent session plugins no longer have all callbacks called unconditionally. -The callback has to register which callbacks it's interested in receiving by returning a bitmask from ``std::uint32_t implemented_features()``. +The callback has to register which callbacks it's interested in receiving by returning a bitmask from ``feature_flags_t implemented_features()``. The return value is documented in the plugin class. RSS functions removed diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index bcea2e11a..1e84eafcf 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_EXTENSIONS_HPP_INCLUDED #include "libtorrent/units.hpp" +#include "libtorrent/flags.hpp" // OVERVIEW // @@ -182,6 +183,12 @@ namespace libtorrent { struct session_handle; struct peer_connection_handle; + struct feature_flags_tag; + + // these are flags that can be returned by implemented_features() + // indicating which callbacks this plugin is interested in + using feature_flags_t = flags::bitfield_flag; + // this is the base class for a session plugin. One primary feature // is that it is notified of all torrents that are added to the session, // and can add its own torrent_plugins. @@ -190,26 +197,21 @@ namespace libtorrent { // hidden virtual ~plugin() {} - // these are flags that can be returned by implemented_features() - // indicating which callbacks this plugin is interested in - enum feature_flags_t - { - // include this bit if your plugin needs to alter the order of the - // optimistic unchoke of peers. i.e. have the on_optimistic_unchoke() - // callback be called. - optimistic_unchoke_feature = 1, + // include this bit if your plugin needs to alter the order of the + // optimistic unchoke of peers. i.e. have the on_optimistic_unchoke() + // callback be called. + static constexpr feature_flags_t optimistic_unchoke_feature = 1_bit; - // include this bit if your plugin needs to have on_tick() called - tick_feature = 2, + // include this bit if your plugin needs to have on_tick() called + static constexpr feature_flags_t tick_feature = 2_bit; - // include this bit if your plugin needs to have on_dht_request() - // called - dht_request_feature = 4, + // include this bit if your plugin needs to have on_dht_request() + // called + static constexpr feature_flags_t dht_request_feature = 3_bit; - // include this bit if your plugin needs to have on_alert() - // called - alert_feature = 8, - }; + // include this bit if your plugin needs to have on_alert() + // called + static constexpr feature_flags_t alert_feature = 4_bit; // This function is expected to return a bitmask indicating which features // this plugin implements. Some callbacks on this object may not be called @@ -217,7 +219,7 @@ namespace libtorrent { // callbacks may still be called even if the corresponding feature is not // specified in the return value here. See feature_flags_t for possible // flags to return. - virtual std::uint32_t implemented_features() { return 0; } + virtual feature_flags_t implemented_features() { return {}; } // this is called by the session every time a new torrent is added. // The ``torrent*`` points to the internal torrent object created diff --git a/src/session.cpp b/src/session.cpp index adf3b45bd..5d1e150f6 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -35,11 +35,21 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/extensions/ut_metadata.hpp" #include "libtorrent/extensions/smart_ban.hpp" #include "libtorrent/session.hpp" +#include "libtorrent/extensions.hpp" #include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_call.hpp" namespace libtorrent { +#ifndef TORRENT_DISABLE_EXTENSIONS + // declared in extensions.hpp + // remove this once C++17 is required + constexpr feature_flags_t plugin::optimistic_unchoke_feature; + constexpr feature_flags_t plugin::tick_feature; + constexpr feature_flags_t plugin::dht_request_feature; + constexpr feature_flags_t plugin::alert_feature; +#endif + settings_pack min_memory_usage() { settings_pack set; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 6a93cb7d4..88a14f9d1 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -815,7 +815,7 @@ namespace aux { // TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT_VAL(ext, ext); - std::uint32_t const features = ext->implemented_features(); + feature_flags_t const features = ext->implemented_features(); m_ses_extensions[plugins_all_idx].push_back(ext); diff --git a/test/test_direct_dht.cpp b/test/test_direct_dht.cpp index e0fcd44bf..ebb5449a4 100644 --- a/test/test_direct_dht.cpp +++ b/test/test_direct_dht.cpp @@ -47,7 +47,7 @@ namespace struct test_plugin : plugin { - std::uint32_t implemented_features() override + feature_flags_t implemented_features() override { return plugin::dht_request_feature; }