net/IPv4Address: add "noexcept"

This commit is contained in:
Max Kellermann 2018-08-20 15:43:35 +02:00
parent 489e11072e
commit 5de46268af
2 changed files with 21 additions and 21 deletions

View File

@ -33,7 +33,7 @@
#include <assert.h> #include <assert.h>
static const struct sockaddr_in * static const struct sockaddr_in *
CastToIPv4(const struct sockaddr *p) CastToIPv4(const struct sockaddr *p) noexcept
{ {
assert(p->sa_family == AF_INET); assert(p->sa_family == AF_INET);
@ -42,5 +42,5 @@ CastToIPv4(const struct sockaddr *p)
return reinterpret_cast<const struct sockaddr_in *>(q); return reinterpret_cast<const struct sockaddr_in *>(q);
} }
IPv4Address::IPv4Address(SocketAddress src) IPv4Address::IPv4Address(SocketAddress src) noexcept
:address(*CastToIPv4(src.GetAddress())) {} :address(*CastToIPv4(src.GetAddress())) {}

View File

@ -50,11 +50,11 @@ class IPv4Address {
#ifdef WIN32 #ifdef WIN32
static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b, 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 }}}; 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); return ConstructInAddr(x >> 24, x >> 16, x >> 8, x);
} }
#else #else
@ -64,22 +64,22 @@ class IPv4Address {
#endif #endif
static constexpr in_addr_t ConstructInAddrT(uint8_t a, uint8_t b, 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); 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) }; return { ToBE32(x) };
} }
static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b, 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) }; return { ConstructInAddrT(a, b, c, d) };
} }
#endif #endif
static constexpr struct sockaddr_in Construct(struct in_addr address, static constexpr struct sockaddr_in Construct(struct in_addr address,
uint16_t port) { uint16_t port) noexcept {
return { return {
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
sizeof(struct sockaddr_in), sizeof(struct sockaddr_in),
@ -92,54 +92,54 @@ class IPv4Address {
} }
static constexpr struct sockaddr_in Construct(uint32_t address, static constexpr struct sockaddr_in Construct(uint32_t address,
uint16_t port) { uint16_t port) noexcept {
return Construct(ConstructInAddr(address), port); return Construct(ConstructInAddr(address), port);
} }
public: public:
IPv4Address() = default; 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)) {} :address(Construct(_address, port)) {}
constexpr IPv4Address(uint8_t a, uint8_t b, uint8_t c, 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) {} :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) {} :IPv4Address(ConstructInAddr(INADDR_ANY), port) {}
/** /**
* Convert a #SocketAddress to a #IPv4Address. Its address family must be AF_INET. * 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); return ConstructInAddr(INADDR_LOOPBACK);
} }
operator SocketAddress() const { constexpr operator SocketAddress() const noexcept {
return SocketAddress(reinterpret_cast<const struct sockaddr *>(&address), return SocketAddress((const struct sockaddr *)&address,
sizeof(address)); sizeof(address));
} }
SocketAddress::size_type GetSize() { constexpr SocketAddress::size_type GetSize() const noexcept {
return sizeof(address); return sizeof(address);
} }
constexpr bool IsDefined() const { constexpr bool IsDefined() const noexcept {
return address.sin_family != AF_UNSPEC; return address.sin_family != AF_UNSPEC;
} }
constexpr uint16_t GetPort() const { constexpr uint16_t GetPort() const noexcept {
return FromBE16(address.sin_port); return FromBE16(address.sin_port);
} }
void SetPort(uint16_t port) { void SetPort(uint16_t port) noexcept {
address.sin_port = ToBE16(port); address.sin_port = ToBE16(port);
} }
constexpr const struct in_addr &GetAddress() const { constexpr const struct in_addr &GetAddress() const noexcept {
return address.sin_addr; return address.sin_addr;
} }
}; };