From 25fdb5805ccf50684ee9261360e71a6282c23080 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 17 Jun 2022 10:43:04 -0500 Subject: [PATCH] refactor: use tr_torrent_id_t (#3314) * refactor: use semantic type tr_torrent_type_t in libtransmission * refactor: use semantic type tr_torrent_type_t in gtk client * refactor: use semantic type tr_torrent_type_t in qt client --- gtk/Application.cc | 14 ++++----- gtk/DetailsDialog.cc | 43 +++++++++++--------------- gtk/DetailsDialog.h | 2 +- gtk/Dialogs.cc | 2 +- gtk/Dialogs.h | 2 +- gtk/FileList.cc | 22 ++++++------- gtk/FileList.h | 4 +-- gtk/Notify.cc | 14 ++++----- gtk/Notify.h | 6 ++-- gtk/RelocateDialog.cc | 16 +++++++--- gtk/RelocateDialog.h | 4 +-- gtk/Session.cc | 14 ++++----- gtk/Session.h | 12 +++---- libtransmission/cache.cc | 6 ++-- libtransmission/inout.cc | 4 +-- libtransmission/session.cc | 4 +-- libtransmission/torrent.cc | 16 +++++----- libtransmission/torrent.h | 7 ++++- libtransmission/torrents.cc | 18 +++++------ libtransmission/torrents.h | 10 +++--- libtransmission/transmission.h | 6 ++-- libtransmission/webseed.cc | 14 ++++++--- qt/DetailsDialog.cc | 4 +-- qt/Torrent.h | 2 +- qt/Typedefs.h | 4 ++- tests/libtransmission/torrents-test.cc | 20 ++++++------ 26 files changed, 141 insertions(+), 129 deletions(-) diff --git a/gtk/Application.cc b/gtk/Application.cc index aaef9c43f..adc23971f 100644 --- a/gtk/Application.cc +++ b/gtk/Application.cc @@ -109,7 +109,7 @@ private: guint info, guint time_); - bool on_rpc_changed_idle(tr_rpc_callback_type type, int torrent_id); + bool on_rpc_changed_idle(tr_rpc_callback_type type, tr_torrent_id_t torrent_id); void placeWindowFromPrefs(); void presentMainWindow(); @@ -137,7 +137,7 @@ private: void on_add_torrent(tr_ctor* ctor); void on_prefs_changed(tr_quark key); - std::vector get_selected_torrent_ids() const; + std::vector get_selected_torrent_ids() const; tr_torrent* get_first_selected_torrent() const; counts_data get_selected_torrent_counts() const; @@ -193,7 +193,7 @@ void gtr_window_present(T const& window) **** ***/ -std::string get_details_dialog_key(std::vector const& id_list) +std::string get_details_dialog_key(std::vector const& id_list) { auto tmp = id_list; std::sort(tmp.begin(), tmp.end()); @@ -210,9 +210,9 @@ std::string get_details_dialog_key(std::vector const& id_list) } // namespace -std::vector Application::Impl::get_selected_torrent_ids() const +std::vector Application::Impl::get_selected_torrent_ids() const { - std::vector ids; + std::vector ids; sel_->selected_foreach([&ids](auto const& /*path*/, auto const& iter) { ids.push_back(iter->get_value(torrent_cols.torrent_id)); }); return ids; @@ -393,7 +393,7 @@ void Application::Impl::on_main_window_size_allocated(Gtk::Allocation& /*alloc*/ **** listen to changes that come from RPC ***/ -bool Application::Impl::on_rpc_changed_idle(tr_rpc_callback_type type, int torrent_id) +bool Application::Impl::on_rpc_changed_idle(tr_rpc_callback_type type, tr_torrent_id_t torrent_id) { switch (type) { @@ -1335,7 +1335,7 @@ bool Application::Impl::call_rpc_for_selected_torrents(std::string const& method void Application::Impl::remove_selected(bool delete_files) { - std::vector l; + auto l = std::vector{}; sel_->selected_foreach([&l](auto const& /*path*/, auto const& iter) { l.push_back(iter->get_value(torrent_cols.torrent_id)); }); diff --git a/gtk/DetailsDialog.cc b/gtk/DetailsDialog.cc index 472abb5c4..268dcd68a 100644 --- a/gtk/DetailsDialog.cc +++ b/gtk/DetailsDialog.cc @@ -45,7 +45,7 @@ public: TR_DISABLE_COPY_MOVE(Impl) - void set_torrents(std::vector const& torrent_ids); + void set_torrents(std::vector const& torrent_ids); private: Gtk::Widget* info_page_new(); @@ -84,7 +84,7 @@ private: void refreshPeerList(std::vector const& torrents); void refreshWebseedList(std::vector const& torrents); - int tracker_list_get_current_torrent_id() const; + tr_torrent_id_t tracker_list_get_current_torrent_id() const; tr_torrent* tracker_list_get_current_torrent() const; std::vector getTorrents() const; @@ -155,7 +155,7 @@ private: FileList* file_list_ = nullptr; Gtk::Label* file_label_ = nullptr; - std::vector ids_; + std::vector ids_; Glib::RefPtr const core_; sigc::connection periodic_refresh_tag_; @@ -2093,7 +2093,7 @@ public: add(key); } - Gtk::TreeModelColumn torrent_id; + Gtk::TreeModelColumn torrent_id; Gtk::TreeModelColumn text; Gtk::TreeModelColumn is_backup; Gtk::TreeModelColumn tracker_id; @@ -2118,34 +2118,27 @@ bool DetailsDialog::Impl::trackerVisibleFunc(Gtk::TreeModel::const_iterator cons return !iter->get_value(tracker_cols.is_backup); } -int DetailsDialog::Impl::tracker_list_get_current_torrent_id() const +tr_torrent_id_t DetailsDialog::Impl::tracker_list_get_current_torrent_id() const { - int torrent_id = -1; - - /* if there's only one torrent in the dialog, always use it */ + // if there's only one torrent in the dialog, always use it if (ids_.size() == 1) { - torrent_id = ids_.front(); + return ids_.front(); } - /* otherwise, use the selected tracker's torrent */ - if (torrent_id < 0) + // otherwise, use the selected tracker's torrent + auto const sel = tracker_view_->get_selection(); + if (auto const iter = sel->get_selected(); iter) { - auto const sel = tracker_view_->get_selection(); - - if (auto const iter = sel->get_selected(); iter) - { - torrent_id = iter->get_value(tracker_cols.torrent_id); - } + return iter->get_value(tracker_cols.torrent_id); } - return torrent_id; + return -1; } tr_torrent* DetailsDialog::Impl::tracker_list_get_current_torrent() const { - int const torrent_id = tracker_list_get_current_torrent_id(); - return core_->find_torrent(torrent_id); + return core_->find_torrent(tracker_list_get_current_torrent_id()); } namespace @@ -2254,7 +2247,7 @@ void DetailsDialog::Impl::refreshTracker(std::vector const& torrent } } - edit_trackers_button_->set_sensitive(tracker_list_get_current_torrent_id() >= 0); + edit_trackers_button_->set_sensitive(tracker_list_get_current_torrent_id() > 0); } void DetailsDialog::Impl::onScrapeToggled() @@ -2326,7 +2319,7 @@ void DetailsDialog::Impl::on_edit_trackers() if (tor != nullptr) { guint row; - int const torrent_id = tr_torrentId(tor); + auto const torrent_id = tr_torrentId(tor); auto d = std::make_shared( fmt::format(_("{torrent_name} - Edit Trackers"), fmt::arg("torrent_name", tr_torrentName(tor))), @@ -2663,15 +2656,15 @@ DetailsDialog::Impl::Impl(DetailsDialog& dialog, Glib::RefPtr const& co last_page_tag_ = n->signal_switch_page().connect([](Widget*, guint page) { DetailsDialog::Impl::last_page_ = page; }); } -void DetailsDialog::set_torrents(std::vector const& ids) +void DetailsDialog::set_torrents(std::vector const& ids) { impl_->set_torrents(ids); } -void DetailsDialog::Impl::set_torrents(std::vector const& ids) +void DetailsDialog::Impl::set_torrents(std::vector const& ids) { Glib::ustring title; - int const len = ids.size(); + auto const len = ids.size(); ids_ = ids; diff --git a/gtk/DetailsDialog.h b/gtk/DetailsDialog.h index 780e22965..96d7d00fa 100644 --- a/gtk/DetailsDialog.h +++ b/gtk/DetailsDialog.h @@ -24,7 +24,7 @@ public: static std::unique_ptr create(Gtk::Window& parent, Glib::RefPtr const& core); - void set_torrents(std::vector const& torrent_ids); + void set_torrents(std::vector const& torrent_ids); protected: DetailsDialog(Gtk::Window& parent, Glib::RefPtr const& core); diff --git a/gtk/Dialogs.cc b/gtk/Dialogs.cc index a33c7add8..1e3f58447 100644 --- a/gtk/Dialogs.cc +++ b/gtk/Dialogs.cc @@ -21,7 +21,7 @@ void gtr_confirm_remove( Gtk::Window& parent, Glib::RefPtr const& core, - std::vector const& torrent_ids, + std::vector const& torrent_ids, bool delete_files) { int connected = 0; diff --git a/gtk/Dialogs.h b/gtk/Dialogs.h index 7f7309fe0..701af57df 100644 --- a/gtk/Dialogs.h +++ b/gtk/Dialogs.h @@ -16,5 +16,5 @@ class Session; void gtr_confirm_remove( Gtk::Window& parent, Glib::RefPtr const& core, - std::vector const& torrent_ids, + std::vector const& torrent_ids, bool delete_files); diff --git a/gtk/FileList.cc b/gtk/FileList.cc index 50635295d..007229195 100644 --- a/gtk/FileList.cc +++ b/gtk/FileList.cc @@ -80,12 +80,12 @@ FileModelColumns const file_cols; class FileList::Impl { public: - Impl(FileList& widget, Glib::RefPtr const& core, int torrent_id); + Impl(FileList& widget, Glib::RefPtr const& core, tr_torrent_id_t tor_id); ~Impl(); TR_DISABLE_COPY_MOVE(Impl) - void set_torrent(int torrent_id); + void set_torrent(tr_torrent_id_t tor_id); private: void clearData(); @@ -110,7 +110,7 @@ private: // GtkWidget* top_ = nullptr; // == widget_ Gtk::TreeView* view_ = nullptr; Glib::RefPtr store_; - int torrent_id_ = 0; + tr_torrent_id_t torrent_id_ = {}; sigc::connection timeout_tag_; }; @@ -470,9 +470,9 @@ void buildTree(FileRowNode& node, build_data& build) } // namespace -void FileList::set_torrent(int torrent_id) +void FileList::set_torrent(tr_torrent_id_t tor_id) { - impl_->set_torrent(torrent_id); + impl_->set_torrent(tor_id); } struct PairHash @@ -484,14 +484,14 @@ struct PairHash } }; -void FileList::Impl::set_torrent(int torrentId) +void FileList::Impl::set_torrent(tr_torrent_id_t tor_id) { /* unset the old fields */ clearData(); /* instantiate the model */ store_ = Gtk::TreeStore::create(file_cols); - torrent_id_ = torrentId; + torrent_id_ = tor_id; /* populate the model */ if (torrent_id_ > 0) @@ -831,13 +831,13 @@ void FileList::Impl::cell_edited_callback(Glib::ustring const& path_string, Glib rename_data.release()); } -FileList::FileList(Glib::RefPtr const& core, int torrent_id) +FileList::FileList(Glib::RefPtr const& core, tr_torrent_id_t tor_id) : Gtk::ScrolledWindow() - , impl_(std::make_unique(*this, core, torrent_id)) + , impl_(std::make_unique(*this, core, tor_id)) { } -FileList::Impl::Impl(FileList& widget, Glib::RefPtr const& core, int torrent_id) +FileList::Impl::Impl(FileList& widget, Glib::RefPtr const& core, tr_torrent_id_t tor_id) : widget_(widget) , core_(core) { @@ -956,7 +956,7 @@ FileList::Impl::Impl(FileList& widget, Glib::RefPtr const& core, int to widget_.add(*view_); widget_.set_size_request(-1, 200); - set_torrent(torrent_id); + set_torrent(tor_id); } FileList::~FileList() = default; diff --git a/gtk/FileList.h b/gtk/FileList.h index efbb12d56..ca0e28138 100644 --- a/gtk/FileList.h +++ b/gtk/FileList.h @@ -16,13 +16,13 @@ class Session; class FileList : public Gtk::ScrolledWindow { public: - FileList(Glib::RefPtr const& core, int torrent_id); + FileList(Glib::RefPtr const& core, tr_torrent_id_t torrent_id); ~FileList() override; TR_DISABLE_COPY_MOVE(FileList) void clear(); - void set_torrent(int torrent_id); + void set_torrent(tr_torrent_id_t torrent_id); private: class Impl; diff --git a/gtk/Notify.cc b/gtk/Notify.cc index 94229d99f..edcfc4b65 100644 --- a/gtk/Notify.cc +++ b/gtk/Notify.cc @@ -30,7 +30,7 @@ auto const NotificationsDbusCoreInterface = Glib::ustring("org.freedesktop.Notif struct TrNotification { Glib::RefPtr core; - int torrent_id = 0; + tr_torrent_id_t torrent_id = {}; }; Glib::RefPtr proxy; @@ -162,7 +162,7 @@ void notify_callback(Glib::RefPtr& res, TrNotification const& } // namespace -void gtr_notify_torrent_completed(Glib::RefPtr const& core, int torrent_id) +void gtr_notify_torrent_completed(Glib::RefPtr const& core, tr_torrent_id_t tor_id) { if (gtr_pref_flag_get(TR_KEY_torrent_complete_sound_enabled)) { @@ -184,9 +184,9 @@ void gtr_notify_torrent_completed(Glib::RefPtr const& core, int torrent g_return_if_fail(proxy != nullptr); - auto const* const tor = core->find_torrent(torrent_id); + auto const* const tor = core->find_torrent(tor_id); - auto const n = TrNotification{ core, torrent_id }; + auto const n = TrNotification{ core, tor_id }; std::vector actions; if (server_supports_actions) @@ -220,7 +220,7 @@ void gtr_notify_torrent_completed(Glib::RefPtr const& core, int torrent -1)); } -void gtr_notify_torrent_added(Glib::RefPtr const& core, int torrent_id) +void gtr_notify_torrent_added(Glib::RefPtr const& core, tr_torrent_id_t tor_id) { g_return_if_fail(proxy != nullptr); @@ -229,7 +229,7 @@ void gtr_notify_torrent_added(Glib::RefPtr const& core, int torrent_id) return; } - auto const* const tor = core->find_torrent(torrent_id); + auto const* const tor = core->find_torrent(tor_id); std::vector actions; if (server_supports_actions) @@ -238,7 +238,7 @@ void gtr_notify_torrent_added(Glib::RefPtr const& core, int torrent_id) actions.emplace_back(_("Start Now")); } - auto const n = TrNotification{ core, torrent_id }; + auto const n = TrNotification{ core, tor_id }; proxy->call( "Notify", diff --git a/gtk/Notify.h b/gtk/Notify.h index 0e91cb04f..1dd1b3906 100644 --- a/gtk/Notify.h +++ b/gtk/Notify.h @@ -7,10 +7,12 @@ #include +#include + class Session; void gtr_notify_init(); -void gtr_notify_torrent_added(Glib::RefPtr const& core, int torrent_id); +void gtr_notify_torrent_added(Glib::RefPtr const& core, tr_torrent_id_t tor_id); -void gtr_notify_torrent_completed(Glib::RefPtr const& core, int torrent_id); +void gtr_notify_torrent_completed(Glib::RefPtr const& core, tr_torrent_id_t tor_id); diff --git a/gtk/RelocateDialog.cc b/gtk/RelocateDialog.cc index a9d98dd3a..659008a5f 100644 --- a/gtk/RelocateDialog.cc +++ b/gtk/RelocateDialog.cc @@ -30,7 +30,7 @@ std::string previousLocation; class RelocateDialog::Impl { public: - Impl(RelocateDialog& dialog, Glib::RefPtr const& core, std::vector const& torrent_ids); + Impl(RelocateDialog& dialog, Glib::RefPtr const& core, std::vector const& torrent_ids); ~Impl(); TR_DISABLE_COPY_MOVE(Impl) @@ -44,7 +44,7 @@ private: private: RelocateDialog& dialog_; Glib::RefPtr const core_; - std::vector torrent_ids_; + std::vector torrent_ids_; int done_ = 0; bool do_move_ = false; @@ -141,12 +141,15 @@ void RelocateDialog::Impl::onResponse(int response) std::unique_ptr RelocateDialog::create( Gtk::Window& parent, Glib::RefPtr const& core, - std::vector const& torrent_ids) + std::vector const& torrent_ids) { return std::unique_ptr(new RelocateDialog(parent, core, torrent_ids)); } -RelocateDialog::RelocateDialog(Gtk::Window& parent, Glib::RefPtr const& core, std::vector const& torrent_ids) +RelocateDialog::RelocateDialog( + Gtk::Window& parent, + Glib::RefPtr const& core, + std::vector const& torrent_ids) : Gtk::Dialog(_("Set Torrent Location"), parent, true) , impl_(std::make_unique(*this, core, torrent_ids)) { @@ -154,7 +157,10 @@ RelocateDialog::RelocateDialog(Gtk::Window& parent, Glib::RefPtr const& RelocateDialog::~RelocateDialog() = default; -RelocateDialog::Impl::Impl(RelocateDialog& dialog, Glib::RefPtr const& core, std::vector const& torrent_ids) +RelocateDialog::Impl::Impl( + RelocateDialog& dialog, + Glib::RefPtr const& core, + std::vector const& torrent_ids) : dialog_(dialog) , core_(core) , torrent_ids_(torrent_ids) diff --git a/gtk/RelocateDialog.h b/gtk/RelocateDialog.h index eb0cfdf88..519d5cf42 100644 --- a/gtk/RelocateDialog.h +++ b/gtk/RelocateDialog.h @@ -24,10 +24,10 @@ public: static std::unique_ptr create( Gtk::Window& parent, Glib::RefPtr const& core, - std::vector const& torrent_ids); + std::vector const& torrent_ids); protected: - RelocateDialog(Gtk::Window& parent, Glib::RefPtr const& core, std::vector const& torrent_ids); + RelocateDialog(Gtk::Window& parent, Glib::RefPtr const& core, std::vector const& torrent_ids); private: class Impl; diff --git a/gtk/Session.cc b/gtk/Session.cc index 07f1d44a0..87bfc0f60 100644 --- a/gtk/Session.cc +++ b/gtk/Session.cc @@ -882,10 +882,10 @@ Glib::ustring get_collated_name(tr_torrent const* tor) struct metadata_callback_data { Session* core; - int torrent_id; + tr_torrent_id_t torrent_id; }; -Gtk::TreeModel::iterator find_row_from_torrent_id(Glib::RefPtr const& model, int id) +Gtk::TreeModel::iterator find_row_from_torrent_id(Glib::RefPtr const& model, tr_torrent_id_t id) { for (auto const& row : model->children()) { @@ -1234,7 +1234,7 @@ void Session::Impl::torrents_added() signal_add_error.emit(ERR_NO_MORE_TORRENTS, {}); } -void Session::torrent_changed(int id) +void Session::torrent_changed(tr_torrent_id_t id) { auto const model = impl_->get_raw_model(); @@ -1244,7 +1244,7 @@ void Session::torrent_changed(int id) } } -void Session::remove_torrent(int id, bool delete_local_data) +void Session::remove_torrent(tr_torrent_id_t id, bool delete_local_data) { auto* tor = find_torrent(id); @@ -1390,7 +1390,7 @@ void Session::update() impl_->update(); } -void Session::start_now(int id) +void Session::start_now(tr_torrent_id_t id) { tr_variant top; tr_variantInitDict(&top, 2); @@ -1752,7 +1752,7 @@ size_t Session::Impl::get_active_torrent_count() const return activeCount; } -tr_torrent* Session::find_torrent(int id) const +tr_torrent* Session::find_torrent(tr_torrent_id_t id) const { tr_torrent* tor = nullptr; @@ -1764,7 +1764,7 @@ tr_torrent* Session::find_torrent(int id) const return tor; } -void Session::open_folder(int torrent_id) +void Session::open_folder(tr_torrent_id_t torrent_id) { auto const* tor = find_torrent(torrent_id); diff --git a/gtk/Session.h b/gtk/Session.h index f59386383..96ecfe1c5 100644 --- a/gtk/Session.h +++ b/gtk/Session.h @@ -47,7 +47,7 @@ public: size_t get_torrent_count() const; - tr_torrent* find_torrent(int id) const; + tr_torrent* find_torrent(tr_torrent_id_t id) const; /****** ******* @@ -84,14 +84,14 @@ public: */ void torrents_added(); - void torrent_changed(int id); + void torrent_changed(tr_torrent_id_t id); /****** ******* ******/ /* remove a torrent */ - void remove_torrent(int id, bool delete_files); + void remove_torrent(tr_torrent_id_t id, bool delete_files); /* update the model with current torrent status */ void update(); @@ -99,7 +99,7 @@ public: /** * Attempts to start a torrent immediately. */ - void start_now(int id); + void start_now(tr_torrent_id_t id); /** *** Set a preference value, save the prefs file, and emit the "prefs-changed" signal @@ -120,7 +120,7 @@ public: void exec(tr_variant const* benc); - void open_folder(int torrent_id); + void open_folder(tr_torrent_id_t torrent_id); sigc::signal& signal_add_error(); sigc::signal& signal_add_prompt(); @@ -148,7 +148,7 @@ public: Gtk::TreeModelColumn name_collated; Gtk::TreeModelColumn torrent; - Gtk::TreeModelColumn torrent_id; + Gtk::TreeModelColumn torrent_id; Gtk::TreeModelColumn speed_up; Gtk::TreeModelColumn speed_down; Gtk::TreeModelColumn active_peers_up; diff --git a/libtransmission/cache.cc b/libtransmission/cache.cc index a0a0360be..c2ffd1508 100644 --- a/libtransmission/cache.cc +++ b/libtransmission/cache.cc @@ -24,7 +24,7 @@ Cache::Key Cache::makeKey(tr_torrent const* torrent, tr_block_info::Location loc) noexcept { - return std::make_pair(torrent->uniqueId, loc.block); + return std::make_pair(torrent->id(), loc.block); } std::pair Cache::findContiguous(CIter const begin, CIter const end, CIter const iter) noexcept @@ -213,7 +213,7 @@ int Cache::flushSpan(CIter const begin, CIter const end) int Cache::flushFile(tr_torrent* torrent, tr_file_index_t i) { auto const compare = CompareCacheBlockByKey{}; - auto const tor_id = torrent->uniqueId; + auto const tor_id = torrent->id(); auto const [block_begin, block_end] = tr_torGetFileBlockSpan(torrent, i); return flushSpan( @@ -224,7 +224,7 @@ int Cache::flushFile(tr_torrent* torrent, tr_file_index_t i) int Cache::flushTorrent(tr_torrent* torrent) { auto const compare = CompareCacheBlockByKey{}; - auto const tor_id = torrent->uniqueId; + auto const tor_id = torrent->id(); return flushSpan( std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, 0), compare), diff --git a/libtransmission/inout.cc b/libtransmission/inout.cc index 2d5d6f2b8..6e25a55a3 100644 --- a/libtransmission/inout.cc +++ b/libtransmission/inout.cc @@ -120,7 +120,7 @@ int readOrWriteBytes( **** Find the fd ***/ - auto fd = session->openFiles().get(tor->uniqueId, file_index, do_write); + auto fd = session->openFiles().get(tor->id(), file_index, do_write); auto filename = tr_pathbuf{}; if (!fd && !getFilename(filename, tor, file_index, io_mode)) { @@ -132,7 +132,7 @@ int readOrWriteBytes( // open (and maybe create) the file auto const prealloc = (!do_write || !tor->fileIsWanted(file_index)) ? TR_PREALLOCATE_NONE : tor->session->preallocationMode; - fd = session->openFiles().get(tor->uniqueId, file_index, do_write, filename, prealloc, file_size); + fd = session->openFiles().get(tor->id(), file_index, do_write, filename, prealloc, file_size); if (fd && do_write) { // make a note that we just created a file diff --git a/libtransmission/session.cc b/libtransmission/session.cc index 271742fd4..8529496f6 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -2927,11 +2927,11 @@ static int bandwidthGroupWrite(tr_session const* session, std::string_view confi void tr_session::closeTorrentFiles(tr_torrent* tor) noexcept { this->cache->flushTorrent(tor); - openFiles().closeTorrent(tor->uniqueId); + openFiles().closeTorrent(tor->id()); } void tr_session::closeTorrentFile(tr_torrent* tor, tr_file_index_t file_num) noexcept { this->cache->flushFile(tor, file_num); - openFiles().closeFile(tor->uniqueId, file_num); + openFiles().closeFile(tor->id(), file_num); } diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 68a50bf25..7dc228261 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -73,9 +73,9 @@ uint64_t tr_torrentTotalSize(tr_torrent const* tor) return tor->totalSize(); } -int tr_torrentId(tr_torrent const* tor) +tr_torrent_id_t tr_torrentId(tr_torrent const* tor) { - return tor != nullptr ? tor->uniqueId : -1; + return tor != nullptr ? tor->id() : -1; } tr_torrent* tr_torrentFindFromId(tr_session* session, int id) @@ -715,7 +715,7 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor) auto const& labels = tr_ctorGetLabels(ctor); tor->setLabels(labels); - tor->uniqueId = session->torrents().add(tor); + tor->unique_id_ = session->torrents().add(tor); tr_peerMgrAddTorrent(session->peerMgr, tor); @@ -1028,7 +1028,7 @@ tr_stat const* tr_torrentStat(tr_torrent* tor) } tr_stat* const s = &tor->stats; - s->id = tor->uniqueId; + s->id = tor->id(); s->activity = tr_torrentGetActivity(tor); s->error = tor->error; s->queuePosition = tor->queuePosition; @@ -1915,10 +1915,10 @@ void tr_torrent::setLabels(std::vector const& new_labels) for (auto label : new_labels) { - if (std::find(std::begin(this->labels), std::end(this->labels), label) == std::end(this->labels)) - { - this->labels.push_back(label); - } + if (std::find(std::begin(this->labels), std::end(this->labels), label) == std::end(this->labels)) + { + this->labels.push_back(label); + } } this->labels.shrink_to_fit(); this->setDirty(); diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 948f07c00..253ebbf23 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -579,6 +579,11 @@ public: return {}; } + [[nodiscard]] constexpr auto id() const noexcept + { + return unique_id_; + } + void setDateActive(time_t t); void setLabels(std::vector const& new_labels); @@ -712,7 +717,7 @@ public: int queuePosition = 0; - int uniqueId = 0; + tr_torrent_id_t unique_id_ = 0; tr_completeness completeness = TR_LEECH; diff --git a/libtransmission/torrents.cc b/libtransmission/torrents.cc index d54fc4f65..c8f7f5e5a 100644 --- a/libtransmission/torrents.cc +++ b/libtransmission/torrents.cc @@ -42,7 +42,7 @@ struct CompareTorrentByHash } // namespace -tr_torrent* tr_torrents::get(int id) +tr_torrent* tr_torrents::get(tr_torrent_id_t id) { TR_ASSERT(0 < id); TR_ASSERT(static_cast(id) < std::size(by_id_)); @@ -52,7 +52,7 @@ tr_torrent* tr_torrents::get(int id) } auto* const tor = by_id_.at(id); - TR_ASSERT(tor == nullptr || tor->uniqueId == id); + TR_ASSERT(tor == nullptr || tor->id() == id); TR_ASSERT( std::count_if(std::begin(removed_), std::end(removed_), [&id](auto const& removed) { return id == removed.first; }) == (tor == nullptr ? 1 : 0)); @@ -77,9 +77,9 @@ tr_torrent const* tr_torrents::get(tr_sha1_digest_t const& hash) const return begin == end ? nullptr : *begin; } -int tr_torrents::add(tr_torrent* tor) +tr_torrent_id_t tr_torrents::add(tr_torrent* tor) { - auto const id = static_cast(std::size(by_id_)); + auto const id = static_cast(std::size(by_id_)); by_id_.push_back(tor); by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}), tor); return id; @@ -88,17 +88,17 @@ int tr_torrents::add(tr_torrent* tor) void tr_torrents::remove(tr_torrent const* tor, time_t timestamp) { TR_ASSERT(tor != nullptr); - TR_ASSERT(get(tor->uniqueId) == tor); + TR_ASSERT(get(tor->id()) == tor); - by_id_[tor->uniqueId] = nullptr; + by_id_[tor->id()] = nullptr; auto const [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}); by_hash_.erase(begin, end); - removed_.emplace_back(tor->uniqueId, timestamp); + removed_.emplace_back(tor->id(), timestamp); } -std::vector tr_torrents::removedSince(time_t timestamp) const +std::vector tr_torrents::removedSince(time_t timestamp) const { - auto ids = std::set{}; + auto ids = std::set{}; for (auto const& [id, removed_at] : removed_) { diff --git a/libtransmission/torrents.h b/libtransmission/torrents.h index 52b6ff734..36a808c79 100644 --- a/libtransmission/torrents.h +++ b/libtransmission/torrents.h @@ -25,12 +25,12 @@ class tr_torrents { public: // returns a fast lookup id for `tor` - [[nodiscard]] int add(tr_torrent* tor); + [[nodiscard]] tr_torrent_id_t add(tr_torrent* tor); void remove(tr_torrent const* tor, time_t current_time); // O(1) - [[nodiscard]] tr_torrent* get(int id); + [[nodiscard]] tr_torrent* get(tr_torrent_id_t id); // O(log n) [[nodiscard]] tr_torrent const* get(tr_sha1_digest_t const& hash) const; @@ -57,7 +57,7 @@ public: return get(key) != nullptr; } - [[nodiscard]] std::vector removedSince(time_t) const; + [[nodiscard]] std::vector removedSince(time_t) const; [[nodiscard]] auto cbegin() const noexcept { @@ -100,7 +100,7 @@ public: private: std::vector by_hash_; - // This is a lookup table where by_id_[id]->uniqueId == id. + // This is a lookup table where by_id_[id]->id() == id. // There is a small tradeoff here -- lookup is O(1) at the cost // of a wasted slot in the lookup table whenever a torrent is // removed. This improves speed for all use cases at the cost of @@ -114,5 +114,5 @@ private: // may be testing for >0 as a validity check. std::vector by_id_{ nullptr }; - std::vector> removed_; + std::vector> removed_; }; diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index ac8873f70..7c4932c0f 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -985,7 +985,7 @@ uint64_t tr_torrentGetBytesLeftToAllocate(tr_torrent const* torrent); * IDs are fast lookup keys, but are not persistent between sessions. * If you need that, use tr_torrentView().hash_string. */ -int tr_torrentId(tr_torrent const* torrent); +tr_torrent_id_t tr_torrentId(tr_torrent const* torrent); tr_torrent* tr_torrentFindFromId(tr_session* session, int id); @@ -1346,7 +1346,7 @@ struct tr_tracker_view int seederCount; // number of seeders the tracker knows of, or -1 if unknown int tier; // which tier this tracker is in - int id; // unique transmission-generated ID for use in libtransmission API + tr_torrent_id_t id; // unique transmission-generated ID for use in libtransmission API tr_tracker_state announceState; // whether we're announcing, waiting to announce, etc. tr_tracker_state scrapeState; // whether we're scraping, waiting to scrape, etc. @@ -1612,7 +1612,7 @@ struct tr_stat /** The torrent's unique Id. @see tr_torrentId() */ - int id; + tr_torrent_id_t id; /** Number of seconds since the last activity (or since started). -1 if activity is not seeding or downloading. */ diff --git a/libtransmission/webseed.cc b/libtransmission/webseed.cc index b548d2404..7092cd3b6 100644 --- a/libtransmission/webseed.cc +++ b/libtransmission/webseed.cc @@ -242,7 +242,7 @@ public: publish(&e); } - int const torrent_id; + tr_torrent_id_t const torrent_id; std::string const base_url; tr_peer_callback const callback; void* const callback_data; @@ -294,7 +294,11 @@ private: std::shared_ptr const content_{ evbuffer_new(), evbuffer_free }; public: - write_block_data(tr_session* session_in, int torrent_id_in, tr_webseed* webseed_in, tr_block_info::Location loc_in) + write_block_data( + tr_session* session_in, + tr_torrent_id_t torrent_id_in, + tr_webseed* webseed_in, + tr_block_info::Location loc_in) : session{ session_in } , torrent_id{ torrent_id_in } , webseed{ webseed_in } @@ -326,7 +330,7 @@ public: private: tr_session* const session; - int const torrent_id; + tr_torrent_id_t const torrent_id; tr_webseed* const webseed; tr_block_info::Location const loc; }; @@ -358,7 +362,7 @@ void useFetchedBlocks(tr_webseed_task* task) } else { - auto* const data = new write_block_data{ session, tor->uniqueId, webseed, task->loc }; + auto* const data = new write_block_data{ session, tor->id(), webseed, task->loc }; evbuffer_remove_buffer(task->content(), data->content(), block_size); tr_runInEventThread(session, &write_block_data::write_block_func, data); } @@ -504,7 +508,7 @@ void task_request_next_chunk(tr_webseed_task* task) makeUrl(webseed, tor->fileSubpath(file_index), std::back_inserter(url)); auto options = tr_web::FetchOptions{ url.sv(), onPartialDataFetched, task }; options.range = fmt::format(FMT_STRING("{:d}-{:d}"), file_offset, file_offset + this_chunk - 1); - options.speed_limit_tag = tor->uniqueId; + options.speed_limit_tag = tor->id(); options.buffer = task->content(); tor->session->web->fetch(std::move(options)); } diff --git a/qt/DetailsDialog.cc b/qt/DetailsDialog.cc index 2e910a314..0a0930966 100644 --- a/qt/DetailsDialog.cc +++ b/qt/DetailsDialog.cc @@ -334,9 +334,9 @@ void DetailsDialog::refreshModel() void DetailsDialog::onTorrentsEdited(torrent_ids_t const& ids) { // std::set_intersection requires sorted inputs - auto a = std::vector{ ids.begin(), ids.end() }; + auto a = std::vector{ ids.begin(), ids.end() }; std::sort(std::begin(a), std::end(a)); - auto b = std::vector{ ids_.begin(), ids_.end() }; + auto b = std::vector{ ids_.begin(), ids_.end() }; std::sort(std::begin(b), std::end(b)); // are any of the edited torrents on display here? diff --git a/qt/Torrent.h b/qt/Torrent.h index bb87797b2..dd26020d9 100644 --- a/qt/Torrent.h +++ b/qt/Torrent.h @@ -627,7 +627,7 @@ public: fields_t update(tr_quark const* keys, tr_variant const* const* values, size_t n); private: - int const id_; + tr_torrent_id_t const id_; bool download_limited_ = {}; bool honors_session_limits_ = {}; diff --git a/qt/Typedefs.h b/qt/Typedefs.h index 58a0b2f79..c142b37d7 100644 --- a/qt/Typedefs.h +++ b/qt/Typedefs.h @@ -2,4 +2,6 @@ #include -using torrent_ids_t = std::unordered_set; +#include + +using torrent_ids_t = std::unordered_set; diff --git a/tests/libtransmission/torrents-test.cc b/tests/libtransmission/torrents-test.cc index f8cb947ab..940e0af3c 100644 --- a/tests/libtransmission/torrents-test.cc +++ b/tests/libtransmission/torrents-test.cc @@ -33,7 +33,7 @@ TEST_F(TorrentsTest, simpleTests) auto const id = torrents.add(tor); EXPECT_GT(id, 0); - tor->uniqueId = id; + tor->unique_id_ = id; EXPECT_TRUE(std::empty(torrents.removedSince(0))); EXPECT_FALSE(std::empty(torrents)); @@ -68,8 +68,8 @@ TEST_F(TorrentsTest, rangedLoop) auto tm = tr_torrent_metainfo{}; EXPECT_TRUE(tm.parseTorrentFile(path)); auto* const tor = new tr_torrent{ std::move(tm) }; - tor->uniqueId = torrents.add(tor); - EXPECT_EQ(tor, torrents.get(tor->uniqueId)); + tor->unique_id_ = torrents.add(tor); + EXPECT_EQ(tor, torrents.get(tor->id())); torrents_set.insert(tor); } @@ -99,7 +99,7 @@ TEST_F(TorrentsTest, removedSince) auto const path = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, '/', name }; auto tm = tr_torrent_metainfo{}; auto* const tor = new tr_torrent{ std::move(tm) }; - tor->uniqueId = torrents.add(tor); + tor->unique_id_ = torrents.add(tor); torrents_v.push_back(tor); } @@ -108,18 +108,18 @@ TEST_F(TorrentsTest, removedSince) for (size_t i = 0; i < 4; ++i) { auto* const tor = torrents_v[i]; - EXPECT_EQ(tor, torrents.get(tor->uniqueId)); + EXPECT_EQ(tor, torrents.get(tor->id())); torrents.remove(torrents_v[i], TimeRemoved[i]); - EXPECT_EQ(nullptr, torrents.get(tor->uniqueId)); + EXPECT_EQ(nullptr, torrents.get(tor->id())); } - auto remove = std::vector{}; - remove = { torrents_v[3]->uniqueId }; + auto remove = std::vector{}; + remove = { torrents_v[3]->id() }; EXPECT_EQ(remove, torrents.removedSince(300)); EXPECT_EQ(remove, torrents.removedSince(201)); - remove = { torrents_v[1]->uniqueId, torrents_v[2]->uniqueId, torrents_v[3]->uniqueId }; + remove = { torrents_v[1]->id(), torrents_v[2]->id(), torrents_v[3]->id() }; EXPECT_EQ(remove, torrents.removedSince(200)); - remove = { torrents_v[0]->uniqueId, torrents_v[1]->uniqueId, torrents_v[2]->uniqueId, torrents_v[3]->uniqueId }; + remove = { torrents_v[0]->id(), torrents_v[1]->id(), torrents_v[2]->id(), torrents_v[3]->id() }; EXPECT_EQ(remove, torrents.removedSince(50)); std::for_each(std::begin(torrents_v), std::end(torrents_v), [](auto* tor) { delete tor; });