refactor: move tr_session::getAllTorrents to tr_torrents (#6179)

This commit is contained in:
Charles Kerr 2023-10-30 16:38:02 -04:00 committed by GitHub
parent 1c421d6d23
commit f0e9f90782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 26 deletions

View File

@ -126,15 +126,9 @@ auto getTorrents(tr_session* session, tr_variant* args)
{
if (sv == "recently-active"sv)
{
time_t const cutoff = tr_time() - RecentlyActiveSeconds;
auto const& by_id = session->torrents().sorted_by_id();
torrents.reserve(std::size(by_id));
std::copy_if(
std::begin(by_id),
std::end(by_id),
std::back_inserter(torrents),
[&cutoff](auto const* tor) { return tor != nullptr && tor->has_changed_since(cutoff); });
auto const cutoff = tr_time() - RecentlyActiveSeconds;
torrents = session->torrents().get_matching([cutoff](tr_torrent const* tor)
{ return tor->has_changed_since(cutoff); });
}
else
{
@ -147,13 +141,7 @@ auto getTorrents(tr_session* session, tr_variant* args)
}
else // all of them
{
auto const& by_id = session->torrents().sorted_by_id();
torrents.reserve(std::size(by_id));
std::copy_if(
std::begin(by_id),
std::end(by_id),
std::back_inserter(torrents),
[](auto const* tor) { return tor != nullptr; });
torrents = session->torrents().get_all();
}
return torrents;

View File

@ -1345,7 +1345,7 @@ void tr_session::closeImplPart1(std::promise<void>* closed_promise, std::chrono:
// Close the torrents in order of most active to least active
// so that the most important announce=stopped events are
// fired out first...
auto torrents = getAllTorrents();
auto torrents = torrents_.get_all();
std::sort(
std::begin(torrents),
std::end(torrents),

View File

@ -795,11 +795,6 @@ public:
return settings_.idle_seeding_limit_minutes;
}
[[nodiscard]] std::vector<tr_torrent*> getAllTorrents() const
{
return std::vector<tr_torrent*>{ std::begin(torrents()), std::end(torrents()) };
}
/*module_visible*/
auto rpcNotify(tr_rpc_callback_type type, tr_torrent* tor = nullptr)

View File

@ -498,9 +498,9 @@ constexpr struct
} CompareTorrentByQueuePosition{};
#ifdef TR_ENABLE_ASSERTS
bool queueIsSequenced(tr_session const* session)
bool queueIsSequenced(tr_session const* const session)
{
auto torrents = session->getAllTorrents();
auto torrents = session->torrents().get_all();
std::sort(
std::begin(torrents),
std::end(torrents),

View File

@ -9,8 +9,11 @@
#error only libtransmission should #include this header.
#endif
#include <algorithm>
#include <cstddef> // size_t
#include <ctime>
#include <functional>
#include <iterator>
#include <string_view>
#include <utility>
#include <vector>
@ -107,9 +110,22 @@ public:
return std::empty(by_hash_);
}
[[nodiscard]] constexpr auto const& sorted_by_id() const noexcept
[[nodiscard]] auto get_matching(std::function<bool(tr_torrent const*)> pred_in) const
{
return by_id_;
auto const pred = [&pred_in](tr_torrent const* const tor)
{
return tor != nullptr && pred_in(tor);
};
auto vec = std::vector<tr_torrent*>{};
vec.reserve(size());
std::copy_if(std::begin(by_id_), std::end(by_id_), std::back_inserter(vec), pred);
return vec;
}
[[nodiscard]] auto get_all() const
{
return get_matching([](tr_torrent const*) { return true; });
}
private: