From d9f8d4b642377ce2607fbdea726c26306a5fd9d6 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Sun, 11 Dec 2016 20:50:30 -0500 Subject: [PATCH] refactor to more use of auto c++11 keyword (#1404) refactor to more use of auto c++11 keyword --- include/libtorrent/enum_net.hpp | 8 +++---- include/libtorrent/tracker_manager.hpp | 7 ++---- src/choker.cpp | 21 ++++++----------- src/enum_net.cpp | 8 +++---- src/natpmp.cpp | 4 ++-- src/part_file.cpp | 12 +++++----- src/peer_class_set.cpp | 3 +-- src/peer_connection.cpp | 2 +- src/session_impl.cpp | 4 +--- src/stat_cache.cpp | 2 +- src/tracker_manager.cpp | 32 ++++++++++---------------- src/upnp.cpp | 2 +- src/ut_metadata.cpp | 4 ++-- 13 files changed, 44 insertions(+), 65 deletions(-) diff --git a/include/libtorrent/enum_net.hpp b/include/libtorrent/enum_net.hpp index 060ddebcd..d44f55707 100644 --- a/include/libtorrent/enum_net.hpp +++ b/include/libtorrent/enum_net.hpp @@ -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; } diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 0e4e2395c..4bb8debdc 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -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> ; - udp_conns_t m_udp_conns; + std::unordered_map> m_udp_conns; - using http_conns_t = std::vector>; - http_conns_t m_http_conns; + std::vector> m_http_conns; send_fun_t m_send_fun; send_fun_hostname_t m_send_fun_hostname; diff --git a/src/choker.cpp b/src/choker.cpp index ecec9e16a..71714a82b 100644 --- a/src/choker.cpp +++ b/src/choker.cpp @@ -265,11 +265,10 @@ namespace libtorrent , aux::session_settings const& sett) { #if TORRENT_USE_ASSERTS - for (std::vector::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::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::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::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; diff --git a/src/enum_net.cpp b/src/enum_net.cpp index d59b59f51..4f7d293bc 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -1149,8 +1149,8 @@ namespace libtorrent std::vector 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 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(); } } diff --git a/src/natpmp.cpp b/src/natpmp.cpp index 4a1abf49a..04d57778a 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -234,7 +234,7 @@ int natpmp::add_mapping(portmap_protocol const p, int const external_port if (m_disabled) return -1; - std::vector::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::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; }); diff --git a/src/part_file.cpp b/src/part_file.cpp index 04a83c708..2300186c5 100644 --- a/src/part_file.cpp +++ b/src/part_file.cpp @@ -180,7 +180,7 @@ namespace libtorrent if (ec) return -1; int slot = -1; - std::unordered_map::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 l(m_mutex); - std::unordered_map::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 l(m_mutex); - std::unordered_map::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::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::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::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; diff --git a/src/peer_class_set.cpp b/src/peer_class_set.cpp index 4fe3d019e..6c4d4ac0d 100644 --- a/src/peer_class_set.cpp +++ b/src/peer_class_set.cpp @@ -58,8 +58,7 @@ namespace libtorrent void peer_class_set::remove_class(peer_class_pool& pool, peer_class_t const c) { - std::array::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) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 93dc42f79..2ee16ad05 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2277,7 +2277,7 @@ namespace libtorrent } int fast_idx = -1; - std::vector::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()); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 8480af08c..09ba2b862 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -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::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()); diff --git a/src/stat_cache.cpp b/src/stat_cache.cpp index 97974ad87..91b263b1b 100644 --- a/src/stat_cache.cpp +++ b/src/stat_cache.cpp @@ -113,7 +113,7 @@ namespace libtorrent int stat_cache::add_error(error_code const& ec) { - std::vector::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; diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index 1de6ce736..772e519e1 100644 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -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 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 con - = std::make_shared( - std::ref(ios), std::ref(*this), std::cref(req), c); + auto con = std::make_shared(ios, *this, req, c); m_http_conns.push_back(con); con->start(); return; } else if (protocol == "udp") { - std::shared_ptr con - = std::make_shared( - std::ref(ios), std::ref(*this), std::cref(req) , c); + auto con = std::make_shared(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> close_http_connections; std::vector> 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 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 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) diff --git a/src/upnp.cpp b/src/upnp.cpp index 00585a513..3c03bbaea 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -762,7 +762,7 @@ void upnp::next(rootdevice& d, int i) } else { - std::vector::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; diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 4c0aae857..e41346f06 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -366,7 +366,7 @@ namespace libtorrent { namespace break; case metadata_piece: { - std::vector::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::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;