From e538740355ccb95444c4e6d3d455711a948c6404 Mon Sep 17 00:00:00 2001 From: Bascy Date: Fri, 19 Feb 2021 23:07:07 +0100 Subject: [PATCH 1/4] Added operator== and operator!= overload for easier comparing --- libraries/BLE/src/BLEAddress.cpp | 7 +++++++ libraries/BLE/src/BLEAddress.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/libraries/BLE/src/BLEAddress.cpp b/libraries/BLE/src/BLEAddress.cpp index 6d83b17e68e..f4f078f3136 100644 --- a/libraries/BLE/src/BLEAddress.cpp +++ b/libraries/BLE/src/BLEAddress.cpp @@ -62,6 +62,13 @@ bool BLEAddress::equals(BLEAddress otherAddress) { return memcmp(otherAddress.getNative(), m_address, 6) == 0; } // equals +bool BLEAddress::operator==(BLEAddress otherAddress) { + return equals(otherAddress); +} + +bool BLEAddress::operator!=(BLEAddress otherAddress) { + return !equals(otherAddress); +} /** * @brief Return the native representation of the address. diff --git a/libraries/BLE/src/BLEAddress.h b/libraries/BLE/src/BLEAddress.h index 7eff4da4bb6..3f85fb9acab 100644 --- a/libraries/BLE/src/BLEAddress.h +++ b/libraries/BLE/src/BLEAddress.h @@ -23,6 +23,8 @@ class BLEAddress { BLEAddress(esp_bd_addr_t address); BLEAddress(std::string stringAddress); bool equals(BLEAddress otherAddress); + bool operator==(BLEAddress otherAddress); + bool operator!=(BLEAddress otherAddress); esp_bd_addr_t* getNative(); std::string toString(); From 5b49cb5b1b1c6badff4d9144359e4e24f410f11e Mon Sep 17 00:00:00 2001 From: Bascy Date: Fri, 19 Feb 2021 23:11:57 +0100 Subject: [PATCH 2/4] Use ESP_BD_ADDR_LEN define --- libraries/BLE/src/BLEAddress.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEAddress.cpp b/libraries/BLE/src/BLEAddress.cpp index f4f078f3136..b6f6bacc5e0 100644 --- a/libraries/BLE/src/BLEAddress.cpp +++ b/libraries/BLE/src/BLEAddress.cpp @@ -59,7 +59,7 @@ BLEAddress::BLEAddress(std::string stringAddress) { * @return True if the addresses are equal. */ bool BLEAddress::equals(BLEAddress otherAddress) { - return memcmp(otherAddress.getNative(), m_address, 6) == 0; + return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) == 0; } // equals bool BLEAddress::operator==(BLEAddress otherAddress) { From c8a50d52eba708d71b33ff4ce7bcf59c40a4642c Mon Sep 17 00:00:00 2001 From: Bascy Date: Fri, 19 Feb 2021 23:24:14 +0100 Subject: [PATCH 3/4] Added <, <=, >, >= operator overloads to BLEAddress - Allows use of BLEAddres as key in std::map<> --- libraries/BLE/src/BLEAddress.cpp | 18 +++++++++++++++++- libraries/BLE/src/BLEAddress.h | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEAddress.cpp b/libraries/BLE/src/BLEAddress.cpp index b6f6bacc5e0..ffbb446b6ee 100644 --- a/libraries/BLE/src/BLEAddress.cpp +++ b/libraries/BLE/src/BLEAddress.cpp @@ -67,7 +67,23 @@ bool BLEAddress::operator==(BLEAddress otherAddress) { } bool BLEAddress::operator!=(BLEAddress otherAddress) { - return !equals(otherAddress); + return ! this == otherAddress; +} + +bool BLEAddress::operator<(BLEAddress otherAddress) { + return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) < 0; +} + +bool BLEAddress::operator<=(BLEAddress otherAddress) { + return !this > otherAddress; +} + +bool BLEAddress::operator>=(BLEAddress otherAddress) { + return !this < otherAddress; +} + +bool BLEAddress::operator>(BLEAddress otherAddress) { + return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) > 0; } /** diff --git a/libraries/BLE/src/BLEAddress.h b/libraries/BLE/src/BLEAddress.h index 3f85fb9acab..495214e4897 100644 --- a/libraries/BLE/src/BLEAddress.h +++ b/libraries/BLE/src/BLEAddress.h @@ -25,6 +25,10 @@ class BLEAddress { bool equals(BLEAddress otherAddress); bool operator==(BLEAddress otherAddress); bool operator!=(BLEAddress otherAddress); + bool operator<(BLEAddress otherAddress); + bool operator<=(BLEAddress otherAddress); + bool operator>(BLEAddress otherAddress); + bool operator>=(BLEAddress otherAddress); esp_bd_addr_t* getNative(); std::string toString(); From b5ad6e936ee30b99c738dd9df3ab913113b5308e Mon Sep 17 00:00:00 2001 From: Bascy Date: Sat, 20 Feb 2021 01:08:13 +0100 Subject: [PATCH 4/4] Corrected use of const --- libraries/BLE/src/BLEAddress.cpp | 26 +++++++++++++------------- libraries/BLE/src/BLEAddress.h | 12 ++++++------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/libraries/BLE/src/BLEAddress.cpp b/libraries/BLE/src/BLEAddress.cpp index ffbb446b6ee..9a31cf0146f 100644 --- a/libraries/BLE/src/BLEAddress.cpp +++ b/libraries/BLE/src/BLEAddress.cpp @@ -62,34 +62,34 @@ bool BLEAddress::equals(BLEAddress otherAddress) { return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) == 0; } // equals -bool BLEAddress::operator==(BLEAddress otherAddress) { - return equals(otherAddress); +bool BLEAddress::operator==(const BLEAddress& otherAddress) const { + return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) == 0; } -bool BLEAddress::operator!=(BLEAddress otherAddress) { - return ! this == otherAddress; +bool BLEAddress::operator!=(const BLEAddress& otherAddress) const { + return !(*this == otherAddress); } -bool BLEAddress::operator<(BLEAddress otherAddress) { - return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) < 0; +bool BLEAddress::operator<(const BLEAddress& otherAddress) const { + return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) < 0; } -bool BLEAddress::operator<=(BLEAddress otherAddress) { - return !this > otherAddress; +bool BLEAddress::operator<=(const BLEAddress& otherAddress) const { + return !(*this > otherAddress); } -bool BLEAddress::operator>=(BLEAddress otherAddress) { - return !this < otherAddress; +bool BLEAddress::operator>=(const BLEAddress& otherAddress) const { + return !(*this < otherAddress); } -bool BLEAddress::operator>(BLEAddress otherAddress) { - return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) > 0; +bool BLEAddress::operator>(const BLEAddress& otherAddress) const { + return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) > 0; } /** * @brief Return the native representation of the address. * @return The native representation of the address. - */ + */ esp_bd_addr_t *BLEAddress::getNative() { return &m_address; } // getNative diff --git a/libraries/BLE/src/BLEAddress.h b/libraries/BLE/src/BLEAddress.h index 495214e4897..864b09a3e03 100644 --- a/libraries/BLE/src/BLEAddress.h +++ b/libraries/BLE/src/BLEAddress.h @@ -23,12 +23,12 @@ class BLEAddress { BLEAddress(esp_bd_addr_t address); BLEAddress(std::string stringAddress); bool equals(BLEAddress otherAddress); - bool operator==(BLEAddress otherAddress); - bool operator!=(BLEAddress otherAddress); - bool operator<(BLEAddress otherAddress); - bool operator<=(BLEAddress otherAddress); - bool operator>(BLEAddress otherAddress); - bool operator>=(BLEAddress otherAddress); + bool operator==(const BLEAddress& otherAddress) const; + bool operator!=(const BLEAddress& otherAddress) const; + bool operator<(const BLEAddress& otherAddress) const; + bool operator<=(const BLEAddress& otherAddress) const; + bool operator>(const BLEAddress& otherAddress) const; + bool operator>=(const BLEAddress& otherAddress) const; esp_bd_addr_t* getNative(); std::string toString();