make feature_flags proper type-safe
This commit is contained in:
parent
f0bac4cac7
commit
bb65acb082
|
@ -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
|
||||
|
|
|
@ -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<std::uint8_t, feature_flags_tag>;
|
||||
|
||||
// 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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue