Skip to content

Commit e3037ef

Browse files
committed
nsapi - Added equality operators to SocketAddress class
1 parent da3b07d commit e3037ef

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

features/net/network-socket/SocketAddress.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ SocketAddress::operator bool() const
258258
return false;
259259
}
260260

261+
bool operator==(const SocketAddress &a, const SocketAddress &b)
262+
{
263+
int count = 0;
264+
if (a._addr.version == NSAPI_IPv4 && b._addr.version == NSAPI_IPv4) {
265+
count = NSAPI_IPv4_BYTES;
266+
} else if (a._addr.version == NSAPI_IPv6 && b._addr.version == NSAPI_IPv6) {
267+
count = NSAPI_IPv6_BYTES;
268+
} else {
269+
return false;
270+
}
271+
272+
return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0);
273+
}
274+
275+
bool operator!=(const SocketAddress &a, const SocketAddress &b)
276+
{
277+
return !(a == b);
278+
}
279+
261280
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
262281
{
263282
_ip_address[0] = '\0';

features/net/network-socket/SocketAddress.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ class SocketAddress {
137137
*/
138138
operator bool() const;
139139

140+
/** Compare two addresses for equality
141+
*
142+
* @return True if both addresses are equal
143+
*/
144+
friend bool operator==(const SocketAddress &a, const SocketAddress &b);
145+
146+
/** Compare two addresses for equality
147+
*
148+
* @return True if both addresses are not equal
149+
*/
150+
friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
151+
140152
private:
141153
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
142154

0 commit comments

Comments
 (0)