summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsbell <jsbell@chromium.org>2015-11-30 15:50:25 -0800
committerCommit bot <commit-bot@chromium.org>2015-11-30 23:51:29 +0000
commitcea42a57e3ffbe411d2afdd8c61a5219bafcacc7 (patch)
tree89274ce34dbebbb3c8c8b6fe9d4f701281a33a51
parent4268d807325d96016ad43dec4957da1b11bc71ed (diff)
downloadchromium_src-cea42a57e3ffbe411d2afdd8c61a5219bafcacc7.zip
chromium_src-cea42a57e3ffbe411d2afdd8c61a5219bafcacc7.tar.gz
chromium_src-cea42a57e3ffbe411d2afdd8c61a5219bafcacc7.tar.bz2
Use std::tie() for operator< in net/
Simplify the code for operator< when comparing multiple members using a common std::tie idiom. BUG=555171 Review URL: https://codereview.chromium.org/1463053002 Cr-Commit-Position: refs/heads/master@{#362276}
-rw-r--r--net/base/host_port_pair.h6
-rw-r--r--net/base/ip_endpoint.cc17
-rw-r--r--net/base/network_quality_estimator.h3
-rw-r--r--net/cert_net/cert_net_fetcher_impl.cc12
-rw-r--r--net/der/parse_values.cc16
-rw-r--r--net/dns/host_cache.h10
-rw-r--r--net/dns/host_resolver_impl_unittest.cc5
-rw-r--r--net/dns/mdns_cache.cc14
-rw-r--r--net/http/http_server_properties.h8
-rw-r--r--net/proxy/proxy_server.h7
-rw-r--r--net/quic/quic_server_id.cc10
-rw-r--r--net/spdy/spdy_session_key.cc12
12 files changed, 53 insertions, 67 deletions
diff --git a/net/base/host_port_pair.h b/net/base/host_port_pair.h
index 9b1df56..489b971 100644
--- a/net/base/host_port_pair.h
+++ b/net/base/host_port_pair.h
@@ -6,6 +6,8 @@
#define NET_BASE_HOST_PORT_PAIR_H_
#include <string>
+#include <tuple>
+
#include "base/basictypes.h"
#include "net/base/net_export.h"
@@ -34,9 +36,7 @@ class NET_EXPORT HostPortPair {
// TODO(willchan): Define a functor instead.
// Comparator function so this can be placed in a std::map.
bool operator<(const HostPortPair& other) const {
- if (port_ != other.port_)
- return port_ < other.port_;
- return host_ < other.host_;
+ return std::tie(port_, host_) < std::tie(other.port_, other.host_);
}
// Equality test of contents. (Probably another violation of style guide).
diff --git a/net/base/ip_endpoint.cc b/net/base/ip_endpoint.cc
index 7834849..f7f7b18 100644
--- a/net/base/ip_endpoint.cc
+++ b/net/base/ip_endpoint.cc
@@ -4,6 +4,8 @@
#include "net/base/ip_endpoint.h"
+#include <tuple>
+
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/sys_byteorder.h"
@@ -110,19 +112,16 @@ std::string IPEndPoint::ToStringWithoutPort() const {
return IPAddressToString(address_);
}
-bool IPEndPoint::operator<(const IPEndPoint& that) const {
+bool IPEndPoint::operator<(const IPEndPoint& other) const {
// Sort IPv4 before IPv6.
- if (address_.size() != that.address_.size()) {
- return address_.size() < that.address_.size();
- }
- if (address_ != that.address_) {
- return address_ < that.address_;
+ if (address_.size() != other.address_.size()) {
+ return address_.size() < other.address_.size();
}
- return port_ < that.port_;
+ return std::tie(address_, port_) < std::tie(other.address_, other.port_);
}
-bool IPEndPoint::operator==(const IPEndPoint& that) const {
- return address_ == that.address_ && port_ == that.port_;
+bool IPEndPoint::operator==(const IPEndPoint& other) const {
+ return address_ == other.address_ && port_ == other.port_;
}
} // namespace net
diff --git a/net/base/network_quality_estimator.h b/net/base/network_quality_estimator.h
index dc6b322..ea4eaa1 100644
--- a/net/base/network_quality_estimator.h
+++ b/net/base/network_quality_estimator.h
@@ -10,6 +10,7 @@
#include <deque>
#include <map>
#include <string>
+#include <tuple>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
@@ -201,7 +202,7 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
// Overloaded because NetworkID is used as key in a map.
bool operator<(const NetworkID& other) const {
- return type < other.type || (type == other.type && id < other.id);
+ return std::tie(type, id) < std::tie(other.type, other.id);
}
// Connection type of the network.
diff --git a/net/cert_net/cert_net_fetcher_impl.cc b/net/cert_net/cert_net_fetcher_impl.cc
index bf7623b..bec90b2 100644
--- a/net/cert_net/cert_net_fetcher_impl.cc
+++ b/net/cert_net/cert_net_fetcher_impl.cc
@@ -4,6 +4,8 @@
#include "net/cert_net/cert_net_fetcher_impl.h"
+#include <tuple>
+
#include "base/callback_helpers.h"
#include "base/containers/linked_list.h"
#include "base/logging.h"
@@ -133,13 +135,9 @@ CertNetFetcherImpl::RequestParams::RequestParams()
bool CertNetFetcherImpl::RequestParams::operator<(
const RequestParams& other) const {
- if (url != other.url)
- return url < other.url;
- if (http_method != other.http_method)
- return http_method < other.http_method;
- if (max_response_bytes != other.max_response_bytes)
- return max_response_bytes < other.max_response_bytes;
- return timeout < other.timeout;
+ return std::tie(url, http_method, max_response_bytes, timeout) <
+ std::tie(other.url, other.http_method, other.max_response_bytes,
+ other.timeout);
}
// CertNetFetcherImpl::Job tracks an outstanding URLRequest as well as all of
diff --git a/net/der/parse_values.cc b/net/der/parse_values.cc
index 8786f9d..a2df4b8a 100644
--- a/net/der/parse_values.cc
+++ b/net/der/parse_values.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <tuple>
+
#include "base/logging.h"
#include "base/numerics/safe_math.h"
#include "net/der/parse_values.h"
@@ -277,17 +279,9 @@ bool ParseBitString(const Input& in, BitString* out) {
}
bool operator<(const GeneralizedTime& lhs, const GeneralizedTime& rhs) {
- if (lhs.year != rhs.year)
- return lhs.year < rhs.year;
- if (lhs.month != rhs.month)
- return lhs.month < rhs.month;
- if (lhs.day != rhs.day)
- return lhs.day < rhs.day;
- if (lhs.hours != rhs.hours)
- return lhs.hours < rhs.hours;
- if (lhs.minutes != rhs.minutes)
- return lhs.minutes < rhs.minutes;
- return lhs.seconds < rhs.seconds;
+ return std::tie(lhs.year, lhs.month, lhs.day, lhs.hours, lhs.minutes,
+ lhs.seconds) < std::tie(rhs.year, rhs.month, rhs.day,
+ rhs.hours, rhs.minutes, rhs.seconds);
}
// A UTC Time in DER encoding should be YYMMDDHHMMSSZ, but some CAs encode
diff --git a/net/dns/host_cache.h b/net/dns/host_cache.h
index e8628fb..d9108c0 100644
--- a/net/dns/host_cache.h
+++ b/net/dns/host_cache.h
@@ -7,6 +7,7 @@
#include <functional>
#include <string>
+#include <tuple>
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
@@ -46,14 +47,13 @@ class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) {
host_resolver_flags(host_resolver_flags) {}
bool operator<(const Key& other) const {
+ // The order of comparisons of |Key| fields is arbitrary, thus
// |address_family| and |host_resolver_flags| are compared before
// |hostname| under assumption that integer comparisons are faster than
// string comparisons.
- if (address_family != other.address_family)
- return address_family < other.address_family;
- if (host_resolver_flags != other.host_resolver_flags)
- return host_resolver_flags < other.host_resolver_flags;
- return hostname < other.hostname;
+ return std::tie(address_family, host_resolver_flags, hostname) <
+ std::tie(other.address_family, other.host_resolver_flags,
+ other.hostname);
}
std::string hostname;
diff --git a/net/dns/host_resolver_impl_unittest.cc b/net/dns/host_resolver_impl_unittest.cc
index ac71749..3681180 100644
--- a/net/dns/host_resolver_impl_unittest.cc
+++ b/net/dns/host_resolver_impl_unittest.cc
@@ -6,6 +6,7 @@
#include <algorithm>
#include <string>
+#include <tuple>
#include "base/bind.h"
#include "base/bind_helpers.h"
@@ -61,8 +62,8 @@ class MockHostResolverProc : public HostResolverProc {
ResolveKey(const std::string& hostname, AddressFamily address_family)
: hostname(hostname), address_family(address_family) {}
bool operator<(const ResolveKey& other) const {
- return address_family < other.address_family ||
- (address_family == other.address_family && hostname < other.hostname);
+ return std::tie(address_family, hostname) <
+ std::tie(other.address_family, other.hostname);
}
std::string hostname;
AddressFamily address_family;
diff --git a/net/dns/mdns_cache.cc b/net/dns/mdns_cache.cc
index 010a34f..a55fdca 100644
--- a/net/dns/mdns_cache.cc
+++ b/net/dns/mdns_cache.cc
@@ -5,6 +5,7 @@
#include "net/dns/mdns_cache.h"
#include <algorithm>
+#include <tuple>
#include <utility>
#include "base/stl_util.h"
@@ -44,16 +45,9 @@ MDnsCache::Key& MDnsCache::Key::operator=(
MDnsCache::Key::~Key() {
}
-bool MDnsCache::Key::operator<(const MDnsCache::Key& key) const {
- if (name_ != key.name_)
- return name_ < key.name_;
-
- if (type_ != key.type_)
- return type_ < key.type_;
-
- if (optional_ != key.optional_)
- return optional_ < key.optional_;
- return false; // keys are equal
+bool MDnsCache::Key::operator<(const MDnsCache::Key& other) const {
+ return std::tie(name_, type_, optional_) <
+ std::tie(other.name_, other.type_, other.optional_);
}
bool MDnsCache::Key::operator==(const MDnsCache::Key& key) const {
diff --git a/net/http/http_server_properties.h b/net/http/http_server_properties.h
index 1df4759..983828a 100644
--- a/net/http/http_server_properties.h
+++ b/net/http/http_server_properties.h
@@ -7,6 +7,7 @@
#include <map>
#include <string>
+#include <tuple>
#include <vector>
#include "base/basictypes.h"
@@ -125,11 +126,8 @@ struct NET_EXPORT AlternativeService {
}
bool operator<(const AlternativeService& other) const {
- if (protocol != other.protocol)
- return protocol < other.protocol;
- if (host != other.host)
- return host < other.host;
- return port < other.port;
+ return std::tie(protocol, host, port) <
+ std::tie(other.protocol, other.host, other.port);
}
std::string ToString() const;
diff --git a/net/proxy/proxy_server.h b/net/proxy/proxy_server.h
index 1ff6536..12ec217 100644
--- a/net/proxy/proxy_server.h
+++ b/net/proxy/proxy_server.h
@@ -12,6 +12,8 @@
#endif
#include <string>
+#include <tuple>
+
#include "net/base/host_port_pair.h"
#include "net/base/net_export.h"
@@ -149,9 +151,8 @@ class NET_EXPORT ProxyServer {
// Comparator function so this can be placed in a std::map.
bool operator<(const ProxyServer& other) const {
- if (scheme_ != other.scheme_)
- return scheme_ < other.scheme_;
- return host_port_pair_ < other.host_port_pair_;
+ return std::tie(scheme_, host_port_pair_) <
+ std::tie(other.scheme_, other.host_port_pair_);
}
private:
diff --git a/net/quic/quic_server_id.cc b/net/quic/quic_server_id.cc
index 822c1b0..25bac39 100644
--- a/net/quic/quic_server_id.cc
+++ b/net/quic/quic_server_id.cc
@@ -3,8 +3,10 @@
// found in the LICENSE file.
#include "net/quic/quic_server_id.h"
-#include "base/logging.h"
+#include <tuple>
+
+#include "base/logging.h"
#include "net/base/host_port_pair.h"
#include "net/base/port_util.h"
#include "url/gurl.h"
@@ -32,10 +34,8 @@ QuicServerId::QuicServerId(const string& host,
QuicServerId::~QuicServerId() {}
bool QuicServerId::operator<(const QuicServerId& other) const {
- if (!host_port_pair_.Equals(other.host_port_pair_)) {
- return host_port_pair_ < other.host_port_pair_;
- }
- return privacy_mode_ < other.privacy_mode_;
+ return std::tie(host_port_pair_, privacy_mode_) <
+ std::tie(other.host_port_pair_, other.privacy_mode_);
}
bool QuicServerId::operator==(const QuicServerId& other) const {
diff --git a/net/spdy/spdy_session_key.cc b/net/spdy/spdy_session_key.cc
index 3ef95de..59f36df 100644
--- a/net/spdy/spdy_session_key.cc
+++ b/net/spdy/spdy_session_key.cc
@@ -4,6 +4,8 @@
#include "net/spdy/spdy_session_key.h"
+#include <tuple>
+
#include "base/logging.h"
namespace net {
@@ -33,11 +35,10 @@ SpdySessionKey::SpdySessionKey(const HostPortProxyPair& host_port_proxy_pair,
SpdySessionKey::~SpdySessionKey() {}
bool SpdySessionKey::operator<(const SpdySessionKey& other) const {
- if (privacy_mode_ != other.privacy_mode_)
- return privacy_mode_ < other.privacy_mode_;
- if (!host_port_proxy_pair_.first.Equals(other.host_port_proxy_pair_.first))
- return host_port_proxy_pair_.first < other.host_port_proxy_pair_.first;
- return host_port_proxy_pair_.second < other.host_port_proxy_pair_.second;
+ return std::tie(privacy_mode_, host_port_proxy_pair_.first,
+ host_port_proxy_pair_.second) <
+ std::tie(other.privacy_mode_, other.host_port_proxy_pair_.first,
+ other.host_port_proxy_pair_.second);
}
bool SpdySessionKey::Equals(const SpdySessionKey& other) const {
@@ -47,4 +48,3 @@ bool SpdySessionKey::Equals(const SpdySessionKey& other) const {
}
} // namespace net
-