Use c++11 features in webrtc/base/network.cc as a test to see if we can use them.

R=kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/25209005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7777 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
pthatcher@webrtc.org
2014-12-01 23:18:27 +00:00
parent 146e0fd30f
commit 5950b645b9

View File

@ -140,9 +140,8 @@ NetworkManagerBase::NetworkManagerBase() : ipv6_enabled_(true) {
} }
NetworkManagerBase::~NetworkManagerBase() { NetworkManagerBase::~NetworkManagerBase() {
for (NetworkMap::iterator i = networks_map_.begin(); for (const auto& kv : networks_map_) {
i != networks_map_.end(); ++i) { delete kv.second;
delete i->second;
} }
} }
@ -167,49 +166,44 @@ void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
*changed = true; *changed = true;
// First, build a set of network-keys to the ipaddresses. // First, build a set of network-keys to the ipaddresses.
for (uint32 i = 0; i < list.size(); ++i) { for (Network* network : list) {
bool might_add_to_merged_list = false; bool might_add_to_merged_list = false;
std::string key = MakeNetworkKey(list[i]->name(), std::string key = MakeNetworkKey(network->name(),
list[i]->prefix(), network->prefix(),
list[i]->prefix_length()); network->prefix_length());
if (consolidated_address_list.find(key) == if (consolidated_address_list.find(key) ==
consolidated_address_list.end()) { consolidated_address_list.end()) {
AddressList addrlist; AddressList addrlist;
addrlist.net = list[i]; addrlist.net = network;
consolidated_address_list[key] = addrlist; consolidated_address_list[key] = addrlist;
might_add_to_merged_list = true; might_add_to_merged_list = true;
} }
const std::vector<InterfaceAddress>& addresses = list[i]->GetIPs(); const std::vector<InterfaceAddress>& addresses = network->GetIPs();
AddressList& current_list = consolidated_address_list[key]; AddressList& current_list = consolidated_address_list[key];
for (std::vector<InterfaceAddress>::const_iterator it = addresses.begin(); for (const InterfaceAddress& address : addresses) {
it != addresses.end(); current_list.ips.push_back(address);
++it) {
current_list.ips.push_back(*it);
} }
if (!might_add_to_merged_list) { if (!might_add_to_merged_list) {
delete list[i]; delete network;
} }
} }
// Next, look for existing network objects to re-use. // Next, look for existing network objects to re-use.
for (std::map<std::string, AddressList>::iterator it = for (const auto& kv : consolidated_address_list) {
consolidated_address_list.begin(); const std::string& key = kv.first;
it != consolidated_address_list.end(); Network* net = kv.second.net;
++it) { auto existing = networks_map_.find(key);
const std::string& key = it->first;
Network* net = it->second.net;
NetworkMap::iterator existing = networks_map_.find(key);
if (existing == networks_map_.end()) { if (existing == networks_map_.end()) {
// This network is new. Place it in the network map. // This network is new. Place it in the network map.
merged_list.push_back(net); merged_list.push_back(net);
networks_map_[key] = net; networks_map_[key] = net;
// Also, we might have accumulated IPAddresses from the first // Also, we might have accumulated IPAddresses from the first
// step, set it here. // step, set it here.
net->SetIPs(it->second.ips, true); net->SetIPs(kv.second.ips, true);
*changed = true; *changed = true;
} else { } else {
// This network exists in the map already. Reset its IP addresses. // This network exists in the map already. Reset its IP addresses.
*changed = existing->second->SetIPs(it->second.ips, *changed); *changed = existing->second->SetIPs(kv.second.ips, *changed);
merged_list.push_back(existing->second); merged_list.push_back(existing->second);
if (existing->second != net) { if (existing->second != net) {
delete net; delete net;
@ -229,9 +223,8 @@ void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
// requirements, we will just assign a preference value starting with 127, // requirements, we will just assign a preference value starting with 127,
// in decreasing order. // in decreasing order.
int pref = kHighestNetworkPreference; int pref = kHighestNetworkPreference;
for (NetworkList::const_iterator iter = networks_.begin(); for (Network* network : networks_) {
iter != networks_.end(); ++iter) { network->set_preference(pref);
(*iter)->set_preference(pref);
if (pref > 0) { if (pref > 0) {
--pref; --pref;
} else { } else {
@ -305,7 +298,7 @@ void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces,
prefix = TruncateIP(ip, prefix_length); prefix = TruncateIP(ip, prefix_length);
std::string key = MakeNetworkKey(std::string(cursor->ifa_name), std::string key = MakeNetworkKey(std::string(cursor->ifa_name),
prefix, prefix_length); prefix, prefix_length);
NetworkMap::iterator existing_network = current_networks.find(key); auto existing_network = current_networks.find(key);
if (existing_network == current_networks.end()) { if (existing_network == current_networks.end()) {
scoped_ptr<Network> network(new Network(cursor->ifa_name, scoped_ptr<Network> network(new Network(cursor->ifa_name,
cursor->ifa_name, cursor->ifa_name,
@ -451,7 +444,7 @@ bool BasicNetworkManager::CreateNetworks(bool include_ignored,
IPAddress prefix; IPAddress prefix;
int prefix_length = GetPrefix(prefixlist, ip, &prefix); int prefix_length = GetPrefix(prefixlist, ip, &prefix);
std::string key = MakeNetworkKey(name, prefix, prefix_length); std::string key = MakeNetworkKey(name, prefix, prefix_length);
NetworkMap::iterator existing_network = current_networks.find(key); auto existing_network = current_networks.find(key);
if (existing_network == current_networks.end()) { if (existing_network == current_networks.end()) {
scoped_ptr<Network> network(new Network(name, scoped_ptr<Network> network(new Network(name,
description, description,
@ -508,8 +501,8 @@ bool IsDefaultRoute(const std::string& network_name) {
bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const { bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const {
// Ignore networks on the explicit ignore list. // Ignore networks on the explicit ignore list.
for (size_t i = 0; i < network_ignore_list_.size(); ++i) { for (const std::string& ignored_name : network_ignore_list_) {
if (network.name() == network_ignore_list_[i]) { if (network.name() == ignored_name) {
return true; return true;
} }
} }
@ -608,8 +601,7 @@ void BasicNetworkManager::DumpNetworks(bool include_ignored) {
NetworkList list; NetworkList list;
CreateNetworks(include_ignored, &list); CreateNetworks(include_ignored, &list);
LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:"; LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:";
for (size_t i = 0; i < list.size(); ++i) { for (const Network* network : list) {
const Network* network = list[i];
if (!network->ignored() || include_ignored) { if (!network->ignored() || include_ignored) {
LOG(LS_INFO) << network->ToString() << ": " LOG(LS_INFO) << network->ToString() << ": "
<< network->description() << network->description()
@ -618,8 +610,8 @@ void BasicNetworkManager::DumpNetworks(bool include_ignored) {
} }
// Release the network list created previously. // Release the network list created previously.
// Do this in a seperated for loop for better readability. // Do this in a seperated for loop for better readability.
for (size_t i = 0; i < list.size(); ++i) { for (Network* network : list) {
delete list[i]; delete network;
} }
} }
@ -642,22 +634,18 @@ Network::Network(const std::string& name, const std::string& desc,
// Sets the addresses of this network. Returns true if the address set changed. // Sets the addresses of this network. Returns true if the address set changed.
// Change detection is short circuited if the changed argument is true. // Change detection is short circuited if the changed argument is true.
bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) { bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) {
changed = changed || ips.size() != ips_.size();
// Detect changes with a nested loop; n-squared but we expect on the order // Detect changes with a nested loop; n-squared but we expect on the order
// of 2-3 addresses per network. // of 2-3 addresses per network.
for (std::vector<InterfaceAddress>::const_iterator it = ips.begin(); changed = changed || ips.size() != ips_.size();
!changed && it != ips.end(); if (!changed) {
++it) { for (const InterfaceAddress& ip : ips) {
bool found = false; if (std::find(ips_.begin(), ips_.end(), ip) == ips_.end()) {
for (std::vector<InterfaceAddress>::iterator inner_it = ips_.begin(); changed = true;
!found && inner_it != ips_.end(); break;
++inner_it) {
if (*it == *inner_it) {
found = true;
} }
} }
changed = !found;
} }
ips_ = ips; ips_ = ips;
return changed; return changed;
} }
@ -674,21 +662,21 @@ IPAddress Network::GetBestIP() const {
InterfaceAddress selected_ip, ula_ip; InterfaceAddress selected_ip, ula_ip;
for (size_t i = 0; i < ips_.size(); i++) { for (const InterfaceAddress& ip : ips_) {
// Ignore any address which has been deprecated already. // Ignore any address which has been deprecated already.
if (ips_[i].ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED)
continue; continue;
// ULA address should only be returned when we have no other // ULA address should only be returned when we have no other
// global IP. // global IP.
if (IPIsULA(static_cast<const IPAddress&>(ips_[i]))) { if (IPIsULA(static_cast<const IPAddress&>(ip))) {
ula_ip = ips_[i]; ula_ip = ip;
continue; continue;
} }
selected_ip = ips_[i]; selected_ip = ip;
// Search could stop once a temporary non-deprecated one is found. // Search could stop once a temporary non-deprecated one is found.
if (ips_[i].ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY) if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY)
break; break;
} }