1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Minor tidy-up to IPAddress

This commit is contained in:
jules 2018-10-15 10:06:36 +01:00
parent 27888d3de9
commit 14989b6eda
2 changed files with 31 additions and 33 deletions

View file

@ -23,6 +23,19 @@
namespace juce
{
/** Union used to split a 16-bit unsigned integer into 2 8-bit unsigned integers or vice-versa */
union IPAddressByteUnion
{
uint16 combined;
uint8 split[2];
};
static void zeroUnusedBytes (uint8* address) noexcept
{
for (int i = 4; i < 16; ++i)
address[i] = 0;
}
IPAddress::IPAddress() noexcept
{
for (int i = 0; i < 16; ++i)
@ -35,12 +48,12 @@ IPAddress::IPAddress (const uint8 bytes[], bool IPv6) noexcept : isIPv6 (IPv6)
address[i] = bytes[i];
if (! isIPv6)
zeroUnusedBytes();
zeroUnusedBytes (address);
}
IPAddress::IPAddress (const uint16 bytes[8]) noexcept : isIPv6 (true)
{
ByteUnion temp;
IPAddressByteUnion temp;
for (int i = 0; i < 8; ++i)
{
@ -56,7 +69,7 @@ IPAddress::IPAddress (uint8 a0, uint8 a1, uint8 a2, uint8 a3) noexcept : isIPv6
address[0] = a0; address[1] = a1;
address[2] = a2; address[3] = a3;
zeroUnusedBytes();
zeroUnusedBytes (address);
}
IPAddress::IPAddress (uint16 a1, uint16 a2, uint16 a3, uint16 a4,
@ -65,7 +78,7 @@ IPAddress::IPAddress (uint16 a1, uint16 a2, uint16 a3, uint16 a4,
{
uint16 array[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
ByteUnion temp;
IPAddressByteUnion temp;
for (int i = 0; i < 8; ++i)
{
@ -82,7 +95,7 @@ IPAddress::IPAddress (uint32 n) noexcept : isIPv6 (false)
address[2] = (n >> 8) & 255;
address[3] = (n & 255);
zeroUnusedBytes();
zeroUnusedBytes (address);
}
bool IPAddress::isNull() const
@ -118,7 +131,7 @@ IPAddress::IPAddress (const String& adr)
for (int i = 0; i < 4; ++i)
address[i] = (uint8) tokens[i].getIntValue();
zeroUnusedBytes();
zeroUnusedBytes (address);
}
else
{
@ -152,7 +165,7 @@ IPAddress::IPAddress (const String& adr)
break;
}
ByteUnion temp;
IPAddressByteUnion temp;
temp.combined = (uint16) CharacterFunctions::HexParser<int>::parse (tokens[i].getCharPointer());
address[i * 2] = temp.split[0];
@ -173,7 +186,7 @@ String IPAddress::toString() const
return s;
}
ByteUnion temp;
IPAddressByteUnion temp;
temp.split[0] = address[0];
temp.split[1] = address[1];

View file

@ -65,12 +65,12 @@ public:
@param bytes The array containing the bytes to read.
@param IPv6 if true indicates that 16 bytes should be read instead of 4.
*/
explicit IPAddress (const uint8 bytes[], bool IPv6 = false) noexcept;
explicit IPAddress (const uint8* bytes, bool IPv6 = false) noexcept;
/** Creates an IPv6 address from an array of 8 16-bit integers
@param bytes The array containing the bytes to read.
*/
explicit IPAddress (const uint16 bytes[8]) noexcept;
explicit IPAddress (const uint16* bytes) noexcept;
/** Creates an IPv4 address from 4 bytes. */
IPAddress (uint8 address1, uint8 address2, uint8 address3, uint8 address4) noexcept;
@ -99,14 +99,14 @@ public:
@returns 0 if the two addresses are identical, negative if this address is smaller than
the other one, or positive if is greater.
*/
int compare (const IPAddress& other) const noexcept;
int compare (const IPAddress&) const noexcept;
bool operator== (const IPAddress& other) const noexcept;
bool operator!= (const IPAddress& other) const noexcept;
bool operator< (const IPAddress& other) const noexcept;
bool operator> (const IPAddress& other) const noexcept;
bool operator<= (const IPAddress& other) const noexcept;
bool operator>= (const IPAddress& other) const noexcept;
bool operator== (const IPAddress&) const noexcept;
bool operator!= (const IPAddress&) const noexcept;
bool operator< (const IPAddress&) const noexcept;
bool operator> (const IPAddress&) const noexcept;
bool operator<= (const IPAddress&) const noexcept;
bool operator>= (const IPAddress&) const noexcept;
//==============================================================================
/** The elements of the IP address. */
@ -135,23 +135,8 @@ public:
/** If the IPAdress is the address of an interface on the machine, returns the associated broadcast address.
If the address is not an interface, it will return a null address.
*/
*/
static IPAddress getInterfaceBroadcastAddress (const IPAddress& interfaceAddress);
private:
/** Union used to split a 16-bit unsigned integer into 2 8-bit unsigned integers or vice-versa */
union ByteUnion
{
uint16 combined;
uint8 split[2];
};
/** Method used to zero the remaining bytes of the address array when creating IPv4 addresses */
void zeroUnusedBytes() noexcept
{
for (int i = 4; i < 16; ++i)
address[i] = 0;
}
};
} // namespace juce