Skip to content

Commit 44aaf13

Browse files
authored
Added BLEAddress operator overload methods (#4839)
Allows BLEAddress to be used as key in std::map etc
1 parent 560c0f4 commit 44aaf13

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Diff for: libraries/BLE/src/BLEAddress.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,37 @@ BLEAddress::BLEAddress(std::string stringAddress) {
5959
* @return True if the addresses are equal.
6060
*/
6161
bool BLEAddress::equals(BLEAddress otherAddress) {
62-
return memcmp(otherAddress.getNative(), m_address, 6) == 0;
62+
return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) == 0;
6363
} // equals
6464

65+
bool BLEAddress::operator==(const BLEAddress& otherAddress) const {
66+
return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) == 0;
67+
}
68+
69+
bool BLEAddress::operator!=(const BLEAddress& otherAddress) const {
70+
return !(*this == otherAddress);
71+
}
72+
73+
bool BLEAddress::operator<(const BLEAddress& otherAddress) const {
74+
return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) < 0;
75+
}
76+
77+
bool BLEAddress::operator<=(const BLEAddress& otherAddress) const {
78+
return !(*this > otherAddress);
79+
}
80+
81+
bool BLEAddress::operator>=(const BLEAddress& otherAddress) const {
82+
return !(*this < otherAddress);
83+
}
84+
85+
bool BLEAddress::operator>(const BLEAddress& otherAddress) const {
86+
return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) > 0;
87+
}
6588

6689
/**
6790
* @brief Return the native representation of the address.
6891
* @return The native representation of the address.
69-
*/
92+
*/
7093
esp_bd_addr_t *BLEAddress::getNative() {
7194
return &m_address;
7295
} // getNative

Diff for: libraries/BLE/src/BLEAddress.h

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class BLEAddress {
2323
BLEAddress(esp_bd_addr_t address);
2424
BLEAddress(std::string stringAddress);
2525
bool equals(BLEAddress otherAddress);
26+
bool operator==(const BLEAddress& otherAddress) const;
27+
bool operator!=(const BLEAddress& otherAddress) const;
28+
bool operator<(const BLEAddress& otherAddress) const;
29+
bool operator<=(const BLEAddress& otherAddress) const;
30+
bool operator>(const BLEAddress& otherAddress) const;
31+
bool operator>=(const BLEAddress& otherAddress) const;
2632
esp_bd_addr_t* getNative();
2733
std::string toString();
2834

0 commit comments

Comments
 (0)