Skip to content

nsapi - Add equality operators to SocketAddress class #2665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions features/net/network-socket/SocketAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ SocketAddress::operator bool() const
return false;
}

bool operator==(const SocketAddress &a, const SocketAddress &b)
{
int count = 0;
if (a._addr.version == NSAPI_IPv4 && b._addr.version == NSAPI_IPv4) {
count = NSAPI_IPv4_BYTES;
} else if (a._addr.version == NSAPI_IPv6 && b._addr.version == NSAPI_IPv6) {
count = NSAPI_IPv6_BYTES;
} else {
return false;
}

return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0);
}

bool operator!=(const SocketAddress &a, const SocketAddress &b)
{
return !(a == b);
}

void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
{
_ip_address[0] = '\0';
Expand Down
12 changes: 12 additions & 0 deletions features/net/network-socket/SocketAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ class SocketAddress {
*/
operator bool() const;

/** Compare two addresses for equality
*
* @return True if both addresses are equal
*/
friend bool operator==(const SocketAddress &a, const SocketAddress &b);

/** Compare two addresses for equality
*
* @return True if both addresses are not equal
*/
friend bool operator!=(const SocketAddress &a, const SocketAddress &b);

private:
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);

Expand Down