Skip to content

Commit bb65acb

Browse files
committed
make feature_flags proper type-safe
1 parent f0bac4c commit bb65acb

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

docs/upgrade_to_1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ plugins
120120
=======
121121

122122
libtorrent session plugins no longer have all callbacks called unconditionally.
123-
The callback has to register which callbacks it's interested in receiving by returning a bitmask from ``std::uint32_t implemented_features()``.
123+
The callback has to register which callbacks it's interested in receiving by returning a bitmask from ``feature_flags_t implemented_features()``.
124124
The return value is documented in the plugin class.
125125

126126
RSS functions removed

include/libtorrent/extensions.hpp

+23-21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
3434
#define TORRENT_EXTENSIONS_HPP_INCLUDED
3535

3636
#include "libtorrent/units.hpp"
37+
#include "libtorrent/flags.hpp"
3738

3839
// OVERVIEW
3940
//
@@ -182,6 +183,12 @@ namespace libtorrent {
182183
struct session_handle;
183184
struct peer_connection_handle;
184185

186+
struct feature_flags_tag;
187+
188+
// these are flags that can be returned by implemented_features()
189+
// indicating which callbacks this plugin is interested in
190+
using feature_flags_t = flags::bitfield_flag<std::uint8_t, feature_flags_tag>;
191+
185192
// this is the base class for a session plugin. One primary feature
186193
// is that it is notified of all torrents that are added to the session,
187194
// and can add its own torrent_plugins.
@@ -190,34 +197,29 @@ namespace libtorrent {
190197
// hidden
191198
virtual ~plugin() {}
192199

193-
// these are flags that can be returned by implemented_features()
194-
// indicating which callbacks this plugin is interested in
195-
enum feature_flags_t
196-
{
197-
// include this bit if your plugin needs to alter the order of the
198-
// optimistic unchoke of peers. i.e. have the on_optimistic_unchoke()
199-
// callback be called.
200-
optimistic_unchoke_feature = 1,
201-
202-
// include this bit if your plugin needs to have on_tick() called
203-
tick_feature = 2,
204-
205-
// include this bit if your plugin needs to have on_dht_request()
206-
// called
207-
dht_request_feature = 4,
208-
209-
// include this bit if your plugin needs to have on_alert()
210-
// called
211-
alert_feature = 8,
212-
};
200+
// include this bit if your plugin needs to alter the order of the
201+
// optimistic unchoke of peers. i.e. have the on_optimistic_unchoke()
202+
// callback be called.
203+
static constexpr feature_flags_t optimistic_unchoke_feature = 1_bit;
204+
205+
// include this bit if your plugin needs to have on_tick() called
206+
static constexpr feature_flags_t tick_feature = 2_bit;
207+
208+
// include this bit if your plugin needs to have on_dht_request()
209+
// called
210+
static constexpr feature_flags_t dht_request_feature = 3_bit;
211+
212+
// include this bit if your plugin needs to have on_alert()
213+
// called
214+
static constexpr feature_flags_t alert_feature = 4_bit;
213215

214216
// This function is expected to return a bitmask indicating which features
215217
// this plugin implements. Some callbacks on this object may not be called
216218
// unless the corresponding feature flag is returned here. Note that
217219
// callbacks may still be called even if the corresponding feature is not
218220
// specified in the return value here. See feature_flags_t for possible
219221
// flags to return.
220-
virtual std::uint32_t implemented_features() { return 0; }
222+
virtual feature_flags_t implemented_features() { return {}; }
221223

222224
// this is called by the session every time a new torrent is added.
223225
// The ``torrent*`` points to the internal torrent object created

src/session.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,21 @@ POSSIBILITY OF SUCH DAMAGE.
3535
#include "libtorrent/extensions/ut_metadata.hpp"
3636
#include "libtorrent/extensions/smart_ban.hpp"
3737
#include "libtorrent/session.hpp"
38+
#include "libtorrent/extensions.hpp"
3839
#include "libtorrent/aux_/session_impl.hpp"
3940
#include "libtorrent/aux_/session_call.hpp"
4041

4142
namespace libtorrent {
4243

44+
#ifndef TORRENT_DISABLE_EXTENSIONS
45+
// declared in extensions.hpp
46+
// remove this once C++17 is required
47+
constexpr feature_flags_t plugin::optimistic_unchoke_feature;
48+
constexpr feature_flags_t plugin::tick_feature;
49+
constexpr feature_flags_t plugin::dht_request_feature;
50+
constexpr feature_flags_t plugin::alert_feature;
51+
#endif
52+
4353
settings_pack min_memory_usage()
4454
{
4555
settings_pack set;

src/session_impl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ namespace aux {
815815
// TORRENT_ASSERT(is_single_thread());
816816
TORRENT_ASSERT_VAL(ext, ext);
817817

818-
std::uint32_t const features = ext->implemented_features();
818+
feature_flags_t const features = ext->implemented_features();
819819

820820
m_ses_extensions[plugins_all_idx].push_back(ext);
821821

test/test_direct_dht.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace
4747

4848
struct test_plugin : plugin
4949
{
50-
std::uint32_t implemented_features() override
50+
feature_flags_t implemented_features() override
5151
{
5252
return plugin::dht_request_feature;
5353
}

0 commit comments

Comments
 (0)