make feature_flags proper type-safe

This commit is contained in:
arvidn 2017-10-14 17:40:27 +02:00 committed by Arvid Norberg
parent f0bac4cac7
commit bb65acb082
5 changed files with 33 additions and 21 deletions

View File

@ -120,7 +120,7 @@ plugins
======= =======
libtorrent session plugins no longer have all callbacks called unconditionally. 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. The return value is documented in the plugin class.
RSS functions removed RSS functions removed

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_EXTENSIONS_HPP_INCLUDED #define TORRENT_EXTENSIONS_HPP_INCLUDED
#include "libtorrent/units.hpp" #include "libtorrent/units.hpp"
#include "libtorrent/flags.hpp"
// OVERVIEW // OVERVIEW
// //
@ -182,6 +183,12 @@ namespace libtorrent {
struct session_handle; struct session_handle;
struct peer_connection_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<std::uint8_t, feature_flags_tag>;
// this is the base class for a session plugin. One primary feature // 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, // is that it is notified of all torrents that are added to the session,
// and can add its own torrent_plugins. // and can add its own torrent_plugins.
@ -190,26 +197,21 @@ namespace libtorrent {
// hidden // hidden
virtual ~plugin() {} virtual ~plugin() {}
// these are flags that can be returned by implemented_features() // include this bit if your plugin needs to alter the order of the
// indicating which callbacks this plugin is interested in // optimistic unchoke of peers. i.e. have the on_optimistic_unchoke()
enum feature_flags_t // callback be called.
{ static constexpr feature_flags_t optimistic_unchoke_feature = 1_bit;
// 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 have on_tick() called // include this bit if your plugin needs to have on_tick() called
tick_feature = 2, static constexpr feature_flags_t tick_feature = 2_bit;
// include this bit if your plugin needs to have on_dht_request() // include this bit if your plugin needs to have on_dht_request()
// called // called
dht_request_feature = 4, static constexpr feature_flags_t dht_request_feature = 3_bit;
// include this bit if your plugin needs to have on_alert() // include this bit if your plugin needs to have on_alert()
// called // called
alert_feature = 8, static constexpr feature_flags_t alert_feature = 4_bit;
};
// This function is expected to return a bitmask indicating which features // This function is expected to return a bitmask indicating which features
// this plugin implements. Some callbacks on this object may not be called // 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 // callbacks may still be called even if the corresponding feature is not
// specified in the return value here. See feature_flags_t for possible // specified in the return value here. See feature_flags_t for possible
// flags to return. // 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. // this is called by the session every time a new torrent is added.
// The ``torrent*`` points to the internal torrent object created // The ``torrent*`` points to the internal torrent object created

View File

@ -35,11 +35,21 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/extensions/ut_metadata.hpp" #include "libtorrent/extensions/ut_metadata.hpp"
#include "libtorrent/extensions/smart_ban.hpp" #include "libtorrent/extensions/smart_ban.hpp"
#include "libtorrent/session.hpp" #include "libtorrent/session.hpp"
#include "libtorrent/extensions.hpp"
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/aux_/session_call.hpp" #include "libtorrent/aux_/session_call.hpp"
namespace libtorrent { 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 min_memory_usage()
{ {
settings_pack set; settings_pack set;

View File

@ -815,7 +815,7 @@ namespace aux {
// TORRENT_ASSERT(is_single_thread()); // TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT_VAL(ext, ext); 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); m_ses_extensions[plugins_all_idx].push_back(ext);

View File

@ -47,7 +47,7 @@ namespace
struct test_plugin : plugin struct test_plugin : plugin
{ {
std::uint32_t implemented_features() override feature_flags_t implemented_features() override
{ {
return plugin::dht_request_feature; return plugin::dht_request_feature;
} }