net/AddressInfo: add noexcept

This commit is contained in:
Max Kellermann
2022-04-01 20:45:22 +02:00
committed by Max Kellermann
parent 6bacb23002
commit 601e5e6abc
2 changed files with 27 additions and 26 deletions

View File

@@ -40,8 +40,8 @@ static constexpr auto address_family_ranking = std::array {
AF_INET6, AF_INET6,
}; };
static bool static constexpr bool
IsAddressFamilyBetter(int previous, int next) IsAddressFamilyBetter(int previous, int next) noexcept
{ {
for (auto i : address_family_ranking) { for (auto i : address_family_ranking) {
if (next == i) if (next == i)
@@ -53,21 +53,21 @@ IsAddressFamilyBetter(int previous, int next)
return false; return false;
} }
static bool static constexpr bool
IsBetter(const AddressInfo &previous, const AddressInfo &next) IsBetter(const AddressInfo &previous, const AddressInfo &next) noexcept
{ {
return IsAddressFamilyBetter(previous.GetFamily(), return IsAddressFamilyBetter(previous.GetFamily(),
next.GetFamily()); next.GetFamily());
} }
static bool static constexpr bool
IsBetter(const AddressInfo *previous, const AddressInfo &next) IsBetter(const AddressInfo *previous, const AddressInfo &next) noexcept
{ {
return previous == nullptr || IsBetter(*previous, next); return previous == nullptr || IsBetter(*previous, next);
} }
const AddressInfo & const AddressInfo &
AddressInfoList::GetBest() const AddressInfoList::GetBest() const noexcept
{ {
assert(!empty()); assert(!empty());

View File

@@ -58,19 +58,19 @@ class AddressInfo : addrinfo {
~AddressInfo() = delete; ~AddressInfo() = delete;
public: public:
constexpr int GetFamily() const { constexpr int GetFamily() const noexcept {
return ai_family; return ai_family;
} }
constexpr int GetType() const { constexpr int GetType() const noexcept {
return ai_socktype; return ai_socktype;
} }
constexpr int GetProtocol() const { constexpr int GetProtocol() const noexcept {
return ai_protocol; return ai_protocol;
} }
constexpr operator SocketAddress() const { constexpr operator SocketAddress() const noexcept {
return {ai_addr, (SocketAddress::size_type)ai_addrlen}; return {ai_addr, (SocketAddress::size_type)ai_addrlen};
} }
}; };
@@ -80,25 +80,26 @@ class AddressInfoList {
public: public:
AddressInfoList() = default; AddressInfoList() = default;
explicit AddressInfoList(struct addrinfo *_value):value(_value) {} explicit AddressInfoList(struct addrinfo *_value) noexcept
:value(_value) {}
AddressInfoList(AddressInfoList &&src) AddressInfoList(AddressInfoList &&src) noexcept
:value(std::exchange(src.value, nullptr)) {} :value(std::exchange(src.value, nullptr)) {}
~AddressInfoList() { ~AddressInfoList() noexcept {
freeaddrinfo(value); freeaddrinfo(value);
} }
AddressInfoList &operator=(AddressInfoList &&src) { AddressInfoList &operator=(AddressInfoList &&src) noexcept {
std::swap(value, src.value); std::swap(value, src.value);
return *this; return *this;
} }
bool empty() const { bool empty() const noexcept {
return value == nullptr; return value == nullptr;
} }
const AddressInfo &front() const { const AddressInfo &front() const noexcept {
return *(const AddressInfo *)value; return *(const AddressInfo *)value;
} }
@@ -109,42 +110,42 @@ public:
* connections. * connections.
*/ */
[[gnu::pure]] [[gnu::pure]]
const AddressInfo &GetBest() const; const AddressInfo &GetBest() const noexcept;
class const_iterator { class const_iterator {
struct addrinfo *cursor; struct addrinfo *cursor;
public: public:
explicit constexpr const_iterator(struct addrinfo *_cursor) explicit constexpr const_iterator(struct addrinfo *_cursor) noexcept
:cursor(_cursor) {} :cursor(_cursor) {}
constexpr bool operator==(const_iterator other) const { constexpr bool operator==(const_iterator other) const noexcept {
return cursor == other.cursor; return cursor == other.cursor;
} }
constexpr bool operator!=(const_iterator other) const { constexpr bool operator!=(const_iterator other) const noexcept {
return cursor != other.cursor; return cursor != other.cursor;
} }
const_iterator &operator++() { const_iterator &operator++() noexcept {
cursor = cursor->ai_next; cursor = cursor->ai_next;
return *this; return *this;
} }
constexpr const AddressInfo &operator*() const { constexpr const AddressInfo &operator*() const noexcept {
return *(const AddressInfo *)cursor; return *(const AddressInfo *)cursor;
} }
constexpr const AddressInfo *operator->() const { constexpr const AddressInfo *operator->() const noexcept {
return (const AddressInfo *)cursor; return (const AddressInfo *)cursor;
} }
}; };
const_iterator begin() const { const_iterator begin() const noexcept {
return const_iterator(value); return const_iterator(value);
} }
const_iterator end() const { const_iterator end() const noexcept {
return const_iterator(nullptr); return const_iterator(nullptr);
} }
}; };