make add_torrent_params movable and have add_torrent/async_add_torrent take it by value

This commit is contained in:
arvidn 2017-03-26 14:54:16 -04:00 committed by Arvid Norberg
parent 967ad06899
commit 6cdd598e7c
16 changed files with 53 additions and 49 deletions

View File

@ -51,7 +51,7 @@ int main(int argc, char const* argv[])
lt::add_torrent_params atp; lt::add_torrent_params atp;
atp.url = argv[1]; atp.url = argv[1];
atp.save_path = "."; // save in current dir 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 (;;) { for (;;) {
std::vector<lt::alert*> alerts; std::vector<lt::alert*> alerts;

View File

@ -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); lt::add_torrent_params atp = lt::read_resume_data(&buf[0], int(buf.size()), ec);
atp.url = argv[1]; atp.url = argv[1];
atp.save_path = "."; // save in current dir 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 // this is the handle we'll set once we get the notification of it being
// added // added

View File

@ -568,7 +568,7 @@ void add_magnet(lt::session& ses, lt::string_view uri)
p.storage_mode = static_cast<lt::storage_mode_t>(allocation_mode); p.storage_mode = static_cast<lt::storage_mode_t>(allocation_mode);
std::printf("adding magnet: %s\n", uri.to_string().c_str()); 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 // 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.storage_mode = (storage_mode_t)allocation_mode;
p.flags &= ~add_torrent_params::flag_duplicate_is_error; p.flags &= ~add_torrent_params::flag_duplicate_is_error;
p.userdata = static_cast<void*>(new std::string(torrent)); p.userdata = static_cast<void*>(new std::string(torrent));
ses.async_add_torrent(p); ses.async_add_torrent(std::move(p));
return true; return true;
} }
@ -913,7 +913,7 @@ bool handle_alert(torrent_view& view, session_view& ses_view
} }
else if (state_update_alert* p = alert_cast<state_update_alert>(a)) else if (state_update_alert* p = alert_cast<state_update_alert>(a))
{ {
view.update_torrents(p->status); view.update_torrents(std::move(p->status));
return true; return true;
} }
return false; return false;
@ -1274,7 +1274,7 @@ MAGNETURL is a magnet link
continue; continue;
} }
ses.async_add_torrent(p); ses.async_add_torrent(std::move(p));
} }
} }

View File

@ -96,18 +96,17 @@ lt::torrent_handle torrent_view::get_active_handle() const
return m_filtered_handles[m_active_torrent]->handle; return m_filtered_handles[m_active_torrent]->handle;
} }
void torrent_view::update_torrents(std::vector<lt::torrent_status> const& st) void torrent_view::update_torrents(std::vector<lt::torrent_status> st)
{ {
std::set<lt::torrent_handle> updates; std::set<lt::torrent_handle> updates;
bool need_filter_update = false; bool need_filter_update = false;
for (std::vector<lt::torrent_status>::const_iterator i = st.begin(); for (lt::torrent_status& t : st)
i != st.end(); ++i)
{ {
auto j = m_all_handles.find(*i); auto j = m_all_handles.find(t);
// add new entries here // add new entries here
if (j == m_all_handles.end()) 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)) if (show_torrent(*j))
{ {
m_filtered_handles.push_back(&*j); m_filtered_handles.push_back(&*j);
@ -116,12 +115,12 @@ void torrent_view::update_torrents(std::vector<lt::torrent_status> const& st)
} }
else else
{ {
bool prev_show = show_torrent(*j); bool const prev_show = show_torrent(*j);
((lt::torrent_status&)*j) = *i; const_cast<lt::torrent_status&>(*j) = std::move(t);
if (prev_show != show_torrent(*j)) if (prev_show != show_torrent(*j))
need_filter_update = true; need_filter_update = true;
else else
updates.insert(i->handle); updates.insert(j->handle);
} }
} }
if (need_filter_update) if (need_filter_update)
@ -132,19 +131,16 @@ void torrent_view::update_torrents(std::vector<lt::torrent_status> const& st)
else else
{ {
int torrent_index = 0; int torrent_index = 0;
for (std::vector<lt::torrent_status const*>::iterator i for (auto i = m_filtered_handles.begin();
= m_filtered_handles.begin(); i != m_filtered_handles.end(); ++torrent_index, ++i)
i != m_filtered_handles.end(); ++torrent_index)
{ {
if (torrent_index < m_scroll_position if (torrent_index < m_scroll_position
|| torrent_index >= m_scroll_position + m_height - header_size) || torrent_index >= m_scroll_position + m_height - header_size)
{ {
++i;
continue; continue;
} }
lt::torrent_status const& s = **i; lt::torrent_status const& s = **i;
++i;
if (!s.handle.is_valid()) if (!s.handle.is_valid())
continue; continue;

View File

@ -36,7 +36,7 @@ struct torrent_view
lt::torrent_status const& get_active_torrent() const; lt::torrent_status const& get_active_torrent() const;
lt::torrent_handle get_active_handle() const; lt::torrent_handle get_active_handle() const;
void update_torrents(std::vector<lt::torrent_status> const& st); void update_torrents(std::vector<lt::torrent_status> st);
int height() const; int height() const;

View File

@ -87,6 +87,10 @@ namespace libtorrent
// data for the torrent. For more information, see the ``storage`` field. // data for the torrent. For more information, see the ``storage`` field.
explicit add_torrent_params(storage_constructor_type sc = default_storage_constructor) explicit add_torrent_params(storage_constructor_type sc = default_storage_constructor)
: storage(sc) {} : 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 // values for the ``flags`` field
enum flags_t : std::uint64_t enum flags_t : std::uint64_t

View File

@ -474,7 +474,7 @@ namespace libtorrent
std::shared_ptr<torrent> const& torrent_ptr, void* userdata); std::shared_ptr<torrent> const& torrent_ptr, void* userdata);
#endif #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 // second return value is true if the torrent was added and false if an
// existing one was found. // existing one was found.
std::pair<std::shared_ptr<torrent>, bool> std::pair<std::shared_ptr<torrent>, bool>

View File

@ -65,13 +65,13 @@ namespace libtorrent
// deprecated in 0.16. Instead, pass in the magnet link as add_torrent_params::url // deprecated in 0.16. Instead, pass in the magnet link as add_torrent_params::url
TORRENT_DEPRECATED TORRENT_DEPRECATED
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params const& p); , add_torrent_params p);
#endif #endif
// deprecated in 0.16. Instead, pass in the magnet link as add_torrent_params::url // deprecated in 0.16. Instead, pass in the magnet link as add_torrent_params::url
TORRENT_DEPRECATED TORRENT_DEPRECATED
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri 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 #endif

View File

@ -232,7 +232,7 @@ namespace libtorrent
torrent_handle add_torrent(add_torrent_params const& params); torrent_handle add_torrent(add_torrent_params const& params);
#endif #endif
torrent_handle add_torrent(add_torrent_params const& params, error_code& ec); 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 BOOST_NO_EXCEPTIONS
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE

View File

@ -80,7 +80,7 @@ void run_test(Setup const& setup, Torrent const& torrent
lt::add_torrent_params params = create_torrent(0, false); 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_auto_managed;
params.flags &= ~lt::add_torrent_params::flag_paused; params.flags &= ~lt::add_torrent_params::flag_paused;
ses->async_add_torrent(params); ses->async_add_torrent(std::move(params));
lt::torrent_handle h; lt::torrent_handle h;
print_alerts(*ses, [&](lt::session& ses, lt::alert const* a) { print_alerts(*ses, [&](lt::session& ses, lt::alert const* a) {

View File

@ -113,14 +113,14 @@ namespace libtorrent
{ {
parse_magnet_uri(uri, p, ec); parse_magnet_uri(uri, p, ec);
if (ec) return torrent_handle(); 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 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 #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()); 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()); 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 torrent_handle add_magnet_uri(session& ses, std::string const& uri
, add_torrent_params const& p) , add_torrent_params p)
{ {
error_code ec; 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<system_error>(ec); if (ec) aux::throw_ex<system_error>(ec);
return ret; return ret;
} }

View File

@ -342,11 +342,14 @@ namespace libtorrent
return sync_call_ret<torrent_handle>(&session_impl::add_torrent, p, ecr); return sync_call_ret<torrent_handle>(&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()); 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); p->save_path = complete(p->save_path);
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE

View File

@ -4619,7 +4619,7 @@ namespace aux {
#endif #endif
error_code ec; error_code ec;
add_torrent(*params, ec); add_torrent(std::move(*params), ec);
} }
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
@ -4636,7 +4636,7 @@ namespace aux {
TORRENT_ASSERT(params->ti->is_valid()); TORRENT_ASSERT(params->ti->is_valid());
TORRENT_ASSERT(params->ti->num_files() > 0); TORRENT_ASSERT(params->ti->num_files() > 0);
params->url.clear(); params->url.clear();
add_torrent(*params, ec); add_torrent(std::move(*params), ec);
} }
#endif #endif
@ -4653,11 +4653,10 @@ namespace aux {
} }
#endif #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) , error_code& ec)
{ {
// params is updated by add_torrent_impl() // params is updated by add_torrent_impl()
add_torrent_params params = p;
std::shared_ptr<torrent> torrent_ptr; std::shared_ptr<torrent> torrent_ptr;
// in case there's an error, make sure to abort the torrent before leaving // 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(); }); auto abort_torrent = aux::scope_end([&]{ if (torrent_ptr) torrent_ptr->abort(); });
bool added; 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); std::tie(torrent_ptr, added) = add_torrent_impl(params, ec);
torrent_handle const handle(torrent_ptr); torrent_handle const handle(torrent_ptr);

View File

@ -157,7 +157,7 @@ TORRENT_TEST(load_empty_file)
atp.ti = std::make_shared<torrent_info>("", 0, std::ref(ignore_errors)); atp.ti = std::make_shared<torrent_info>("", 0, std::ref(ignore_errors));
atp.save_path = "."; atp.save_path = ".";
error_code ec; 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(!h.is_valid());
TEST_CHECK(ec == error_code(errors::no_metadata)) 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.flags = lt::add_torrent_params::flag_paused;
ps.save_path = "."; 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)); std::this_thread::sleep_for(lt::milliseconds(2000));
h.resume(); h.resume();

View File

@ -654,7 +654,7 @@ void test_fastresume(bool const test_deprecated)
p.ti = std::make_shared<torrent_info>(std::cref(*t)); p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp1"); p.save_path = combine_path(test_path, "tmp1");
p.storage_mode = storage_mode_sparse; 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"))); TEST_CHECK(exists(combine_path(p.save_path, "temporary")));
if (!exists(combine_path(p.save_path, "temporary"))) if (!exists(combine_path(p.save_path, "temporary")))
return; return;
@ -719,7 +719,7 @@ void test_fastresume(bool const test_deprecated)
p.ti = std::make_shared<torrent_info>(std::cref(*t)); p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp1"); p.save_path = combine_path(test_path, "tmp1");
p.storage_mode = storage_mode_sparse; 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"); std::printf("expecting fastresume to be rejected becase the files were removed");
alert const* a = wait_for_alert(ses, fastresume_rejected_alert::alert_type alert const* a = wait_for_alert(ses, fastresume_rejected_alert::alert_type
@ -771,7 +771,7 @@ TORRENT_TEST(rename_file)
p.ti = info; p.ti = info;
p.save_path = "."; p.save_path = ".";
error_code ec; 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 // make it a seed
std::vector<char> tmp(info->piece_length()); std::vector<char> tmp(info->piece_length());
@ -833,7 +833,7 @@ void test_rename_file_fastresume(bool test_deprecated)
p.ti = std::make_shared<torrent_info>(std::cref(*t)); p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp2"); p.save_path = combine_path(test_path, "tmp2");
p.storage_mode = storage_mode_sparse; 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"); h.rename_file(file_index_t(0), "testing_renamed_files");
std::cout << "renaming file" << std::endl; std::cout << "renaming file" << std::endl;
@ -886,7 +886,7 @@ void test_rename_file_fastresume(bool test_deprecated)
p.ti = std::make_shared<torrent_info>(std::cref(*t)); p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp2"); p.save_path = combine_path(test_path, "tmp2");
p.storage_mode = storage_mode_sparse; 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; torrent_status stat;
for (int i = 0; i < 50; ++i) for (int i = 0; i < 50; ++i)

View File

@ -208,7 +208,7 @@ TORRENT_TEST(total_wanted)
p.ti = info; p.ti = info;
torrent_handle h = ses.add_torrent(p); torrent_handle h = ses.add_torrent(std::move(p));
torrent_status st = h.status(); torrent_status st = h.status();
std::cout << "total_wanted: " << st.total_wanted << " : " << 1024 << std::endl; std::cout << "total_wanted: " << st.total_wanted << " : " << 1024 << std::endl;
@ -240,7 +240,7 @@ TORRENT_TEST(added_peers)
p.save_path = "."; p.save_path = ".";
p.url = "magnet:?xt=urn:btih:abababababababababababababababababababab&x.pe=127.0.0.1:48081&x.pe=127.0.0.2:48082"; 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<peer_list_entry> v; std::vector<peer_list_entry> v;
h.native_handle()->get_full_peer_list(&v); h.native_handle()->get_full_peer_list(&v);
@ -359,7 +359,7 @@ TORRENT_TEST(duplicate_is_not_error)
lt::session ses(settings()); lt::session ses(settings());
ses.async_add_torrent(p); ses.async_add_torrent(p);
ses.async_add_torrent(p); ses.async_add_torrent(std::move(p));
wait_for_downloading(ses, "ses"); 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.url = "file://" + combine_path(combine_path(dir, "test_torrents"), "base.torrent");
p.save_path = "."; 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); alert const* a = wait_for_alert(ses, add_torrent_alert::alert_type);
TEST_CHECK(a); TEST_CHECK(a);
@ -471,7 +471,7 @@ TORRENT_TEST(queue)
add_torrent_params p; add_torrent_params p;
p.ti = ti; p.ti = ti;
p.save_path = "."; p.save_path = ".";
torrents.push_back(ses.add_torrent(p)); torrents.push_back(ses.add_torrent(std::move(p)));
} }
std::vector<int> pieces(torrents[5].torrent_file()->num_pieces(), 0); std::vector<int> pieces(torrents[5].torrent_file()->num_pieces(), 0);