IPEndpoint comparisons

This commit is contained in:
Vinnie Falco
2013-09-21 20:45:25 -07:00
parent 9d9c822efb
commit 56496d8287
2 changed files with 106 additions and 49 deletions

View File

@@ -195,6 +195,16 @@ IPEndpoint& IPEndpoint::operator= (IPEndpoint const& other)
return *this;
}
IPEndpoint IPEndpoint::from_string (std::string const& s)
{
std::stringstream ss (s);
IPEndpoint ep;
ss >> ep;
if (! ss.fail())
return ep;
return IPEndpoint();
}
IPEndpoint& IPEndpoint::operator= (V4 const& address)
{
m_type = ipv4;
@@ -490,6 +500,79 @@ std::istream& operator>> (std::istream &is, IPEndpoint& ep)
//------------------------------------------------------------------------------
int compare (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs)
{
if (lhs.value < rhs.value)
return -1;
else if (lhs.value > rhs.value)
return 1;
return 0;
}
bool operator== (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { return compare (lhs, rhs) == 0; }
bool operator!= (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { return compare (lhs, rhs) != 0; }
bool operator< (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { return compare (lhs, rhs) < 0; }
bool operator<= (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { return compare (lhs, rhs) <= 0; }
bool operator> (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { return compare (lhs, rhs) > 0; }
bool operator>= (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { return compare (lhs, rhs) >= 0; }
static int type_compare (IPEndpoint const& lhs, IPEndpoint const& rhs)
{
if (lhs.type() < rhs.type())
return -1;
else if (lhs.type() > rhs.type())
return 1;
return 0;
}
int compare (IPEndpoint const& lhs, IPEndpoint const& rhs)
{
int const tc (type_compare (lhs, rhs));
if (tc < 0)
return -1;
else if (tc > 0)
return 1;
switch (lhs.type())
{
case IPEndpoint::none: return 0;
case IPEndpoint::ipv4: return compare (lhs.v4(), rhs.v4());
default:
case IPEndpoint::ipv6:
break;
};
bassertfalse;
return 0;
}
bool operator== (IPEndpoint const& lhs, IPEndpoint const& rhs) { return compare (lhs, rhs) == 0; }
bool operator!= (IPEndpoint const& lhs, IPEndpoint const& rhs) { return compare (lhs, rhs) != 0; }
bool operator< (IPEndpoint const& lhs, IPEndpoint const& rhs) { return compare (lhs, rhs) < 0; }
bool operator<= (IPEndpoint const& lhs, IPEndpoint const& rhs) { return compare (lhs, rhs) <= 0; }
bool operator> (IPEndpoint const& lhs, IPEndpoint const& rhs) { return compare (lhs, rhs) > 0; }
bool operator>= (IPEndpoint const& lhs, IPEndpoint const& rhs) { return compare (lhs, rhs) >= 0; }
std::ostream& operator<< (std::ostream &os, IPEndpoint::V4 const& addr)
{
os << addr.to_string();
return os;
}
std::ostream& operator<< (std::ostream &os, IPEndpoint::V6 const& addr)
{
os << addr.to_string();
return os;
}
std::ostream& operator<< (std::ostream &os, IPEndpoint const& ep)
{
os << ep.to_string();
return os;
}
//------------------------------------------------------------------------------
class IPEndpointTests : public UnitTest
{
public: