changeset 383:403fa2642e19

Socket: add operator on addresses
author David Demelier <markand@malikania.fr>
date Tue, 23 Jun 2015 14:47:42 +0200
parents 4b08afed634d
children 2ce3d1578339
files C++/modules/Socket/SocketAddress.cpp C++/modules/Socket/SocketAddress.h C++/tests/Socket/main.cpp
diffstat 3 files changed, 72 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/C++/modules/Socket/SocketAddress.cpp	Tue Jun 23 14:21:47 2015 +0200
+++ b/C++/modules/Socket/SocketAddress.cpp	Tue Jun 23 14:47:42 2015 +0200
@@ -94,6 +94,28 @@
 	};
 }
 
+bool operator==(const Ip &ip1, const Ip &ip2) noexcept
+{
+	const char *addr1 = reinterpret_cast<const char *>(&ip1.address());
+	const char *addr2 = reinterpret_cast<const char *>(&ip2.address());
+
+	return std::equal(
+		addr1, addr1 + ip1.length(),
+		addr2, addr2 + ip2.length()
+	);
+}
+
+bool operator<(const Ip &ip1, const Ip &ip2) noexcept
+{
+	const char *addr1 = reinterpret_cast<const char *>(&ip1.address());
+	const char *addr2 = reinterpret_cast<const char *>(&ip2.address());
+
+	return std::lexicographical_compare(
+		addr1, addr1 + ip1.length(),
+		addr2, addr2 + ip2.length()
+	);
+}
+
 /* --------------------------------------------------------
  * Unix implementation
  * -------------------------------------------------------- */
@@ -133,6 +155,28 @@
 	};
 }
 
+bool operator==(const Unix &unix1, const Unix &unix2) noexcept
+{
+	const char *addr1 = reinterpret_cast<const char *>(&unix1.address());
+	const char *addr2 = reinterpret_cast<const char *>(&unix2.address());
+
+	return std::equal(
+		addr1, addr1 + unix1.length(),
+		addr2, addr2 + unix2.length()
+	);
+}
+
+bool operator<(const Unix &unix1, const Unix &unix2) noexcept
+{
+	const char *addr1 = reinterpret_cast<const char *>(&unix1.address());
+	const char *addr2 = reinterpret_cast<const char *>(&unix1.address());
+
+	return std::lexicographical_compare(
+		addr1, addr1 + unix1.length(),
+		addr2, addr2 + unix2.length()
+	);
+}
+
 #endif // _WIN32
 
 } // !address
--- a/C++/modules/Socket/SocketAddress.h	Tue Jun 23 14:21:47 2015 +0200
+++ b/C++/modules/Socket/SocketAddress.h	Tue Jun 23 14:47:42 2015 +0200
@@ -169,6 +169,10 @@
 	SocketAddressInfo info() const;
 };
 
+bool operator==(const Ip &ip1, const Ip &ip2) noexcept;
+
+bool operator<(const Ip &ip1, const Ip &ip2) noexcept;
+
 /**
  * @class Ipv6
  * @brief Convenient helper for IPv6 protocol
@@ -274,6 +278,10 @@
 	SocketAddressInfo info() const;
 };
 
+bool operator==(const Unix &unix1, const Unix &unix2) noexcept;
+
+bool operator<(const Unix &unix1, const Unix &unix2) noexcept;
+
 #endif // !_WIN32
 
 } // !address
--- a/C++/tests/Socket/main.cpp	Tue Jun 23 14:21:47 2015 +0200
+++ b/C++/tests/Socket/main.cpp	Tue Jun 23 14:47:42 2015 +0200
@@ -610,6 +610,26 @@
 
 #endif
 
+/* --------------------------------------------------------
+ * Operators
+ * -------------------------------------------------------- */
+
+TEST(AddressOperator, less)
+{
+	Ipv4 ip1{"*", 8000};
+	Ipv4 ip2{"*", 8002};
+
+	ASSERT_LT(ip1, ip2);
+}
+
+TEST(AddressOperator, same)
+{
+	Ipv4 ip1{"*", 8000};
+	Ipv4 ip2{"*", 8000};
+
+	ASSERT_EQ(ip1, ip2);
+}
+
 int main(int argc, char **argv)
 {
 	testing::InitGoogleTest(&argc, argv);