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

View File

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

View File

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

View File

@ -1149,8 +1149,8 @@ namespace libtorrent
std::vector<ip_interface> ifs = enum_net_interfaces(ios, ec);
if (ec) return false;
for (int i = 0; i < int(ifs.size()); ++i)
if (ifs[i].name == name) return true;
for (auto const& iface : ifs)
if (iface.name == name) return true;
return false;
}
@ -1161,8 +1161,8 @@ namespace libtorrent
std::vector<ip_interface> ifs = enum_net_interfaces(ios, ec);
if (ec) return std::string();
for (int i = 0; i < int(ifs.size()); ++i)
if (ifs[i].interface_address == addr) return ifs[i].name;
for (auto const& iface : ifs)
if (iface.interface_address == addr) return iface.name;
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;
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; });
if (i == m_mappings.end())
{
@ -275,7 +275,7 @@ void natpmp::try_next_mapping(int const i)
return;
}
std::vector<mapping_t>::iterator const m = std::find_if(
auto const m = std::find_if(
m_mappings.begin(), m_mappings.end()
, [] (mapping_t const& ma) { return ma.act != mapping_t::action::none; });

View File

@ -180,7 +180,7 @@ namespace libtorrent
if (ec) return -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())
slot = allocate_slot(piece);
else
@ -198,7 +198,7 @@ namespace libtorrent
TORRENT_ASSERT(offset >= 0);
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())
{
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::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;
// 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;
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));
if (i != m_piece_map.end())
{
@ -345,7 +345,7 @@ namespace libtorrent
// another thread removed this slot map entry, and invalidated
// our iterator. Now that we hold the lock again, perform
// 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 the slot moved, that's really suspicious
@ -404,7 +404,7 @@ namespace libtorrent
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;
if (i != m_piece_map.end())
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)
{
std::array<peer_class_t, 15>::iterator i = std::find(m_class.begin()
, m_class.begin() + m_size, c);
auto const i = std::find(m_class.begin(), m_class.begin() + m_size, c);
int idx = int(i - m_class.begin());
if (idx == m_size) return; // not found
if (idx < m_size - 1)

View File

@ -2277,7 +2277,7 @@ namespace libtorrent
}
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);
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
// all the other ones.
for (std::vector<peer_connection*>::iterator i = peers.begin()
, end(peers.end()); i != end; ++i)
for (auto p : peers)
{
peer_connection* p = *i;
TORRENT_ASSERT(p);
TORRENT_ASSERT(!p->ignore_unchoke_slots());

View File

@ -113,7 +113,7 @@ namespace libtorrent
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());
m_errors.push_back(ec);
return int(m_errors.size()) - 1;

View File

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

View File

@ -762,7 +762,7 @@ void upnp::next(rootdevice& d, int i)
}
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; });
if (j == d.mapping.end()) return;

View File

@ -366,7 +366,7 @@ namespace libtorrent { namespace
break;
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);
// unwanted piece?
@ -389,7 +389,7 @@ namespace libtorrent { namespace
case metadata_dont_have:
{
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);
// unwanted piece?
if (i == m_sent_requests.end()) return true;