diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 2e8fb8334..9332caf93 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -2687,7 +2687,7 @@ namespace libtorrent { #undef TORRENT_DEFINE_ALERT #undef TORRENT_DEFINE_ALERT_PRIO - enum { num_alert_types = 95 }; // this enum represents "max_alert_index" + 1 + constexpr int num_alert_types = 95; // this constant represents "max_alert_index" + 1 } #endif diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index a88320a42..d6f07b7cc 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -240,7 +240,8 @@ namespace aux { , std::enable_shared_from_this { // the size of each allocation that is chained in the send buffer - enum { send_buffer_size_impl = 128 }; + static constexpr int send_buffer_size_impl = 128; + // plugin feature-index key map enum { diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index 35ce78d4d..77e9c4e49 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -129,7 +129,7 @@ namespace aux { char* buf; - enum { max_refcount = (1 << 29) - 1 }; + static constexpr int max_refcount = (1 << 29) - 1; // the number of references to this buffer. These references // might be in outstanding asynchronous requests or in peer diff --git a/include/libtorrent/kademlia/dos_blocker.hpp b/include/libtorrent/kademlia/dos_blocker.hpp index 84a35ea21..3a15e1e17 100644 --- a/include/libtorrent/kademlia/dos_blocker.hpp +++ b/include/libtorrent/kademlia/dos_blocker.hpp @@ -76,7 +76,7 @@ namespace libtorrent { namespace dht { int count; }; - enum { num_ban_nodes = 20 }; + static constexpr int num_ban_nodes = 20; // the max number of packets we can receive per second from a node before // we block it. diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index d0f65d15f..7595ae407 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -99,7 +99,7 @@ namespace aux { piece_block block; - enum { not_in_buffer = 0x1fffffff }; + static constexpr std::uint32_t not_in_buffer = 0x1fffffff; // the number of bytes into the send buffer this request is. Every time // some portion of the send buffer is transmitted, this offset is diff --git a/include/libtorrent/piece_picker.hpp b/include/libtorrent/piece_picker.hpp index 52c59be59..2bfd17bf8 100644 --- a/include/libtorrent/piece_picker.hpp +++ b/include/libtorrent/piece_picker.hpp @@ -577,15 +577,13 @@ namespace libtorrent { // index is set to this to indicate that we have the // piece. There is no entry for the piece in the // buckets if this is the case. - constexpr static prio_index_t we_have_index{-1}; + static constexpr prio_index_t we_have_index{-1}; - enum : std::uint32_t - { - // the priority value that means the piece is filtered - filter_priority = 0, - // the max number the peer count can hold - max_peer_count = 0xffff - }; + // the priority value that means the piece is filtered + static constexpr std::uint32_t filter_priority = 0; + + // the max number the peer count can hold + static constexpr std::uint32_t max_peer_count = 0xffff; bool have() const { return index == we_have_index; } void set_have() { index = we_have_index; TORRENT_ASSERT(have()); } diff --git a/include/libtorrent/sha1_hash.hpp b/include/libtorrent/sha1_hash.hpp index c9b454668..88610615f 100644 --- a/include/libtorrent/sha1_hash.hpp +++ b/include/libtorrent/sha1_hash.hpp @@ -67,7 +67,7 @@ namespace aux { class digest32 { static_assert(N % 32 == 0, "N must be a multiple of 32"); - enum { number_size = N / 32 }; + static constexpr int number_size = N / 32; public: // the size of the hash in bytes @@ -143,14 +143,14 @@ namespace aux { // shift left ``n`` bits. digest32& operator<<=(int n) { - aux::bits_shift_left({m_number, number_size}, n); + aux::bits_shift_left({m_number, std::size_t(number_size)}, n); return *this; } // shift right ``n`` bits. digest32& operator>>=(int n) { - aux::bits_shift_right({m_number, number_size}, n); + aux::bits_shift_right({m_number, std::size_t(number_size)}, n); return *this; } @@ -177,7 +177,7 @@ namespace aux { int count_leading_zeroes() const { - return aux::count_leading_zeros({m_number, number_size}); + return aux::count_leading_zeros({m_number, std::size_t(number_size)}); } // returns a bit-wise negated copy of the digest diff --git a/include/libtorrent/socket_type.hpp b/include/libtorrent/socket_type.hpp index 6673e2222..ce1dc64b4 100644 --- a/include/libtorrent/socket_type.hpp +++ b/include/libtorrent/socket_type.hpp @@ -133,46 +133,46 @@ namespace libtorrent { template struct socket_type_int_impl - { enum { value = 0 }; }; + { static constexpr int value = 0; }; template <> struct socket_type_int_impl - { enum { value = 1 }; }; + { static constexpr int value = 1; }; template <> struct socket_type_int_impl - { enum { value = 2 }; }; + { static constexpr int value = 2; }; template <> struct socket_type_int_impl - { enum { value = 3 }; }; + { static constexpr int value = 3; }; template <> struct socket_type_int_impl - { enum { value = 4 }; }; + { static constexpr int value = 4; }; #if TORRENT_USE_I2P template <> struct socket_type_int_impl - { enum { value = 5 }; }; + { static constexpr int value = 5; }; #endif #ifdef TORRENT_USE_OPENSSL template <> struct socket_type_int_impl> - { enum { value = 6 }; }; + { static constexpr int value = 6; }; template <> struct socket_type_int_impl> - { enum { value = 7 }; }; + { static constexpr int value = 7; }; template <> struct socket_type_int_impl> - { enum { value = 8 }; }; + { static constexpr int value = 8; }; template <> struct socket_type_int_impl> - { enum { value = 9 }; }; + { static constexpr int value = 9; }; #endif struct TORRENT_EXTRA_EXPORT socket_type diff --git a/include/libtorrent/timestamp_history.hpp b/include/libtorrent/timestamp_history.hpp index 59049188f..ff784b1aa 100644 --- a/include/libtorrent/timestamp_history.hpp +++ b/include/libtorrent/timestamp_history.hpp @@ -44,7 +44,7 @@ namespace libtorrent { // seen in the last 20 minutes struct TORRENT_EXTRA_EXPORT timestamp_history { - enum { history_size = 20 }; + static constexpr int history_size = 20; timestamp_history() : m_base(0), m_index(0), m_num_samples(not_initialized) {} bool initialized() const { return m_num_samples != not_initialized; } @@ -68,7 +68,7 @@ private: // in the circular buffer std::uint16_t m_index; - enum { not_initialized = 0xffff }; + static constexpr std::uint16_t not_initialized = 0xffff; // this is the number of samples since the // last time we stepped one minute. If we diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 4221901cd..f39da3ccf 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1135,7 +1135,7 @@ namespace libtorrent { } void add_suggest_piece(piece_index_t index); - enum { no_gauge_state = 0xf }; + static constexpr int no_gauge_state = 0xf; private: diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index cc72b7688..5bc2f5e8d 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -202,12 +202,6 @@ private: static address_v4 upnp_multicast_address; static udp::endpoint upnp_multicast_endpoint; - // there are routers that's don't support timed - // port maps, without returning error 725. It seems - // safer to always assume that we have to ask for - // permanent leases - enum { default_lease_time = 0 }; - void resend_request(error_code const& e); void on_reply(udp::endpoint const& from, char* buffer , std::size_t bytes_transferred); @@ -294,7 +288,11 @@ private: std::string path; address external_ip; - int lease_duration = default_lease_time; + // there are routers that's don't support timed + // port maps, without returning error 725. It seems + // safer to always assume that we have to ask for + // permanent leases + int lease_duration = 0; // true if the device supports specifying a // specific external port, false if it doesn't diff --git a/src/enum_net.cpp b/src/enum_net.cpp index 4ca5b121b..eb64f9ce4 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -100,7 +100,8 @@ const unsigned long siocgifmtu = SIOCGIFMTU; #define IF_NAMESIZE IFNAMSIZ #endif -namespace libtorrent {namespace { +namespace libtorrent { +namespace { #if !defined TORRENT_BUILD_SIMULATOR @@ -201,7 +202,7 @@ namespace libtorrent {namespace { return msg_len; } - enum { NL_BUFSIZE = 8192 }; + constexpr int NL_BUFSIZE = 8192; int nl_dump_request(int sock, std::uint16_t type, std::uint32_t seq, char family, span msg, std::size_t msg_len) { @@ -495,9 +496,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl } #endif -}} // - -namespace libtorrent { +} // // return (a1 & mask) == (a2 & mask) bool match_addr_mask(address const& a1, address const& a2, address const& mask) diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index a04598d08..700590360 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -78,7 +78,7 @@ namespace { }; // TODO: 2 make this configurable in dht_settings - enum { announce_interval = 30 }; + constexpr time_duration announce_interval = minutes(30); struct dht_immutable_item { @@ -552,7 +552,7 @@ namespace { auto new_end = std::remove_if(peers.begin(), peers.end() , [=](peer_entry const& e) { - return e.added + minutes(int(announce_interval * 3 / 2)) < now; + return e.added + announce_interval * 3 / 2 < now; }); m_counters.peers -= std::int32_t(std::distance(new_end, peers.end())); diff --git a/src/torrent.cpp b/src/torrent.cpp index 1d78b82f8..a72f4577b 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -210,7 +210,7 @@ namespace libtorrent { , m_ssl_torrent(false) , m_deleted(false) , m_auto_managed(p.flags & torrent_flags::auto_managed) - , m_current_gauge_state(no_gauge_state) + , m_current_gauge_state(static_cast(no_gauge_state)) , m_moving_storage(false) , m_inactive(false) , m_downloaded(0xffffff) @@ -534,7 +534,9 @@ namespace libtorrent { if (new_gauge_state != no_gauge_state) inc_stats_counter(new_gauge_state + counters::num_checking_torrents, 1); - m_current_gauge_state = aux::numeric_cast(new_gauge_state); + TORRENT_ASSERT(new_gauge_state >= 0); + TORRENT_ASSERT(new_gauge_state <= no_gauge_state); + m_current_gauge_state = static_cast(new_gauge_state); } void torrent::leave_seed_mode(bool skip_checking) @@ -9345,7 +9347,7 @@ namespace libtorrent { { // this is a silly optimization // to avoid copying of strings - enum { num_strings = 200 }; + int const num_strings = 200; static char buf[num_strings][20]; static int round_robin = 0; char* ret = buf[round_robin]; diff --git a/src/upnp.cpp b/src/upnp.cpp index 558f81060..27cd45176 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -1377,7 +1377,7 @@ void upnp::on_upnp_map_response(error_code const& e if (s.error_code == 725) { - // only permanent leases supported + // The gateway only supports permanent leases d.lease_duration = 0; m.act = portmap_action::add; ++m.failcount;