net/IPv4Address: add "noexcept"
This commit is contained in:
parent
489e11072e
commit
5de46268af
@ -33,7 +33,7 @@
|
||||
#include <assert.h>
|
||||
|
||||
static const struct sockaddr_in *
|
||||
CastToIPv4(const struct sockaddr *p)
|
||||
CastToIPv4(const struct sockaddr *p) noexcept
|
||||
{
|
||||
assert(p->sa_family == AF_INET);
|
||||
|
||||
@ -42,5 +42,5 @@ CastToIPv4(const struct sockaddr *p)
|
||||
return reinterpret_cast<const struct sockaddr_in *>(q);
|
||||
}
|
||||
|
||||
IPv4Address::IPv4Address(SocketAddress src)
|
||||
IPv4Address::IPv4Address(SocketAddress src) noexcept
|
||||
:address(*CastToIPv4(src.GetAddress())) {}
|
||||
|
@ -50,11 +50,11 @@ class IPv4Address {
|
||||
|
||||
#ifdef WIN32
|
||||
static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b,
|
||||
uint8_t c, uint8_t d) {
|
||||
uint8_t c, uint8_t d) noexcept {
|
||||
return {{{ a, b, c, d }}};
|
||||
}
|
||||
|
||||
static constexpr struct in_addr ConstructInAddr(uint32_t x) {
|
||||
static constexpr struct in_addr ConstructInAddr(uint32_t x) noexcept {
|
||||
return ConstructInAddr(x >> 24, x >> 16, x >> 8, x);
|
||||
}
|
||||
#else
|
||||
@ -64,22 +64,22 @@ class IPv4Address {
|
||||
#endif
|
||||
|
||||
static constexpr in_addr_t ConstructInAddrT(uint8_t a, uint8_t b,
|
||||
uint8_t c, uint8_t d) {
|
||||
uint8_t c, uint8_t d) noexcept {
|
||||
return ToBE32((a << 24) | (b << 16) | (c << 8) | d);
|
||||
}
|
||||
|
||||
static constexpr struct in_addr ConstructInAddr(uint32_t x) {
|
||||
static constexpr struct in_addr ConstructInAddr(uint32_t x) noexcept {
|
||||
return { ToBE32(x) };
|
||||
}
|
||||
|
||||
static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b,
|
||||
uint8_t c, uint8_t d) {
|
||||
uint8_t c, uint8_t d) noexcept {
|
||||
return { ConstructInAddrT(a, b, c, d) };
|
||||
}
|
||||
#endif
|
||||
|
||||
static constexpr struct sockaddr_in Construct(struct in_addr address,
|
||||
uint16_t port) {
|
||||
uint16_t port) noexcept {
|
||||
return {
|
||||
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
|
||||
sizeof(struct sockaddr_in),
|
||||
@ -92,54 +92,54 @@ class IPv4Address {
|
||||
}
|
||||
|
||||
static constexpr struct sockaddr_in Construct(uint32_t address,
|
||||
uint16_t port) {
|
||||
uint16_t port) noexcept {
|
||||
return Construct(ConstructInAddr(address), port);
|
||||
}
|
||||
|
||||
public:
|
||||
IPv4Address() = default;
|
||||
|
||||
constexpr IPv4Address(struct in_addr _address, uint16_t port)
|
||||
constexpr IPv4Address(struct in_addr _address, uint16_t port) noexcept
|
||||
:address(Construct(_address, port)) {}
|
||||
|
||||
constexpr IPv4Address(uint8_t a, uint8_t b, uint8_t c,
|
||||
uint8_t d, uint16_t port)
|
||||
uint8_t d, uint16_t port) noexcept
|
||||
:IPv4Address(ConstructInAddr(a, b, c, d), port) {}
|
||||
|
||||
constexpr explicit IPv4Address(uint16_t port)
|
||||
constexpr explicit IPv4Address(uint16_t port) noexcept
|
||||
:IPv4Address(ConstructInAddr(INADDR_ANY), port) {}
|
||||
|
||||
/**
|
||||
* Convert a #SocketAddress to a #IPv4Address. Its address family must be AF_INET.
|
||||
*/
|
||||
explicit IPv4Address(SocketAddress src);
|
||||
explicit IPv4Address(SocketAddress src) noexcept;
|
||||
|
||||
static constexpr struct in_addr Loopback() {
|
||||
static constexpr struct in_addr Loopback() noexcept {
|
||||
return ConstructInAddr(INADDR_LOOPBACK);
|
||||
}
|
||||
|
||||
operator SocketAddress() const {
|
||||
return SocketAddress(reinterpret_cast<const struct sockaddr *>(&address),
|
||||
constexpr operator SocketAddress() const noexcept {
|
||||
return SocketAddress((const struct sockaddr *)&address,
|
||||
sizeof(address));
|
||||
}
|
||||
|
||||
SocketAddress::size_type GetSize() {
|
||||
constexpr SocketAddress::size_type GetSize() const noexcept {
|
||||
return sizeof(address);
|
||||
}
|
||||
|
||||
constexpr bool IsDefined() const {
|
||||
constexpr bool IsDefined() const noexcept {
|
||||
return address.sin_family != AF_UNSPEC;
|
||||
}
|
||||
|
||||
constexpr uint16_t GetPort() const {
|
||||
constexpr uint16_t GetPort() const noexcept {
|
||||
return FromBE16(address.sin_port);
|
||||
}
|
||||
|
||||
void SetPort(uint16_t port) {
|
||||
void SetPort(uint16_t port) noexcept {
|
||||
address.sin_port = ToBE16(port);
|
||||
}
|
||||
|
||||
constexpr const struct in_addr &GetAddress() const {
|
||||
constexpr const struct in_addr &GetAddress() const noexcept {
|
||||
return address.sin_addr;
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user