refactor to more use of auto c++11 keyword (#1404)

refactor to more use of auto c++11 keyword
This commit is contained in:
Alden Torres 2016-12-11 20:50:30 -05:00 committed by Arvid Norberg
parent 21fb0e8c20
commit d9f8d4b642
13 changed files with 44 additions and 65 deletions

View File

@ -140,16 +140,16 @@ namespace libtorrent
bool found = false; bool found = false;
for (int i = 0; i < int(ifs.size()); ++i) for (auto const& iface : ifs)
{ {
// we're looking for a specific interface, and its address // we're looking for a specific interface, and its address
// (which must be of the same family as the address we're // (which must be of the same family as the address we're
// connecting to) // connecting to)
if (strcmp(ifs[i].name, device_name) != 0) continue; if (std::strcmp(iface.name, device_name) != 0) continue;
if (ifs[i].interface_address.is_v4() != (protocol == boost::asio::ip::tcp::v4())) if (iface.interface_address.is_v4() != (protocol == boost::asio::ip::tcp::v4()))
continue; continue;
bind_ep.address(ifs[i].interface_address); bind_ep.address(iface.interface_address);
found = true; found = true;
break; break;
} }

View File

@ -393,12 +393,9 @@ namespace libtorrent
// maps transactionid to the udp_tracker_connection // maps transactionid to the udp_tracker_connection
// These must use shared_ptr to avoid a dangling reference // These must use shared_ptr to avoid a dangling reference
// if a connection is erased while a timeout event is in the queue // if a connection is erased while a timeout event is in the queue
using udp_conns_t = std::unordered_map<std::uint32_t std::unordered_map<std::uint32_t, std::shared_ptr<udp_tracker_connection>> m_udp_conns;
, std::shared_ptr<udp_tracker_connection>> ;
udp_conns_t m_udp_conns;
using http_conns_t = std::vector<std::shared_ptr<http_tracker_connection>>; std::vector<std::shared_ptr<http_tracker_connection>> m_http_conns;
http_conns_t m_http_conns;
send_fun_t m_send_fun; send_fun_t m_send_fun;
send_fun_hostname_t m_send_fun_hostname; send_fun_hostname_t m_send_fun_hostname;

View File

@ -265,11 +265,10 @@ namespace libtorrent
, aux::session_settings const& sett) , aux::session_settings const& sett)
{ {
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
for (std::vector<peer_connection*>::iterator i = peers.begin() for (auto p : peers)
, end(peers.end()); i != end; ++i)
{ {
TORRENT_ASSERT((*i)->self()); TORRENT_ASSERT(p->self());
TORRENT_ASSERT((*i)->associated_torrent().lock()); TORRENT_ASSERT(p->associated_torrent().lock());
} }
#endif #endif
@ -288,10 +287,8 @@ namespace libtorrent
if (sett.get_int(settings_pack::choking_algorithm) if (sett.get_int(settings_pack::choking_algorithm)
== settings_pack::bittyrant_choker) == settings_pack::bittyrant_choker)
{ {
for (std::vector<peer_connection*>::const_iterator i = peers.begin() for (auto const p : peers)
, end(peers.end()); i != end; ++i)
{ {
peer_connection* p = *i;
if (p->is_choked() || !p->is_interesting()) continue; if (p->is_choked() || !p->is_interesting()) continue;
if (!p->has_peer_choked()) if (!p->has_peer_choked())
@ -320,10 +317,8 @@ namespace libtorrent
// until there none left // until there none left
upload_slots = 0; upload_slots = 0;
for (std::vector<peer_connection*>::iterator i = peers.begin() for (auto const p : peers)
, end(peers.end()); i != end; ++i)
{ {
peer_connection* p = *i;
TORRENT_ASSERT(p); TORRENT_ASSERT(p);
if (p->est_reciprocation_rate() > upload_capacity_left) break; if (p->est_reciprocation_rate() > upload_capacity_left) break;
@ -363,11 +358,9 @@ namespace libtorrent
// TODO: make configurable // TODO: make configurable
int rate_threshold = 1024; int rate_threshold = 1024;
for (std::vector<peer_connection*>::const_iterator i = peers.begin() for (auto const p : peers)
, end(peers.end()); i != end; ++i)
{ {
peer_connection const& p = **i; int const rate = int(p->uploaded_in_last_round()
int const rate = int(p.uploaded_in_last_round()
* 1000 / total_milliseconds(unchoke_interval)); * 1000 / total_milliseconds(unchoke_interval));
if (rate < rate_threshold) break; if (rate < rate_threshold) break;

View File

@ -1149,8 +1149,8 @@ namespace libtorrent
std::vector<ip_interface> ifs = enum_net_interfaces(ios, ec); std::vector<ip_interface> ifs = enum_net_interfaces(ios, ec);
if (ec) return false; if (ec) return false;
for (int i = 0; i < int(ifs.size()); ++i) for (auto const& iface : ifs)
if (ifs[i].name == name) return true; if (iface.name == name) return true;
return false; return false;
} }
@ -1161,8 +1161,8 @@ namespace libtorrent
std::vector<ip_interface> ifs = enum_net_interfaces(ios, ec); std::vector<ip_interface> ifs = enum_net_interfaces(ios, ec);
if (ec) return std::string(); if (ec) return std::string();
for (int i = 0; i < int(ifs.size()); ++i) for (auto const& iface : ifs)
if (ifs[i].interface_address == addr) return ifs[i].name; if (iface.interface_address == addr) return iface.name;
return std::string(); return std::string();
} }
} }

View File

@ -234,7 +234,7 @@ int natpmp::add_mapping(portmap_protocol const p, int const external_port
if (m_disabled) return -1; if (m_disabled) return -1;
std::vector<mapping_t>::iterator i = std::find_if(m_mappings.begin() auto i = std::find_if(m_mappings.begin()
, m_mappings.end(), [] (mapping_t const& m) { return m.protocol == portmap_protocol::none; }); , m_mappings.end(), [] (mapping_t const& m) { return m.protocol == portmap_protocol::none; });
if (i == m_mappings.end()) if (i == m_mappings.end())
{ {
@ -275,7 +275,7 @@ void natpmp::try_next_mapping(int const i)
return; return;
} }
std::vector<mapping_t>::iterator const m = std::find_if( auto const m = std::find_if(
m_mappings.begin(), m_mappings.end() m_mappings.begin(), m_mappings.end()
, [] (mapping_t const& ma) { return ma.act != mapping_t::action::none; }); , [] (mapping_t const& ma) { return ma.act != mapping_t::action::none; });

View File

@ -180,7 +180,7 @@ namespace libtorrent
if (ec) return -1; if (ec) return -1;
int slot = -1; int slot = -1;
std::unordered_map<int, int>::iterator i = m_piece_map.find(piece); auto const i = m_piece_map.find(piece);
if (i == m_piece_map.end()) if (i == m_piece_map.end())
slot = allocate_slot(piece); slot = allocate_slot(piece);
else else
@ -198,7 +198,7 @@ namespace libtorrent
TORRENT_ASSERT(offset >= 0); TORRENT_ASSERT(offset >= 0);
std::unique_lock<std::mutex> l(m_mutex); std::unique_lock<std::mutex> l(m_mutex);
std::unordered_map<int, int>::iterator i = m_piece_map.find(piece); auto const i = m_piece_map.find(piece);
if (i == m_piece_map.end()) if (i == m_piece_map.end())
{ {
ec = error_code(boost::system::errc::no_such_file_or_directory ec = error_code(boost::system::errc::no_such_file_or_directory
@ -242,7 +242,7 @@ namespace libtorrent
{ {
std::lock_guard<std::mutex> l(m_mutex); std::lock_guard<std::mutex> l(m_mutex);
std::unordered_map<int, int>::iterator i = m_piece_map.find(piece); auto const i = m_piece_map.find(piece);
if (i == m_piece_map.end()) return; if (i == m_piece_map.end()) return;
// TODO: what do we do if someone is currently reading from the disk // TODO: what do we do if someone is currently reading from the disk
@ -310,7 +310,7 @@ namespace libtorrent
std::int64_t file_offset = 0; std::int64_t file_offset = 0;
for (; piece < end; ++piece) for (; piece < end; ++piece)
{ {
std::unordered_map<int, int>::iterator i = m_piece_map.find(piece); auto const i = m_piece_map.find(piece);
int const block_to_copy = int((std::min)(m_piece_size - piece_offset, size)); int const block_to_copy = int((std::min)(m_piece_size - piece_offset, size));
if (i != m_piece_map.end()) if (i != m_piece_map.end())
{ {
@ -345,7 +345,7 @@ namespace libtorrent
// another thread removed this slot map entry, and invalidated // another thread removed this slot map entry, and invalidated
// our iterator. Now that we hold the lock again, perform // our iterator. Now that we hold the lock again, perform
// another lookup to be sure. // another lookup to be sure.
std::unordered_map<int, int>::iterator j = m_piece_map.find(piece); auto const j = m_piece_map.find(piece);
if (j != m_piece_map.end()) if (j != m_piece_map.end())
{ {
// if the slot moved, that's really suspicious // if the slot moved, that's really suspicious
@ -404,7 +404,7 @@ namespace libtorrent
for (int piece = 0; piece < m_max_pieces; ++piece) for (int piece = 0; piece < m_max_pieces; ++piece)
{ {
std::unordered_map<int, int>::iterator i = m_piece_map.find(piece); auto const i = m_piece_map.find(piece);
int slot = 0xffffffff; int slot = 0xffffffff;
if (i != m_piece_map.end()) if (i != m_piece_map.end())
slot = i->second; slot = i->second;

View File

@ -58,8 +58,7 @@ namespace libtorrent
void peer_class_set::remove_class(peer_class_pool& pool, peer_class_t const c) void peer_class_set::remove_class(peer_class_pool& pool, peer_class_t const c)
{ {
std::array<peer_class_t, 15>::iterator i = std::find(m_class.begin() auto const i = std::find(m_class.begin(), m_class.begin() + m_size, c);
, m_class.begin() + m_size, c);
int idx = int(i - m_class.begin()); int idx = int(i - m_class.begin());
if (idx == m_size) return; // not found if (idx == m_size) return; // not found
if (idx < m_size - 1) if (idx < m_size - 1)

View File

@ -2277,7 +2277,7 @@ namespace libtorrent
} }
int fast_idx = -1; int fast_idx = -1;
std::vector<int>::iterator fast_iter = std::find(m_accept_fast.begin() auto const fast_iter = std::find(m_accept_fast.begin()
, m_accept_fast.end(), r.piece); , m_accept_fast.end(), r.piece);
if (fast_iter != m_accept_fast.end()) fast_idx = int(fast_iter - m_accept_fast.begin()); if (fast_iter != m_accept_fast.end()) fast_idx = int(fast_iter - m_accept_fast.begin());

View File

@ -4173,10 +4173,8 @@ namespace aux {
// go through all the peers and unchoke the first ones and choke // go through all the peers and unchoke the first ones and choke
// all the other ones. // all the other ones.
for (std::vector<peer_connection*>::iterator i = peers.begin() for (auto p : peers)
, end(peers.end()); i != end; ++i)
{ {
peer_connection* p = *i;
TORRENT_ASSERT(p); TORRENT_ASSERT(p);
TORRENT_ASSERT(!p->ignore_unchoke_slots()); TORRENT_ASSERT(!p->ignore_unchoke_slots());

View File

@ -113,7 +113,7 @@ namespace libtorrent
int stat_cache::add_error(error_code const& ec) int stat_cache::add_error(error_code const& ec)
{ {
std::vector<error_code>::iterator i = std::find(m_errors.begin(), m_errors.end(), ec); auto const i = std::find(m_errors.begin(), m_errors.end(), ec);
if (i != m_errors.end()) return int(i - m_errors.begin()); if (i != m_errors.end()) return int(i - m_errors.begin());
m_errors.push_back(ec); m_errors.push_back(ec);
return int(m_errors.size()) - 1; return int(m_errors.size()) - 1;

View File

@ -235,7 +235,7 @@ namespace libtorrent
void tracker_manager::remove_request(http_tracker_connection const* c) void tracker_manager::remove_request(http_tracker_connection const* c)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
http_conns_t::iterator i = std::find_if(m_http_conns.begin(), m_http_conns.end() auto const i = std::find_if(m_http_conns.begin(), m_http_conns.end()
, [c] (std::shared_ptr<http_tracker_connection> const& ptr) { return ptr.get() == c; }); , [c] (std::shared_ptr<http_tracker_connection> const& ptr) { return ptr.get() == c; });
if (i != m_http_conns.end()) if (i != m_http_conns.end())
{ {
@ -283,18 +283,14 @@ namespace libtorrent
if (protocol == "http") if (protocol == "http")
#endif #endif
{ {
std::shared_ptr<http_tracker_connection> con auto con = std::make_shared<http_tracker_connection>(ios, *this, req, c);
= std::make_shared<http_tracker_connection>(
std::ref(ios), std::ref(*this), std::cref(req), c);
m_http_conns.push_back(con); m_http_conns.push_back(con);
con->start(); con->start();
return; return;
} }
else if (protocol == "udp") else if (protocol == "udp")
{ {
std::shared_ptr<udp_tracker_connection> con auto con = std::make_shared<udp_tracker_connection>(ios, *this, req, c);
= std::make_shared<udp_tracker_connection>(
std::ref(ios), std::ref(*this), std::cref(req) , c);
m_udp_conns[con->transaction_id()] = con; m_udp_conns[con->transaction_id()] = con;
con->start(); con->start();
return; return;
@ -331,7 +327,7 @@ namespace libtorrent
if (action > 3) return false; if (action > 3) return false;
std::uint32_t const transaction = aux::read_uint32(ptr); std::uint32_t const transaction = aux::read_uint32(ptr);
udp_conns_t::iterator const i = m_udp_conns.find(transaction); auto const i = m_udp_conns.find(transaction);
if (i == m_udp_conns.end()) if (i == m_udp_conns.end())
{ {
@ -372,7 +368,7 @@ namespace libtorrent
if (action > 3) return false; if (action > 3) return false;
std::uint32_t const transaction = aux::read_uint32(ptr); std::uint32_t const transaction = aux::read_uint32(ptr);
udp_conns_t::iterator const i = m_udp_conns.find(transaction); auto const i = m_udp_conns.find(transaction);
if (i == m_udp_conns.end()) if (i == m_udp_conns.end())
{ {
@ -413,28 +409,25 @@ namespace libtorrent
// removes all connections except 'event=stopped'-requests // removes all connections except 'event=stopped'-requests
m_abort = true; m_abort = true;
http_conns_t close_http_connections; std::vector<std::shared_ptr<http_tracker_connection>> close_http_connections;
std::vector<std::shared_ptr<udp_tracker_connection>> close_udp_connections; std::vector<std::shared_ptr<udp_tracker_connection>> close_udp_connections;
for (http_conns_t::iterator i = m_http_conns.begin() for (auto const& c : m_http_conns)
, end(m_http_conns.end()); i != end; ++i)
{ {
http_tracker_connection* c = i->get();
tracker_request const& req = c->tracker_req(); tracker_request const& req = c->tracker_req();
if (req.event == tracker_request::stopped && !all) if (req.event == tracker_request::stopped && !all)
continue; continue;
close_http_connections.push_back(*i); close_http_connections.push_back(c);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
std::shared_ptr<request_callback> rc = c->requester(); std::shared_ptr<request_callback> rc = c->requester();
if (rc) rc->debug_log("aborting: %s", req.url.c_str()); if (rc) rc->debug_log("aborting: %s", req.url.c_str());
#endif #endif
} }
for (udp_conns_t::iterator i = m_udp_conns.begin() for (auto const& p : m_udp_conns)
, end(m_udp_conns.end()); i != end; ++i)
{ {
std::shared_ptr<udp_tracker_connection> c = i->second; auto const& c = p.second;
tracker_request const& req = c->tracker_req(); tracker_request const& req = c->tracker_req();
if (req.event == tracker_request::stopped && !all) if (req.event == tracker_request::stopped && !all)
continue; continue;
@ -447,10 +440,9 @@ namespace libtorrent
#endif #endif
} }
for (http_conns_t::iterator i = close_http_connections.begin() for (auto const& c : close_http_connections)
, end(close_http_connections.end()); i != end; ++i)
{ {
(*i)->close(); c->close();
} }
for (auto const& c : close_udp_connections) for (auto const& c : close_udp_connections)

View File

@ -762,7 +762,7 @@ void upnp::next(rootdevice& d, int i)
} }
else else
{ {
std::vector<mapping_t>::iterator j = std::find_if(d.mapping.begin(), d.mapping.end() auto const j = std::find_if(d.mapping.begin(), d.mapping.end()
, [] (mapping_t const& m) { return m.act != mapping_t::action::none; }); , [] (mapping_t const& m) { return m.act != mapping_t::action::none; });
if (j == d.mapping.end()) return; if (j == d.mapping.end()) return;

View File

@ -366,7 +366,7 @@ namespace libtorrent { namespace
break; break;
case metadata_piece: case metadata_piece:
{ {
std::vector<int>::iterator i = std::find(m_sent_requests.begin() auto const i = std::find(m_sent_requests.begin()
, m_sent_requests.end(), piece); , m_sent_requests.end(), piece);
// unwanted piece? // unwanted piece?
@ -389,7 +389,7 @@ namespace libtorrent { namespace
case metadata_dont_have: case metadata_dont_have:
{ {
m_request_limit = (std::max)(aux::time_now() + minutes(1), m_request_limit); m_request_limit = (std::max)(aux::time_now() + minutes(1), m_request_limit);
std::vector<int>::iterator i = std::find(m_sent_requests.begin() auto const i = std::find(m_sent_requests.begin()
, m_sent_requests.end(), piece); , m_sent_requests.end(), piece);
// unwanted piece? // unwanted piece?
if (i == m_sent_requests.end()) return true; if (i == m_sent_requests.end()) return true;