Skip to content

Commit b38efb6

Browse files
committed
make alert category flags be a strong type
1 parent c167e28 commit b38efb6

26 files changed

+365
-245
lines changed

bindings/python/src/alert.cpp

+24-25
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ namespace boost
235235
#undef POLY
236236
}
237237

238+
struct dummy3 {};
239+
238240
void bind_alert()
239241
{
240242
using boost::noncopyable;
@@ -263,31 +265,28 @@ void bind_alert()
263265
;
264266
#endif
265267

266-
enum_<alert::category_t>("category_t")
267-
.value("error_notification", alert::error_notification)
268-
.value("peer_notification", alert::peer_notification)
269-
.value("port_mapping_notification", alert::port_mapping_notification)
270-
.value("storage_notification", alert::storage_notification)
271-
.value("tracker_notification", alert::tracker_notification)
272-
.value("debug_notification", alert::debug_notification)
273-
.value("status_notification", alert::status_notification)
274-
.value("progress_notification", alert::progress_notification)
275-
.value("ip_block_notification", alert::ip_block_notification)
276-
.value("performance_warning", alert::performance_warning)
277-
.value("dht_notification", alert::dht_notification)
278-
.value("stats_notification", alert::stats_notification)
279-
.value("session_log_notification", alert::session_log_notification)
280-
.value("torrent_log_notification", alert::torrent_log_notification)
281-
.value("peer_log_notification", alert::peer_log_notification)
282-
.value("incoming_request_notification", alert::incoming_request_notification)
283-
.value("dht_log_notification", alert::dht_log_notification)
284-
.value("dht_operation_notification", alert::dht_operation_notification)
285-
.value("port_mapping_log_notification", alert::port_mapping_log_notification)
286-
.value("picker_log_notification", alert::picker_log_notification)
287-
// deliberately not INT_MAX. Arch linux crash while throwing an exception
288-
.value("all_categories", (alert::category_t)0xfffffff)
289-
;
290-
268+
scope s = class_<dummy3>("category_t");
269+
s.attr("error_notification") = alert::error_notification;
270+
s.attr("peer_notification") = alert::peer_notification;
271+
s.attr("port_mapping_notification") = alert::port_mapping_notification;
272+
s.attr("storage_notification") = alert::storage_notification;
273+
s.attr("tracker_notification") = alert::tracker_notification;
274+
s.attr("debug_notification") = alert::debug_notification;
275+
s.attr("status_notification") = alert::status_notification;
276+
s.attr("progress_notification") = alert::progress_notification;
277+
s.attr("ip_block_notification") = alert::ip_block_notification;
278+
s.attr("performance_warning") = alert::performance_warning;
279+
s.attr("dht_notification") = alert::dht_notification;
280+
s.attr("stats_notification") = alert::stats_notification;
281+
s.attr("session_log_notification") = alert::session_log_notification;
282+
s.attr("torrent_log_notification") = alert::torrent_log_notification;
283+
s.attr("peer_log_notification") = alert::peer_log_notification;
284+
s.attr("incoming_request_notification") = alert::incoming_request_notification;
285+
s.attr("dht_log_notification") = alert::dht_log_notification;
286+
s.attr("dht_operation_notification") = alert::dht_operation_notification;
287+
s.attr("port_mapping_log_notification") = alert::port_mapping_log_notification;
288+
s.attr("picker_log_notification") = alert::picker_log_notification;
289+
s.attr("all_categories") = alert::all_categories;
291290
}
292291

293292
enum_<operation_t>("operation_t")

bindings/python/src/converters.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "libtorrent/aux_/noexcept_movable.hpp"
1616
#include "libtorrent/peer_info.hpp"
1717
#include "libtorrent/alert_types.hpp" // for picker_flags_t
18+
#include "libtorrent/alert.hpp"
1819
#include <vector>
1920

2021
using namespace boost::python;
@@ -297,6 +298,7 @@ void bind_converters()
297298
to_python_converter<lt::bandwidth_state_flags_t, from_bitfield_flag<lt::bandwidth_state_flags_t>>();
298299
to_python_converter<lt::file_open_mode_t, from_bitfield_flag<lt::file_open_mode_t>>();
299300
to_python_converter<lt::picker_flags_t, from_bitfield_flag<lt::picker_flags_t>>();
301+
to_python_converter<lt::alert_category_t, from_bitfield_flag<lt::alert_category_t>>();
300302

301303
// work-around types
302304
to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple<
@@ -345,4 +347,5 @@ void bind_converters()
345347
to_bitfield_flag<lt::bandwidth_state_flags_t>();
346348
to_bitfield_flag<lt::file_open_mode_t>();
347349
to_bitfield_flag<lt::picker_flags_t>();
350+
to_bitfield_flag<lt::alert_category_t>();
348351
}

examples/client_test.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1260,13 +1260,13 @@ MAGNETURL is a magnet link
12601260
settings.set_str(settings_pack::user_agent, "client_test/" LIBTORRENT_VERSION);
12611261
settings.set_int(settings_pack::alert_mask, alert::all_categories
12621262
& ~(alert::dht_notification
1263-
+ alert::progress_notification
1264-
+ alert::stats_notification
1265-
+ alert::session_log_notification
1266-
+ alert::torrent_log_notification
1267-
+ alert::peer_log_notification
1268-
+ alert::dht_log_notification
1269-
+ alert::picker_log_notification
1263+
| alert::progress_notification
1264+
| alert::stats_notification
1265+
| alert::session_log_notification
1266+
| alert::torrent_log_notification
1267+
| alert::peer_log_notification
1268+
| alert::dht_log_notification
1269+
| alert::picker_log_notification
12701270
));
12711271

12721272
lt::session ses(std::move(params));

include/libtorrent/alert.hpp

+96-96
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
4343
//
4444
// By default, only errors are reported. settings_pack::alert_mask can be
4545
// used to specify which kinds of events should be reported. The alert mask is
46-
// comprised by bits from the category_t enum.
46+
// a combination of the alert_category_t flags in the alert class.
4747
//
4848
// Every alert belongs to one or more category. There is a cost associated with
4949
// posting alerts. Only alerts that belong to an enabled category are
@@ -63,9 +63,14 @@ POSSIBILITY OF SUCH DAMAGE.
6363

6464
#include "libtorrent/time.hpp"
6565
#include "libtorrent/config.hpp"
66+
#include "libtorrent/flags.hpp"
6667

6768
namespace libtorrent {
6869

70+
// hidden
71+
struct alert_category_tag;
72+
using alert_category_t = flags::bitfield_flag<std::uint32_t, alert_category_tag>;
73+
6974
// The ``alert`` class is the base class that specific messages are derived from.
7075
// alert types are not copyable, and cannot be constructed by the client. The
7176
// pointers returned by libtorrent are short lived (the details are described
@@ -83,112 +88,107 @@ namespace libtorrent {
8388
enum TORRENT_DEPRECATED severity_t { debug, info, warning, critical, fatal, none };
8489
#endif
8590

86-
// these are bits for the alert_mask used by the session. See
87-
// settings_pack::alert_mask.
88-
enum category_t
89-
{
90-
// Enables alerts that report an error. This includes:
91-
//
92-
// * tracker errors
93-
// * tracker warnings
94-
// * file errors
95-
// * resume data failures
96-
// * web seed errors
97-
// * .torrent files errors
98-
// * listen socket errors
99-
// * port mapping errors
100-
error_notification = 0x1,
101-
102-
// Enables alerts when peers send invalid requests, get banned or
103-
// snubbed.
104-
peer_notification = 0x2,
105-
106-
// Enables alerts for port mapping events. For NAT-PMP and UPnP.
107-
port_mapping_notification = 0x4,
108-
109-
// Enables alerts for events related to the storage. File errors and
110-
// synchronization events for moving the storage, renaming files etc.
111-
storage_notification = 0x8,
112-
113-
// Enables all tracker events. Includes announcing to trackers,
114-
// receiving responses, warnings and errors.
115-
tracker_notification = 0x10,
116-
117-
// Low level alerts for when peers are connected and disconnected.
118-
debug_notification = 0x20,
119-
120-
// Enables alerts for when a torrent or the session changes state.
121-
status_notification = 0x40,
122-
123-
// Alerts for when blocks are requested and completed. Also when
124-
// pieces are completed.
125-
progress_notification = 0x80,
126-
127-
// Alerts when a peer is blocked by the ip blocker or port blocker.
128-
ip_block_notification = 0x100,
129-
130-
// Alerts when some limit is reached that might limit the download
131-
// or upload rate.
132-
performance_warning = 0x200,
133-
134-
// Alerts on events in the DHT node. For incoming searches or
135-
// bootstrapping being done etc.
136-
dht_notification = 0x400,
137-
138-
// If you enable these alerts, you will receive a stats_alert
139-
// approximately once every second, for every active torrent.
140-
// These alerts contain all statistics counters for the interval since
141-
// the lasts stats alert.
142-
stats_notification = 0x800,
91+
// Enables alerts that report an error. This includes:
92+
//
93+
// * tracker errors
94+
// * tracker warnings
95+
// * file errors
96+
// * resume data failures
97+
// * web seed errors
98+
// * .torrent files errors
99+
// * listen socket errors
100+
// * port mapping errors
101+
static constexpr alert_category_t error_notification{0x1};
102+
103+
// Enables alerts when peers send invalid requests, get banned or
104+
// snubbed.
105+
static constexpr alert_category_t peer_notification{0x2};
106+
107+
// Enables alerts for port mapping events. For NAT-PMP and UPnP.
108+
static constexpr alert_category_t port_mapping_notification{0x4};
109+
110+
// Enables alerts for events related to the storage. File errors and
111+
// synchronization events for moving the storage, renaming files etc.
112+
static constexpr alert_category_t storage_notification{0x8};
113+
114+
// Enables all tracker events. Includes announcing to trackers,
115+
// receiving responses, warnings and errors.
116+
static constexpr alert_category_t tracker_notification{0x10};
117+
118+
// Low level alerts for when peers are connected and disconnected.
119+
static constexpr alert_category_t debug_notification{0x20};
120+
121+
// Enables alerts for when a torrent or the session changes state.
122+
static constexpr alert_category_t status_notification{0x40};
123+
124+
// Alerts for when blocks are requested and completed. Also when
125+
// pieces are completed.
126+
static constexpr alert_category_t progress_notification{0x80};
127+
128+
// Alerts when a peer is blocked by the ip blocker or port blocker.
129+
static constexpr alert_category_t ip_block_notification{0x100};
130+
131+
// Alerts when some limit is reached that might limit the download
132+
// or upload rate.
133+
static constexpr alert_category_t performance_warning{0x200};
134+
135+
// Alerts on events in the DHT node. For incoming searches or
136+
// bootstrapping being done etc.
137+
static constexpr alert_category_t dht_notification{0x400};
138+
139+
// If you enable these alerts, you will receive a stats_alert
140+
// approximately once every second, for every active torrent.
141+
// These alerts contain all statistics counters for the interval since
142+
// the lasts stats alert.
143+
static constexpr alert_category_t stats_notification{0x800};
143144

144145
#ifndef TORRENT_NO_DEPRECATE
145-
// Alerts on RSS related events, like feeds being updated, feed error
146-
// conditions and successful RSS feed updates. Enabling this category
147-
// will make you receive rss_alert alerts.
148-
rss_notification TORRENT_DEPRECATED_ENUM = 0x1000,
146+
// Alerts on RSS related events, like feeds being updated, feed error
147+
// conditions and successful RSS feed updates. Enabling this category
148+
// will make you receive rss_alert alerts.
149+
static constexpr alert_category_t TORRENT_DEPRECATED_MEMBER rss_notification{0x1000};
149150
#endif
150151

151-
// Enables debug logging alerts. These are available unless libtorrent
152-
// was built with logging disabled (``TORRENT_DISABLE_LOGGING``). The
153-
// alerts being posted are log_alert and are session wide.
154-
session_log_notification = 0x2000,
152+
// Enables debug logging alerts. These are available unless libtorrent
153+
// was built with logging disabled (``TORRENT_DISABLE_LOGGING``). The
154+
// alerts being posted are log_alert and are session wide.
155+
static constexpr alert_category_t session_log_notification{0x2000};
155156

156-
// Enables debug logging alerts for torrents. These are available
157-
// unless libtorrent was built with logging disabled
158-
// (``TORRENT_DISABLE_LOGGING``). The alerts being posted are
159-
// torrent_log_alert and are torrent wide debug events.
160-
torrent_log_notification = 0x4000,
157+
// Enables debug logging alerts for torrents. These are available
158+
// unless libtorrent was built with logging disabled
159+
// (``TORRENT_DISABLE_LOGGING``). The alerts being posted are
160+
// torrent_log_alert and are torrent wide debug events.
161+
static constexpr alert_category_t torrent_log_notification{0x4000};
161162

162-
// Enables debug logging alerts for peers. These are available unless
163-
// libtorrent was built with logging disabled
164-
// (``TORRENT_DISABLE_LOGGING``). The alerts being posted are
165-
// peer_log_alert and low-level peer events and messages.
166-
peer_log_notification = 0x8000,
163+
// Enables debug logging alerts for peers. These are available unless
164+
// libtorrent was built with logging disabled
165+
// (``TORRENT_DISABLE_LOGGING``). The alerts being posted are
166+
// peer_log_alert and low-level peer events and messages.
167+
static constexpr alert_category_t peer_log_notification{0x8000};
167168

168-
// enables the incoming_request_alert.
169-
incoming_request_notification = 0x10000,
169+
// enables the incoming_request_alert.
170+
static constexpr alert_category_t incoming_request_notification{0x10000};
170171

171-
// enables dht_log_alert, debug logging for the DHT
172-
dht_log_notification = 0x20000,
172+
// enables dht_log_alert, debug logging for the DHT
173+
static constexpr alert_category_t dht_log_notification{0x20000};
173174

174-
// enable events from pure dht operations not related to torrents
175-
dht_operation_notification = 0x40000,
175+
// enable events from pure dht operations not related to torrents
176+
static constexpr alert_category_t dht_operation_notification{0x40000};
176177

177-
// enables port mapping log events. This log is useful
178-
// for debugging the UPnP or NAT-PMP implementation
179-
port_mapping_log_notification = 0x80000,
178+
// enables port mapping log events. This log is useful
179+
// for debugging the UPnP or NAT-PMP implementation
180+
static constexpr alert_category_t port_mapping_log_notification{0x80000};
180181

181-
// enables verbose logging from the piece picker.
182-
picker_log_notification = 0x100000,
182+
// enables verbose logging from the piece picker.
183+
static constexpr alert_category_t picker_log_notification{0x100000};
183184

184-
// The full bitmask, representing all available categories.
185-
//
186-
// since the enum is signed, make sure this isn't
187-
// interpreted as -1. For instance, boost.python
188-
// does that and fails when assigning it to an
189-
// unsigned parameter.
190-
all_categories = 0x7fffffff
191-
};
185+
// The full bitmask, representing all available categories.
186+
//
187+
// since the enum is signed, make sure this isn't
188+
// interpreted as -1. For instance, boost.python
189+
// does that and fails when assigning it to an
190+
// unsigned parameter.
191+
static constexpr alert_category_t all_categories{0x7fffffff};
192192

193193
// hidden
194194
alert();
@@ -242,7 +242,7 @@ namespace libtorrent {
242242
virtual std::string message() const = 0;
243243

244244
// returns a bitmask specifying which categories this alert belong to.
245-
virtual int category() const = 0;
245+
virtual alert_category_t category() const = 0;
246246

247247
#ifndef TORRENT_NO_DEPRECATE
248248

include/libtorrent/alert_manager.hpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace libtorrent {
5555
{
5656
public:
5757
alert_manager(int queue_limit
58-
, std::uint32_t alert_mask = alert::error_notification);
58+
, alert_category_t alert_mask = alert::error_notification);
5959
~alert_manager();
6060

6161
template <class T, typename... Args>
@@ -90,8 +90,7 @@ namespace libtorrent {
9090
template <class T>
9191
bool should_post() const
9292
{
93-
if ((m_alert_mask.load(std::memory_order_relaxed)
94-
& T::static_category) == 0)
93+
if (!(m_alert_mask.load(std::memory_order_relaxed) & T::static_category))
9594
{
9695
return false;
9796
}
@@ -101,12 +100,12 @@ namespace libtorrent {
101100

102101
alert* wait_for_alert(time_duration max_wait);
103102

104-
void set_alert_mask(std::uint32_t m)
103+
void set_alert_mask(alert_category_t const m)
105104
{
106105
m_alert_mask = m;
107106
}
108107

109-
std::uint32_t alert_mask() const
108+
alert_category_t alert_mask() const
110109
{
111110
return m_alert_mask;
112111
}
@@ -131,7 +130,7 @@ namespace libtorrent {
131130

132131
mutable std::mutex m_mutex;
133132
std::condition_variable m_condition;
134-
std::atomic<std::uint32_t> m_alert_mask;
133+
std::atomic<alert_category_t> m_alert_mask;
135134
int m_queue_size_limit;
136135

137136
// this function (if set) is called whenever the number of alerts in

0 commit comments

Comments
 (0)