From cbc469bf420ad60a72cd7225941508a450a0f92b Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 26 Jan 2013 17:23:39 -0500 Subject: [PATCH] Added comparison operators for network_address. --- dlib/sockets/sockets_extensions.h | 29 +++++++++++++++ dlib/sockets/sockets_extensions_abstract.h | 43 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/dlib/sockets/sockets_extensions.h b/dlib/sockets/sockets_extensions.h index b07421d93..3683bac03 100644 --- a/dlib/sockets/sockets_extensions.h +++ b/dlib/sockets/sockets_extensions.h @@ -46,6 +46,35 @@ namespace dlib unsigned short port; }; +// ---------------------------------------------------------------------------------------- + + inline bool operator < ( + const network_address& a, + const network_address& b + ) + { + if (a.host_address < b.host_address) + return true; + else if (a.host_address > b.host_address) + return false; + else if (a.port < b.port) + return true; + else + return false; + } + + inline bool operator== ( + const network_address& a, + const network_address& b + ) { return a.host_address == b.host_address && a.port == b.port; } + + inline bool operator != ( + const network_address& a, + const network_address& b + ) { return !(a == b); } + +// ---------------------------------------------------------------------------------------- + void serialize( const network_address& item, std::ostream& out diff --git a/dlib/sockets/sockets_extensions_abstract.h b/dlib/sockets/sockets_extensions_abstract.h index 607657da6..f40ac705a 100644 --- a/dlib/sockets/sockets_extensions_abstract.h +++ b/dlib/sockets/sockets_extensions_abstract.h @@ -86,6 +86,49 @@ namespace dlib unsigned short port; }; +// ---------------------------------------------------------------------------------------- + + inline bool operator < ( + const network_address& a, + const network_address& b + ); + /*! + ensures + - provides a total ordering over network_address objects so you can use them in + the standard associative containers. The ordering is defined such that if + you sorted network addresses they would sort first on the host_address string + and then, for network_address objects with equal host_address, they would + sort on the port number + !*/ + + inline bool operator== ( + const network_address& a, + const network_address& b + ); + /*! + ensures + - returns true if a and b contain exactly the same address and false otherwise. + That is, the following must be true for this function to return true: + - a.host_address == b.host_address + - a.port == b.port + Note that this means that two addresses which are logically equivalent but + written differently will not compare equal. For example, suppose example.com + has the IP address 10.1.1.1. Then network_address("10.1.1.1:80") and + network_address("example.com:80") really refer to the same network resource + but will nevertheless not compare equal since. + !*/ + + inline bool operator != ( + const network_address& a, + const network_address& b + ); + /*! + ensures + - returns !(a == b) + !*/ + +// ---------------------------------------------------------------------------------------- + void serialize( const network_address& item, std::ostream& out