diff --git a/examples/bt-get.cpp b/examples/bt-get.cpp index 0029a81bf..53e223f55 100644 --- a/examples/bt-get.cpp +++ b/examples/bt-get.cpp @@ -51,7 +51,7 @@ int main(int argc, char const* argv[]) lt::add_torrent_params atp; atp.url = argv[1]; atp.save_path = "."; // save in current dir - lt::torrent_handle h = ses.add_torrent(atp); + lt::torrent_handle h = ses.add_torrent(std::move(atp)); for (;;) { std::vector alerts; diff --git a/examples/bt-get2.cpp b/examples/bt-get2.cpp index 8474d536f..b0ca179d1 100644 --- a/examples/bt-get2.cpp +++ b/examples/bt-get2.cpp @@ -89,7 +89,7 @@ int main(int argc, char const* argv[]) lt::add_torrent_params atp = lt::read_resume_data(&buf[0], int(buf.size()), ec); atp.url = argv[1]; atp.save_path = "."; // save in current dir - ses.async_add_torrent(atp); + ses.async_add_torrent(std::move(atp)); // this is the handle we'll set once we get the notification of it being // added diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 46c7e0059..88409db67 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -568,7 +568,7 @@ void add_magnet(lt::session& ses, lt::string_view uri) p.storage_mode = static_cast(allocation_mode); std::printf("adding magnet: %s\n", uri.to_string().c_str()); - ses.async_add_torrent(p); + ses.async_add_torrent(std::move(p)); } // return false on failure @@ -608,7 +608,7 @@ bool add_torrent(libtorrent::session& ses, std::string torrent) p.storage_mode = (storage_mode_t)allocation_mode; p.flags &= ~add_torrent_params::flag_duplicate_is_error; p.userdata = static_cast(new std::string(torrent)); - ses.async_add_torrent(p); + ses.async_add_torrent(std::move(p)); return true; } @@ -913,7 +913,7 @@ bool handle_alert(torrent_view& view, session_view& ses_view } else if (state_update_alert* p = alert_cast(a)) { - view.update_torrents(p->status); + view.update_torrents(std::move(p->status)); return true; } return false; @@ -1274,7 +1274,7 @@ MAGNETURL is a magnet link continue; } - ses.async_add_torrent(p); + ses.async_add_torrent(std::move(p)); } } diff --git a/examples/torrent_view.cpp b/examples/torrent_view.cpp index 0c7759a9c..337c1e8d3 100644 --- a/examples/torrent_view.cpp +++ b/examples/torrent_view.cpp @@ -96,18 +96,17 @@ lt::torrent_handle torrent_view::get_active_handle() const return m_filtered_handles[m_active_torrent]->handle; } -void torrent_view::update_torrents(std::vector const& st) +void torrent_view::update_torrents(std::vector st) { std::set updates; bool need_filter_update = false; - for (std::vector::const_iterator i = st.begin(); - i != st.end(); ++i) + for (lt::torrent_status& t : st) { - auto j = m_all_handles.find(*i); + auto j = m_all_handles.find(t); // add new entries here if (j == m_all_handles.end()) { - j = m_all_handles.insert(*i).first; + j = m_all_handles.insert(std::move(t)).first; if (show_torrent(*j)) { m_filtered_handles.push_back(&*j); @@ -116,12 +115,12 @@ void torrent_view::update_torrents(std::vector const& st) } else { - bool prev_show = show_torrent(*j); - ((lt::torrent_status&)*j) = *i; + bool const prev_show = show_torrent(*j); + const_cast(*j) = std::move(t); if (prev_show != show_torrent(*j)) need_filter_update = true; else - updates.insert(i->handle); + updates.insert(j->handle); } } if (need_filter_update) @@ -132,19 +131,16 @@ void torrent_view::update_torrents(std::vector const& st) else { int torrent_index = 0; - for (std::vector::iterator i - = m_filtered_handles.begin(); - i != m_filtered_handles.end(); ++torrent_index) + for (auto i = m_filtered_handles.begin(); + i != m_filtered_handles.end(); ++torrent_index, ++i) { if (torrent_index < m_scroll_position || torrent_index >= m_scroll_position + m_height - header_size) { - ++i; continue; } lt::torrent_status const& s = **i; - ++i; if (!s.handle.is_valid()) continue; diff --git a/examples/torrent_view.hpp b/examples/torrent_view.hpp index a0814f423..96165cacd 100644 --- a/examples/torrent_view.hpp +++ b/examples/torrent_view.hpp @@ -36,7 +36,7 @@ struct torrent_view lt::torrent_status const& get_active_torrent() const; lt::torrent_handle get_active_handle() const; - void update_torrents(std::vector const& st); + void update_torrents(std::vector st); int height() const; diff --git a/include/libtorrent/add_torrent_params.hpp b/include/libtorrent/add_torrent_params.hpp index 7a9d881c3..45704c961 100644 --- a/include/libtorrent/add_torrent_params.hpp +++ b/include/libtorrent/add_torrent_params.hpp @@ -87,6 +87,10 @@ namespace libtorrent // data for the torrent. For more information, see the ``storage`` field. explicit add_torrent_params(storage_constructor_type sc = default_storage_constructor) : storage(sc) {} + add_torrent_params(add_torrent_params&&) = default; + add_torrent_params& operator=(add_torrent_params&&) = default; + add_torrent_params(add_torrent_params const&) = default; + add_torrent_params& operator=(add_torrent_params const&) = default; // values for the ``flags`` field enum flags_t : std::uint64_t diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 439902008..19ba69c2c 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -474,7 +474,7 @@ namespace libtorrent std::shared_ptr const& torrent_ptr, void* userdata); #endif - torrent_handle add_torrent(add_torrent_params const&, error_code& ec); + torrent_handle add_torrent(add_torrent_params, error_code& ec); // second return value is true if the torrent was added and false if an // existing one was found. std::pair, bool> diff --git a/include/libtorrent/magnet_uri.hpp b/include/libtorrent/magnet_uri.hpp index 5fb161bd7..308eff5c1 100644 --- a/include/libtorrent/magnet_uri.hpp +++ b/include/libtorrent/magnet_uri.hpp @@ -65,13 +65,13 @@ namespace libtorrent // deprecated in 0.16. Instead, pass in the magnet link as add_torrent_params::url TORRENT_DEPRECATED torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri - , add_torrent_params const& p); + , add_torrent_params p); #endif // deprecated in 0.16. Instead, pass in the magnet link as add_torrent_params::url TORRENT_DEPRECATED torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri - , add_torrent_params const& p, error_code& ec); + , add_torrent_params p, error_code& ec); #endif diff --git a/include/libtorrent/session_handle.hpp b/include/libtorrent/session_handle.hpp index cae8398c9..54281e296 100644 --- a/include/libtorrent/session_handle.hpp +++ b/include/libtorrent/session_handle.hpp @@ -232,7 +232,7 @@ namespace libtorrent torrent_handle add_torrent(add_torrent_params const& params); #endif torrent_handle add_torrent(add_torrent_params const& params, error_code& ec); - void async_add_torrent(add_torrent_params const& params); + void async_add_torrent(add_torrent_params params); #ifndef BOOST_NO_EXCEPTIONS #ifndef TORRENT_NO_DEPRECATE diff --git a/simulation/test_pause.cpp b/simulation/test_pause.cpp index 6ddf273c9..6d91038d1 100644 --- a/simulation/test_pause.cpp +++ b/simulation/test_pause.cpp @@ -80,7 +80,7 @@ void run_test(Setup const& setup, Torrent const& torrent lt::add_torrent_params params = create_torrent(0, false); params.flags &= ~lt::add_torrent_params::flag_auto_managed; params.flags &= ~lt::add_torrent_params::flag_paused; - ses->async_add_torrent(params); + ses->async_add_torrent(std::move(params)); lt::torrent_handle h; print_alerts(*ses, [&](lt::session& ses, lt::alert const* a) { diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index e34c9a428..1152664ed 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -113,14 +113,14 @@ namespace libtorrent { parse_magnet_uri(uri, p, ec); if (ec) return torrent_handle(); - return ses.add_torrent(p, ec); + return ses.add_torrent(std::move(p), ec); } } torrent_handle add_magnet_uri(session& ses, std::string const& uri - , add_torrent_params const& p, error_code& ec) + , add_torrent_params p, error_code& ec) { - return add_magnet_uri_deprecated(ses, uri, p, ec); + return add_magnet_uri_deprecated(ses, uri, std::move(p), ec); } #ifndef BOOST_NO_EXCEPTIONS @@ -153,14 +153,14 @@ namespace libtorrent if (btih.size() == 40 + 9) aux::from_hex({&btih[9], 40}, params.info_hash.data()); else params.info_hash.assign(base32decode(btih.substr(9)).c_str()); - return ses.add_torrent(params); + return ses.add_torrent(std::move(params)); } torrent_handle add_magnet_uri(session& ses, std::string const& uri - , add_torrent_params const& p) + , add_torrent_params p) { error_code ec; - torrent_handle ret = add_magnet_uri_deprecated(ses, uri, p, ec); + torrent_handle ret = add_magnet_uri_deprecated(ses, uri, std::move(p), ec); if (ec) aux::throw_ex(ec); return ret; } diff --git a/src/session_handle.cpp b/src/session_handle.cpp index 01170e07d..668cac3eb 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -342,11 +342,14 @@ namespace libtorrent return sync_call_ret(&session_impl::add_torrent, p, ecr); } - void session_handle::async_add_torrent(add_torrent_params const& params) + void session_handle::async_add_torrent(add_torrent_params params) { TORRENT_ASSERT_PRECOND(!params.save_path.empty()); - add_torrent_params* p = new add_torrent_params(params); + // we cannot capture a unique_ptr into a lambda in c++11, so we use a raw + // pointer for now. async_call uses a lambda expression to post the call + // to the main thread + add_torrent_params* p = new add_torrent_params(std::move(params)); p->save_path = complete(p->save_path); #ifndef TORRENT_NO_DEPRECATE diff --git a/src/session_impl.cpp b/src/session_impl.cpp index e98a602a1..f823ddd1c 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4619,7 +4619,7 @@ namespace aux { #endif error_code ec; - add_torrent(*params, ec); + add_torrent(std::move(*params), ec); } #ifndef TORRENT_NO_DEPRECATE @@ -4636,7 +4636,7 @@ namespace aux { TORRENT_ASSERT(params->ti->is_valid()); TORRENT_ASSERT(params->ti->num_files() > 0); params->url.clear(); - add_torrent(*params, ec); + add_torrent(std::move(*params), ec); } #endif @@ -4653,11 +4653,10 @@ namespace aux { } #endif - torrent_handle session_impl::add_torrent(add_torrent_params const& p + torrent_handle session_impl::add_torrent(add_torrent_params params , error_code& ec) { // params is updated by add_torrent_impl() - add_torrent_params params = p; std::shared_ptr torrent_ptr; // in case there's an error, make sure to abort the torrent before leaving @@ -4665,6 +4664,8 @@ namespace aux { auto abort_torrent = aux::scope_end([&]{ if (torrent_ptr) torrent_ptr->abort(); }); bool added; + // TODO: 3 perhaps params could be moved into the torrent object, instead + // of it being copied by the torrent constructor std::tie(torrent_ptr, added) = add_torrent_impl(params, ec); torrent_handle const handle(torrent_ptr); diff --git a/test/test_session.cpp b/test/test_session.cpp index c66ef3836..ef253ae18 100644 --- a/test/test_session.cpp +++ b/test/test_session.cpp @@ -157,7 +157,7 @@ TORRENT_TEST(load_empty_file) atp.ti = std::make_shared("", 0, std::ref(ignore_errors)); atp.save_path = "."; error_code ec; - torrent_handle h = ses.add_torrent(atp, ec); + torrent_handle h = ses.add_torrent(std::move(atp), ec); TEST_CHECK(!h.is_valid()); TEST_CHECK(ec == error_code(errors::no_metadata)) @@ -189,7 +189,7 @@ TORRENT_TEST(paused_session) ps.flags = lt::add_torrent_params::flag_paused; ps.save_path = "."; - torrent_handle h = s.add_torrent(ps); + torrent_handle h = s.add_torrent(std::move(ps)); std::this_thread::sleep_for(lt::milliseconds(2000)); h.resume(); diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 6b9e0f4a9..54d774bd1 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -654,7 +654,7 @@ void test_fastresume(bool const test_deprecated) p.ti = std::make_shared(std::cref(*t)); p.save_path = combine_path(test_path, "tmp1"); p.storage_mode = storage_mode_sparse; - torrent_handle h = ses.add_torrent(p, ec); + torrent_handle h = ses.add_torrent(std::move(p), ec); TEST_CHECK(exists(combine_path(p.save_path, "temporary"))); if (!exists(combine_path(p.save_path, "temporary"))) return; @@ -719,7 +719,7 @@ void test_fastresume(bool const test_deprecated) p.ti = std::make_shared(std::cref(*t)); p.save_path = combine_path(test_path, "tmp1"); p.storage_mode = storage_mode_sparse; - torrent_handle h = ses.add_torrent(p, ec); + torrent_handle h = ses.add_torrent(std::move(p), ec); std::printf("expecting fastresume to be rejected becase the files were removed"); alert const* a = wait_for_alert(ses, fastresume_rejected_alert::alert_type @@ -771,7 +771,7 @@ TORRENT_TEST(rename_file) p.ti = info; p.save_path = "."; error_code ec; - torrent_handle h = ses.add_torrent(p, ec); + torrent_handle h = ses.add_torrent(std::move(p), ec); // make it a seed std::vector tmp(info->piece_length()); @@ -833,7 +833,7 @@ void test_rename_file_fastresume(bool test_deprecated) p.ti = std::make_shared(std::cref(*t)); p.save_path = combine_path(test_path, "tmp2"); p.storage_mode = storage_mode_sparse; - torrent_handle h = ses.add_torrent(p, ec); + torrent_handle h = ses.add_torrent(std::move(p), ec); h.rename_file(file_index_t(0), "testing_renamed_files"); std::cout << "renaming file" << std::endl; @@ -886,7 +886,7 @@ void test_rename_file_fastresume(bool test_deprecated) p.ti = std::make_shared(std::cref(*t)); p.save_path = combine_path(test_path, "tmp2"); p.storage_mode = storage_mode_sparse; - torrent_handle h = ses.add_torrent(p, ec); + torrent_handle h = ses.add_torrent(std::move(p), ec); torrent_status stat; for (int i = 0; i < 50; ++i) diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index 6ea77591a..dec80e662 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -208,7 +208,7 @@ TORRENT_TEST(total_wanted) p.ti = info; - torrent_handle h = ses.add_torrent(p); + torrent_handle h = ses.add_torrent(std::move(p)); torrent_status st = h.status(); std::cout << "total_wanted: " << st.total_wanted << " : " << 1024 << std::endl; @@ -240,7 +240,7 @@ TORRENT_TEST(added_peers) p.save_path = "."; p.url = "magnet:?xt=urn:btih:abababababababababababababababababababab&x.pe=127.0.0.1:48081&x.pe=127.0.0.2:48082"; - torrent_handle h = ses.add_torrent(p); + torrent_handle h = ses.add_torrent(std::move(p)); std::vector v; h.native_handle()->get_full_peer_list(&v); @@ -359,7 +359,7 @@ TORRENT_TEST(duplicate_is_not_error) lt::session ses(settings()); ses.async_add_torrent(p); - ses.async_add_torrent(p); + ses.async_add_torrent(std::move(p)); wait_for_downloading(ses, "ses"); @@ -427,7 +427,7 @@ TORRENT_TEST(async_load_deprecated) p.url = "file://" + combine_path(combine_path(dir, "test_torrents"), "base.torrent"); p.save_path = "."; - ses.async_add_torrent(p); + ses.async_add_torrent(std::move(p)); alert const* a = wait_for_alert(ses, add_torrent_alert::alert_type); TEST_CHECK(a); @@ -471,7 +471,7 @@ TORRENT_TEST(queue) add_torrent_params p; p.ti = ti; p.save_path = "."; - torrents.push_back(ses.add_torrent(p)); + torrents.push_back(ses.add_torrent(std::move(p))); } std::vector pieces(torrents[5].torrent_file()->num_pieces(), 0);