Refactor and fix some object arithmetic comparisons

This commit is contained in:
Vinnie Falco
2013-11-30 06:27:54 -08:00
parent 76c364ec2d
commit 130c7c5c58
6 changed files with 112 additions and 74 deletions

View File

@@ -334,7 +334,6 @@ private:
/** Comparison. */
/** @{ */
int compare (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
bool operator== (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
bool operator!= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
bool operator< (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
@@ -342,7 +341,6 @@ bool operator<= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
bool operator> (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
bool operator>= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs);
int compare (IPAddress const& lhs, IPAddress const& rhs);
bool operator== (IPAddress const& lhs, IPAddress const& rhs);
bool operator!= (IPAddress const& lhs, IPAddress const& rhs);
bool operator< (IPAddress const& lhs, IPAddress const& rhs);

View File

@@ -554,58 +554,71 @@ IPAddress IPAddress::from_string_altform (std::string const& s)
//------------------------------------------------------------------------------
int compare (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
bool operator== (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
{ return lhs.value == rhs.value; }
bool operator< (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
{ return lhs.value < rhs.value; }
bool operator!= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
{ return ! (lhs == rhs); }
bool operator> (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
{ return rhs < lhs; }
bool operator<= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
{ return ! (rhs < lhs); }
bool operator>= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs)
{ return ! (lhs < rhs); }
//------------------------------------------------------------------------------
bool operator== (IPAddress const& lhs, IPAddress const& rhs)
{
if (lhs.value < rhs.value)
return -1;
else if (lhs.value > rhs.value)
return 1;
return 0;
}
bool operator== (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs) { return compare (lhs, rhs) == 0; }
bool operator!= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs) { return compare (lhs, rhs) != 0; }
bool operator< (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs) { return compare (lhs, rhs) < 0; }
bool operator<= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs) { return compare (lhs, rhs) <= 0; }
bool operator> (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs) { return compare (lhs, rhs) > 0; }
bool operator>= (IPAddress::V4 const& lhs, IPAddress::V4 const& rhs) { return compare (lhs, rhs) >= 0; }
static int type_compare (IPAddress const& lhs, IPAddress const& rhs)
{
if (lhs.type() < rhs.type())
return -1;
else if (lhs.type() > rhs.type())
return 1;
return 0;
}
int compare (IPAddress const& lhs, IPAddress const& rhs)
{
int const tc (type_compare (lhs, rhs));
if (tc < 0)
return -1;
else if (tc > 0)
return 1;
if (lhs.type() != rhs.type())
return false;
switch (lhs.type())
{
case IPAddress::none: return 0;
case IPAddress::ipv4: return compare (lhs.v4(), rhs.v4());
default:
case IPAddress::none: return true;
case IPAddress::ipv4: return lhs.v4() == rhs.v4();
case IPAddress::ipv6:
break;
};
bassertfalse;
return 0;
default:
bassertfalse;
}
return false;
}
bool operator== (IPAddress const& lhs, IPAddress const& rhs) { return compare (lhs, rhs) == 0; }
bool operator!= (IPAddress const& lhs, IPAddress const& rhs) { return compare (lhs, rhs) != 0; }
bool operator< (IPAddress const& lhs, IPAddress const& rhs) { return compare (lhs, rhs) < 0; }
bool operator<= (IPAddress const& lhs, IPAddress const& rhs) { return compare (lhs, rhs) <= 0; }
bool operator> (IPAddress const& lhs, IPAddress const& rhs) { return compare (lhs, rhs) > 0; }
bool operator>= (IPAddress const& lhs, IPAddress const& rhs) { return compare (lhs, rhs) >= 0; }
bool operator< (IPAddress const& lhs, IPAddress const& rhs)
{
if (lhs.type() > rhs.type())
return false;
if (lhs.type() < rhs.type())
return true;
switch (lhs.type())
{
case IPAddress::none: return true;
case IPAddress::ipv4: return lhs.v4() < rhs.v4();
case IPAddress::ipv6:
default:
bassertfalse;
}
return false;
}
bool operator!= (IPAddress const& lhs, IPAddress const& rhs)
{ return ! (lhs == rhs); }
bool operator> (IPAddress const& lhs, IPAddress const& rhs)
{ return rhs < lhs; }
bool operator<= (IPAddress const& lhs, IPAddress const& rhs)
{ return ! (rhs < lhs); }
bool operator>= (IPAddress const& lhs, IPAddress const& rhs)
{ return ! (lhs < rhs); }
//------------------------------------------------------------------------------
std::ostream& operator<< (std::ostream &os, IPAddress::V4 const& addr)
{

View File

@@ -47,7 +47,6 @@ struct Port
SSLContext* context;
};
int compare (Port const& lhs, Port const& rhs);
bool operator== (Port const& lhs, Port const& rhs);
bool operator!= (Port const& lhs, Port const& rhs);
bool operator< (Port const& lhs, Port const& rhs);

View File

@@ -56,35 +56,50 @@ Port::Port (
{
}
int compare (Port const& lhs, Port const& rhs)
bool operator== (Port const& lhs, Port const& rhs)
{
int comp;
comp = compare (lhs.addr, rhs.addr);
if (comp != 0)
return comp;
if (lhs.port < rhs.port)
return -1;
else if (lhs.port > rhs.port)
return 1;
if (lhs.security < rhs.security)
return -1;
else if (lhs.security > rhs.security)
return 1;
if (lhs.addr != rhs.addr)
return false;
if (lhs.port != rhs.port)
return false;
if (lhs.security != rhs.security)
return false;
// 'context' does not participate in the comparison
return 0;
return true;
}
bool operator== (Port const& lhs, Port const& rhs) { return compare (lhs, rhs) == 0; }
bool operator!= (Port const& lhs, Port const& rhs) { return compare (lhs, rhs) != 0; }
bool operator< (Port const& lhs, Port const& rhs) { return compare (lhs, rhs) < 0; }
bool operator<= (Port const& lhs, Port const& rhs) { return compare (lhs, rhs) <= 0; }
bool operator> (Port const& lhs, Port const& rhs) { return compare (lhs, rhs) > 0; }
bool operator>= (Port const& lhs, Port const& rhs) { return compare (lhs, rhs) >= 0; }
bool operator< (Port const& lhs, Port const& rhs)
{
if (lhs.addr > rhs.addr)
return false;
else if (lhs.addr < rhs.addr)
return true;
if (lhs.port > rhs.port)
return false;
else if (lhs.port < rhs.port)
return true;
if (lhs.security > rhs.security)
return false;
else if (lhs.security < rhs.security)
return true;
return true;
}
bool operator!= (Port const& lhs, Port const& rhs)
{ return ! (lhs == rhs); }
bool operator> (Port const& lhs, Port const& rhs)
{ return rhs < lhs; }
bool operator<= (Port const& lhs, Port const& rhs)
{ return ! (rhs < lhs); }
bool operator>= (Port const& lhs, Port const& rhs)
{ return ! (lhs < rhs); }
}
}

View File

@@ -123,6 +123,16 @@ void ServerImpl::remove (Door& door)
//
// Thread
//
//--------------------------------------------------------------------------
int ServerImpl::compare (Port const& lhs, Port const& rhs)
{
if (lhs < rhs)
return -1;
else if (lhs > rhs)
return 1;
return 0;
}
// Updates our Door list based on settings.
//

View File

@@ -72,8 +72,11 @@ public:
void handle_update ();
void update ();
void run ();
static int compare (Port const& lhs, Port const& rhs);
};
}
}