diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 39e704a0d..44431de22 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -300,10 +300,10 @@ namespace aux { io_service& get_io_service() override { return m_io_service; } resolver_interface& get_resolver() override { return m_host_resolver; } - aux::vector& torrent_list(int i) override + aux::vector& torrent_list(torrent_list_index_t i) override { - TORRENT_ASSERT(i >= 0); - TORRENT_ASSERT(i < session_interface::num_torrent_lists); + TORRENT_ASSERT(i >= torrent_list_index_t{}); + TORRENT_ASSERT(i < m_torrent_lists.end_index()); return m_torrent_lists[i]; } @@ -739,7 +739,8 @@ namespace aux { // negative, return INT_MAX int get_int_setting(int n) const; - aux::vector m_torrent_lists[num_torrent_lists]; + aux::array, num_torrent_lists, torrent_list_index_t> + m_torrent_lists; peer_class_pool m_classes; diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index ab363bd81..0a17613df 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/listen_socket_handle.hpp" #include "libtorrent/session_types.hpp" #include "libtorrent/flags.hpp" +#include "libtorrent/link.hpp" // for torrent_list_index_t #include #include @@ -252,41 +253,35 @@ namespace libtorrent { namespace aux { virtual void sent_syn(bool ipv6) = 0; virtual void received_synack(bool ipv6) = 0; - enum torrent_list_index - { - // this is the set of (subscribed) torrents that have changed - // their states since the last time the user requested updates. - torrent_state_updates, + // this is the set of (subscribed) torrents that have changed + // their states since the last time the user requested updates. + static constexpr torrent_list_index_t torrent_state_updates{0}; // all torrents that want to be ticked every second - torrent_want_tick, + static constexpr torrent_list_index_t torrent_want_tick{1}; // all torrents that want more peers and are still downloading // these typically have higher priority when connecting peers - torrent_want_peers_download, + static constexpr torrent_list_index_t torrent_want_peers_download{2}; // all torrents that want more peers and are finished downloading - torrent_want_peers_finished, + static constexpr torrent_list_index_t torrent_want_peers_finished{3}; // torrents that want auto-scrape (only paused auto-managed ones) - torrent_want_scrape, + static constexpr torrent_list_index_t torrent_want_scrape{4}; // auto-managed torrents by state. Only these torrents are considered // when recalculating auto-managed torrents. started auto managed // torrents that are inactive are not part of these lists, because they // are not considered for auto managing (they are left started // unconditionally) - torrent_downloading_auto_managed, - torrent_seeding_auto_managed, - torrent_checking_auto_managed, + static constexpr torrent_list_index_t torrent_downloading_auto_managed{5}; + static constexpr torrent_list_index_t torrent_seeding_auto_managed{6}; + static constexpr torrent_list_index_t torrent_checking_auto_managed{7}; - // all torrents that have resume data to save -// torrent_want_save_resume, + static constexpr std::size_t num_torrent_lists = 8; - num_torrent_lists - }; - - virtual aux::vector& torrent_list(int i) = 0; + virtual aux::vector& torrent_list(torrent_list_index_t i) = 0; virtual bool has_lsd() const = 0; virtual void announce_lsd(sha1_hash const& ih, int port, bool broadcast = false) = 0; diff --git a/include/libtorrent/link.hpp b/include/libtorrent/link.hpp index ae0dbf03e..7520c4046 100644 --- a/include/libtorrent/link.hpp +++ b/include/libtorrent/link.hpp @@ -34,9 +34,13 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_LINK_HPP_INCLUDED #include "libtorrent/aux_/vector.hpp" +#include "libtorrent/units.hpp" namespace libtorrent { + struct torrent_list_tag; + using torrent_list_index_t = aux::strong_typedef; + struct link { link() : index(-1) {} @@ -50,7 +54,8 @@ namespace libtorrent { void clear() { index = -1; } template - void unlink(aux::vector& list, int link_index) + void unlink(aux::vector& list + , torrent_list_index_t const link_index) { if (index == -1) return; TORRENT_ASSERT(index >= 0 && index < int(list.size())); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 9f25af61f..dae01611b 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1164,7 +1164,7 @@ namespace libtorrent { torrent_state get_peer_list_state(); void construct_storage(); - void update_list(int list, bool in); + void update_list(torrent_list_index_t list, bool in); void on_files_deleted(storage_error const& error); void on_torrent_paused(); @@ -1366,7 +1366,8 @@ namespace libtorrent { // TODO: 3 factor out the links (as well as update_list() to a separate // class that torrent can inherit) - link m_links[aux::session_interface::num_torrent_lists]; + aux::array + m_links; private: diff --git a/src/session.cpp b/src/session.cpp index 5d1e150f6..b2cfd24dc 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -50,6 +50,17 @@ namespace libtorrent { constexpr feature_flags_t plugin::alert_feature; #endif +namespace aux { + constexpr torrent_list_index_t session_interface::torrent_state_updates; + constexpr torrent_list_index_t session_interface::torrent_want_tick; + constexpr torrent_list_index_t session_interface::torrent_want_peers_download; + constexpr torrent_list_index_t session_interface::torrent_want_peers_finished; + constexpr torrent_list_index_t session_interface::torrent_want_scrape; + constexpr torrent_list_index_t session_interface::torrent_downloading_auto_managed; + constexpr torrent_list_index_t session_interface::torrent_seeding_auto_managed; + constexpr torrent_list_index_t session_interface::torrent_checking_auto_managed; +} + settings_pack min_memory_usage() { settings_pack set; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 36b3164b5..415feba57 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -6817,7 +6817,7 @@ namespace { && m_settings.get_int(settings_pack::choking_algorithm) == settings_pack::fixed_slots_choker) TORRENT_ASSERT(m_stats_counters[counters::num_unchoke_slots] == (std::numeric_limits::max)()); - for (int l = 0; l < num_torrent_lists; ++l) + for (torrent_list_index_t l{}; l != m_torrent_lists.end_index(); ++l) { std::vector const& list = m_torrent_lists[l]; for (auto const& i : list) diff --git a/src/torrent.cpp b/src/torrent.cpp index ddd737f92..b01d9de0f 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -782,7 +782,7 @@ namespace libtorrent { // torrent #if TORRENT_USE_ASSERTS - for (int i = 0; i < aux::session_interface::num_torrent_lists; ++i) + for (torrent_list_index_t i{}; i != m_links.end_index(); ++i) { if (!m_links[i].in_list()) continue; m_links[i].unlink(m_ses.torrent_list(i), i); @@ -4418,7 +4418,7 @@ namespace libtorrent { m_paused = false; m_auto_managed = false; update_state_list(); - for (int i = 0; i < aux::session_interface::num_torrent_lists; ++i) + for (torrent_list_index_t i{}; i != m_links.end_index(); ++i) { if (!m_links[i].in_list()) continue; m_links[i].unlink(m_ses.torrent_list(i), i); @@ -7113,10 +7113,10 @@ namespace libtorrent { namespace { #ifndef TORRENT_DISABLE_LOGGING - char const* list_name(int idx) + char const* list_name(torrent_list_index_t const idx) { -#define TORRENT_LIST_NAME(n) case aux::session_interface:: n: return #n; - switch (idx) +#define TORRENT_LIST_NAME(n) case static_cast(aux::session_interface:: n): return #n; + switch (static_cast(idx)) { TORRENT_LIST_NAME(torrent_state_updates); TORRENT_LIST_NAME(torrent_want_tick); @@ -7135,7 +7135,7 @@ namespace libtorrent { } // anonymous namespace - void torrent::update_list(int list, bool in) + void torrent::update_list(torrent_list_index_t const list, bool in) { link& l = m_links[list]; aux::vector& v = m_ses.torrent_list(list); @@ -7742,7 +7742,7 @@ namespace libtorrent { } #if TORRENT_USE_ASSERTS - for (int i = 0; i < aux::session_interface::num_torrent_lists; ++i) + for (torrent_list_index_t i{}; i != m_links.end_index(); ++i) { if (!m_links[i].in_list()) continue; int const index = m_links[i].index;