add rvalue reference overloads for add_torrent_params, settings_pack and session_params. Those structures are pretty large so being able to take them by rvalue reference may be beneficial

This commit is contained in:
arvidn 2018-11-11 02:37:13 +01:00 committed by Arvid Norberg
parent f425a3818a
commit 4d88c83576
10 changed files with 92 additions and 35 deletions

View File

@ -979,8 +979,8 @@ void bind_session()
#endif // TORRENT_DISABLE_DHT #endif // TORRENT_DISABLE_DHT
.def("add_torrent", &add_torrent) .def("add_torrent", &add_torrent)
.def("async_add_torrent", &async_add_torrent) .def("async_add_torrent", &async_add_torrent)
.def("async_add_torrent", &lt::session::async_add_torrent) .def("async_add_torrent", static_cast<void (session_handle::*)(lt::add_torrent_params const&)>(&lt::session::async_add_torrent))
.def("add_torrent", allow_threads((lt::torrent_handle (session_handle::*)(add_torrent_params const&))&lt::session::add_torrent)) .def("add_torrent", allow_threads(static_cast<lt::torrent_handle (session_handle::*)(add_torrent_params const&)>(&lt::session::add_torrent)))
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
#if TORRENT_ABI_VERSION == 1 #if TORRENT_ABI_VERSION == 1
.def( .def(

View File

@ -395,7 +395,11 @@ namespace aux {
void add_dht_router(std::pair<std::string, int> const& node); void add_dht_router(std::pair<std::string, int> const& node);
void set_dht_settings(dht::dht_settings const& s); void set_dht_settings(dht::dht_settings const& s);
dht::dht_settings const& get_dht_settings() const { return m_dht_settings; } dht::dht_settings const& get_dht_settings() const { return m_dht_settings; }
void set_dht_state(dht::dht_state state);
// you must give up ownership of the dht state
void set_dht_state(dht::dht_state&& state);
void set_dht_state(dht::dht_state const& state) = delete;
void set_dht_storage(dht::dht_storage_constructor_type sc); void set_dht_storage(dht::dht_storage_constructor_type sc);
void start_dht(); void start_dht();
void stop_dht(); void stop_dht();

View File

@ -72,7 +72,16 @@ namespace libtorrent { namespace dht {
, dht_settings const& settings , dht_settings const& settings
, counters& cnt , counters& cnt
, dht_storage_interface& storage , dht_storage_interface& storage
, dht_state state); , dht_state&& state);
// the dht_state must be moved in!
dht_tracker(dht_observer* observer
, io_service& ios
, send_fun_t const& send_fun
, dht_settings const& settings
, counters& cnt
, dht_storage_interface& storage
, dht_state const& state) = delete;
#if defined(_MSC_VER) && _MSC_VER < 1910 #if defined(_MSC_VER) && _MSC_VER < 1910
// workaround for a bug in msvc 14.0 // workaround for a bug in msvc 14.0

View File

@ -131,10 +131,15 @@ namespace aux {
// (ut_metadata, ut_pex and smart_ban). The default values in the // (ut_metadata, ut_pex and smart_ban). The default values in the
// settings is to start the default features like upnp, NAT-PMP, // settings is to start the default features like upnp, NAT-PMP,
// and dht for example. // and dht for example.
explicit session_params(settings_pack sp = settings_pack()); explicit session_params(settings_pack&& sp);
explicit session_params(settings_pack const& sp);
session_params();
// This constructor helps to configure the set of initial plugins // This constructor helps to configure the set of initial plugins
// to be added to the session before it's started. // to be added to the session before it's started.
session_params(settings_pack sp session_params(settings_pack&& sp
, std::vector<std::shared_ptr<plugin>> exts);
session_params(settings_pack const& sp
, std::vector<std::shared_ptr<plugin>> exts); , std::vector<std::shared_ptr<plugin>> exts);
session_params(session_params const&) = default; session_params(session_params const&) = default;
@ -178,8 +183,13 @@ namespace aux {
// In order to avoid a race condition between starting the session and // In order to avoid a race condition between starting the session and
// configuring it, you can pass in a session_params object. Its settings // configuring it, you can pass in a session_params object. Its settings
// will take effect before the session starts up. // will take effect before the session starts up.
explicit session(session_params params = session_params()) explicit session(session_params const& params)
{ start(session_params(params), nullptr); }
explicit session(session_params&& params)
{ start(std::move(params), nullptr); }
session()
{ {
session_params params;
start(std::move(params), nullptr); start(std::move(params), nullptr);
} }
@ -196,10 +206,10 @@ namespace aux {
// call session::abort() and save the session_proxy first, then // call session::abort() and save the session_proxy first, then
// destruct the session object, then sync with the io_service, then // destruct the session object, then sync with the io_service, then
// destruct the session_proxy object. // destruct the session_proxy object.
session(session_params params, io_service& ios) session(session_params&& params, io_service& ios)
{ { start(std::move(params), &ios); }
start(std::move(params), &ios); session(session_params const& params, io_service& ios)
} { start(session_params(params), &ios); }
// Constructs the session objects which acts as the container of torrents. // Constructs the session objects which acts as the container of torrents.
// It provides configuration options across torrents (such as rate limits, // It provides configuration options across torrents (such as rate limits,
@ -212,11 +222,12 @@ namespace aux {
// NAT-PMP) and default plugins (ut_metadata, ut_pex and smart_ban). The // NAT-PMP) and default plugins (ut_metadata, ut_pex and smart_ban). The
// default is to start those features. If you do not want them to start, // default is to start those features. If you do not want them to start,
// pass 0 as the flags parameter. // pass 0 as the flags parameter.
session(settings_pack pack session(settings_pack&& pack
, session_flags_t const flags = start_default_features | add_default_plugins) , session_flags_t const flags = start_default_features | add_default_plugins)
{ { start(flags, std::move(pack), nullptr); }
start(flags, std::move(pack), nullptr); session(settings_pack const& pack
} , session_flags_t const flags = start_default_features | add_default_plugins)
{ start(flags, settings_pack(pack), nullptr); }
// movable // movable
session(session&&) = default; session(session&&) = default;
@ -239,12 +250,14 @@ namespace aux {
// call session::abort() and save the session_proxy first, then // call session::abort() and save the session_proxy first, then
// destruct the session object, then sync with the io_service, then // destruct the session object, then sync with the io_service, then
// destruct the session_proxy object. // destruct the session_proxy object.
session(settings_pack pack session(settings_pack&& pack
, io_service& ios , io_service& ios
, session_flags_t const flags = start_default_features | add_default_plugins) , session_flags_t const flags = start_default_features | add_default_plugins)
{ { start(flags, std::move(pack), &ios); }
start(flags, std::move(pack), &ios); session(settings_pack const& pack
} , io_service& ios
, session_flags_t const flags = start_default_features | add_default_plugins)
{ start(flags, settings_pack(pack), &ios); }
#if TORRENT_ABI_VERSION == 1 #if TORRENT_ABI_VERSION == 1
#ifdef __GNUC__ #ifdef __GNUC__
@ -348,8 +361,11 @@ namespace aux {
private: private:
void start(session_params params, io_service* ios); void start(session_params&& params, io_service* ios);
void start(session_flags_t flags, settings_pack sp, io_service* ios); void start(session_flags_t flags, settings_pack&& sp, io_service* ios);
void start(session_params const& params, io_service* ios) = delete;
void start(session_flags_t flags, settings_pack const& sp, io_service* ios) = delete;
// data shared between the main thread // data shared between the main thread
// and the working thread // and the working thread

View File

@ -215,7 +215,8 @@ 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 params); void async_add_torrent(add_torrent_params&& params);
void async_add_torrent(add_torrent_params const& params);
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
#if TORRENT_ABI_VERSION == 1 #if TORRENT_ABI_VERSION == 1

View File

@ -124,7 +124,7 @@ TORRENT_TEST(dht_rate_limit)
std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(dhtsett)); std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(dhtsett));
auto dht = std::make_shared<lt::dht::dht_tracker>( auto dht = std::make_shared<lt::dht::dht_tracker>(
&o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5) &o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5)
, dhtsett, cnt, *dht_storage, state); , dhtsett, cnt, *dht_storage, std::move(state));
dht->new_socket(ls); dht->new_socket(ls);
bool stop = false; bool stop = false;
@ -245,7 +245,7 @@ TORRENT_TEST(dht_delete_socket)
std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(dhtsett)); std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(dhtsett));
auto dht = std::make_shared<lt::dht::dht_tracker>( auto dht = std::make_shared<lt::dht::dht_tracker>(
&o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5) &o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5)
, dhtsett, cnt, *dht_storage, state); , dhtsett, cnt, *dht_storage, std::move(state));
dht->start([](std::vector<std::pair<dht::node_entry, std::string>> const&){}); dht->start([](std::vector<std::pair<dht::node_entry, std::string>> const&){});
dht->new_socket(ls); dht->new_socket(ls);

View File

@ -88,7 +88,7 @@ namespace libtorrent { namespace dht {
, dht_settings const& settings , dht_settings const& settings
, counters& cnt , counters& cnt
, dht_storage_interface& storage , dht_storage_interface& storage
, dht_state state) , dht_state&& state)
: m_counters(cnt) : m_counters(cnt)
, m_storage(storage) , m_storage(storage)
, m_state(std::move(state)) , m_state(std::move(state))

View File

@ -319,7 +319,7 @@ namespace {
return params; return params;
} }
void session::start(session_params params, io_service* ios) void session::start(session_params&& params, io_service* ios)
{ {
bool const internal_executor = ios == nullptr; bool const internal_executor = ios == nullptr;
@ -334,16 +334,18 @@ namespace {
*static_cast<session_handle*>(this) = session_handle(m_impl); *static_cast<session_handle*>(this) = session_handle(m_impl);
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (auto const& ext : params.extensions) for (auto& ext : params.extensions)
{ {
m_impl->add_ses_extension(ext); m_impl->add_ses_extension(std::move(ext));
} }
#endif #endif
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
m_impl->set_dht_settings(params.dht_settings); m_impl->set_dht_settings(std::move(params.dht_settings));
m_impl->set_dht_state(std::move(params.dht_state)); m_impl->set_dht_state(std::move(params.dht_state));
m_impl->set_dht_storage(params.dht_storage_constructor);
TORRENT_ASSERT(params.dht_storage_constructor);
m_impl->set_dht_storage(std::move(params.dht_storage_constructor));
#endif #endif
m_impl->start_session(); m_impl->start_session();
@ -376,7 +378,7 @@ namespace {
} }
} }
void session::start(session_flags_t const flags, settings_pack sp, io_service* ios) void session::start(session_flags_t const flags, settings_pack&& sp, io_service* ios)
{ {
start({std::move(sp), start({std::move(sp),
default_plugins(!(flags & add_default_plugins))}, ios); default_plugins(!(flags & add_default_plugins))}, ios);
@ -431,16 +433,36 @@ namespace {
} }
} }
session_params::session_params(settings_pack sp) session_params::session_params(settings_pack&& sp)
: session_params(std::move(sp), default_plugins()) : session_params(std::move(sp), default_plugins())
{} {}
session_params::session_params(settings_pack sp session_params::session_params(settings_pack const& sp)
: session_params(sp, default_plugins())
{}
session_params::session_params()
: extensions(default_plugins())
#ifndef TORRENT_DISABLE_DHT
, dht_storage_constructor(dht::dht_default_storage_constructor)
#endif
{}
session_params::session_params(settings_pack&& sp
, std::vector<std::shared_ptr<plugin>> exts) , std::vector<std::shared_ptr<plugin>> exts)
: settings(std::move(sp)) : settings(std::move(sp))
, extensions(std::move(exts)) , extensions(std::move(exts))
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
, dht_storage_constructor(dht::dht_default_storage_constructor) , dht_storage_constructor(dht::dht_default_storage_constructor)
#endif
{}
session_params::session_params(settings_pack const& sp
, std::vector<std::shared_ptr<plugin>> exts)
: settings(sp)
, extensions(std::move(exts))
#ifndef TORRENT_DISABLE_DHT
, dht_storage_constructor(dht::dht_default_storage_constructor)
#endif #endif
{} {}
} }

View File

@ -393,7 +393,12 @@ namespace {
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 params) void session_handle::async_add_torrent(add_torrent_params const& params)
{
async_add_torrent(add_torrent_params(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());

View File

@ -5860,7 +5860,7 @@ namespace aux {
m_dht_settings = settings; m_dht_settings = settings;
} }
void session_impl::set_dht_state(dht::dht_state state) void session_impl::set_dht_state(dht::dht_state&& state)
{ {
m_dht_state = std::move(state); m_dht_state = std::move(state);
} }