introduce a type to refer to a torrent-list-index, to improve type safety

This commit is contained in:
arvidn 2017-10-23 20:24:54 +02:00 committed by Arvid Norberg
parent b380bf8059
commit 9e69bf3618
7 changed files with 46 additions and 33 deletions

View File

@ -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*>& torrent_list(int i) override
aux::vector<torrent*>& 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<torrent*> m_torrent_lists[num_torrent_lists];
aux::array<aux::vector<torrent*>, num_torrent_lists, torrent_list_index_t>
m_torrent_lists;
peer_class_pool m_classes;

View File

@ -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 <functional>
#include <memory>
@ -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*>& torrent_list(int i) = 0;
virtual aux::vector<torrent*>& 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;

View File

@ -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<int, torrent_list_tag>;
struct link
{
link() : index(-1) {}
@ -50,7 +54,8 @@ namespace libtorrent {
void clear() { index = -1; }
template <class T>
void unlink(aux::vector<T*>& list, int link_index)
void unlink(aux::vector<T*>& list
, torrent_list_index_t const link_index)
{
if (index == -1) return;
TORRENT_ASSERT(index >= 0 && index < int(list.size()));

View File

@ -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<link, aux::session_interface::num_torrent_lists, torrent_list_index_t>
m_links;
private:

View File

@ -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;

View File

@ -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<int>::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<torrent*> const& list = m_torrent_lists[l];
for (auto const& i : list)

View File

@ -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<int>(aux::session_interface:: n): return #n;
switch (static_cast<int>(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<torrent*>& 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;