1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

fix testing IPAddress validity (#5684)

* fix testing IPAddress validity (operator bool())
* +add new operator=
* +cast to u32_t
* + operator >>
This commit is contained in:
david gauchard 2019-01-29 01:13:47 +01:00 committed by GitHub
parent f42bfdfc0d
commit 05ac2e69e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,13 +87,15 @@ class IPAddress: public Printable {
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected
operator uint32_t() const {
return isV4()? v4(): (uint32_t)0;
}
operator uint32_t() const { return isV4()? v4(): (uint32_t)0; }
operator uint32_t() { return isV4()? v4(): (uint32_t)0; }
operator u32_t() const { return isV4()? v4(): (u32_t)0; }
operator u32_t() { return isV4()? v4(): (u32_t)0; }
// the above uint32_t() cast can be ambiguous
// if gcc complains, use instead isSet() or v4() according to what's relevant
bool isSet () const;
operator bool () const { return isSet(); } // <-
operator bool () { return isSet(); } // <- both are needed
// generic IPv4 wrapper to uint32-view like arduino loves to see it
const u32_t& v4() const { return ip_2_ip4(&_ip)->addr; } // for raw_address(const)
u32_t& v4() { return ip_2_ip4(&_ip)->addr; }
@ -118,6 +120,10 @@ class IPAddress: public Printable {
}
bool operator==(const uint8_t* addr) const;
int operator>>(int n) const {
return isV4()? v4() >> n: 0;
}
// Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const {
return isV4()? *(raw_address() + index): 0;
@ -155,6 +161,9 @@ class IPAddress: public Printable {
IPAddress(const ipv4_addr& fw_addr) { setV4(); v4() = fw_addr.addr; }
IPAddress(const ipv4_addr* fw_addr) { setV4(); v4() = fw_addr->addr; }
IPAddress& operator=(const ipv4_addr& fw_addr) { setV4(); v4() = fw_addr.addr; return *this; }
IPAddress& operator=(const ipv4_addr* fw_addr) { setV4(); v4() = fw_addr->addr; return *this; }
operator ip_addr_t () const { return _ip; }
operator const ip_addr_t*() const { return &_ip; }
operator ip_addr_t*() { return &_ip; }
@ -169,6 +178,9 @@ class IPAddress: public Printable {
IPAddress(const ip_addr_t& lwip_addr) { ip_addr_copy(_ip, lwip_addr); }
IPAddress(const ip_addr_t* lwip_addr) { ip_addr_copy(_ip, *lwip_addr); }
IPAddress& operator=(const ip_addr_t& lwip_addr) { ip_addr_copy(_ip, lwip_addr); return *this; }
IPAddress& operator=(const ip_addr_t* lwip_addr) { ip_addr_copy(_ip, *lwip_addr); return *this; }
uint16_t* raw6()
{
setV6();