Files
scylla/gms/inet_address.hh
Kefu Chai f5b05cf981 treewide: use defaulted operator!=() and operator==()
in C++20, compiler generate operator!=() if the corresponding
operator==() is already defined, the language now understands
that the comparison is symmetric in the new standard.

fortunately, our operator!=() is always equivalent to
`! operator==()`, this matches the behavior of the default
generated operator!=(). so, in this change, all `operator!=`
are removed.

in addition to the defaulted operator!=, C++20 also brings to us
the defaulted operator==() -- it is able to generated the
operator==() if the member-wise lexicographical comparison.
under some circumstances, this is exactly what we need. so,
in this change, if the operator==() is also implemented as
a lexicographical comparison of all memeber variables of the
class/struct in question, it is implemented using the default
generated one by removing its body and mark the function as
`default`. moreover, if the class happen to have other comparison
operators which are implemented using lexicographical comparison,
the default generated `operator<=>` is used in place of
the defaulted `operator==`.

sometimes, we fail to mark the operator== with the `const`
specifier, in this change, to fulfil the need of C++ standard,
and to be more correct, the `const` specifier is added.

also, to generate the defaulted operator==, the operand should
be `const class_name&`, but it is not always the case, in the
class of `version`, we use `version` as the parameter type, to
fulfill the need of the C++ standard, the parameter type is
changed to `const version&` instead. this does not change
the semantic of the comparison operator. and is a more idiomatic
way to pass non-trivial struct as function parameters.

please note, because in C++20, both operator= and operator<=> are
symmetric, some of the operators in `multiprecision` are removed.
they are the symmetric form of the another variant. if they were
not removed, compiler would, for instance, find ambiguous
overloaded operator '=='.

this change is a cleanup to modernize the code base with C++20
features.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #13687
2023-04-27 10:24:46 +03:00

102 lines
2.9 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <seastar/net/ipv4_address.hh>
#include <seastar/net/inet_address.hh>
#include <seastar/net/socket_defs.hh>
#include <iosfwd>
#include <optional>
#include <functional>
#include "bytes.hh"
#include "seastarx.hh"
namespace gms {
class inet_address {
private:
net::inet_address _addr;
public:
inet_address() = default;
inet_address(int32_t ip) noexcept
: inet_address(uint32_t(ip)) {
}
explicit inet_address(uint32_t ip) noexcept
: _addr(net::ipv4_address(ip)) {
}
inet_address(const net::inet_address& addr) noexcept : _addr(addr) {}
inet_address(const socket_address& sa) noexcept
: inet_address(sa.addr())
{}
const net::inet_address& addr() const noexcept {
return _addr;
}
inet_address(const inet_address&) = default;
operator const seastar::net::inet_address&() const noexcept {
return _addr;
}
// throws std::invalid_argument if sstring is invalid
inet_address(const sstring& addr) {
// FIXME: We need a real DNS resolver
if (addr == "localhost") {
_addr = net::ipv4_address("127.0.0.1");
} else {
_addr = net::inet_address(addr);
}
}
bytes_view bytes() const noexcept {
return bytes_view(reinterpret_cast<const int8_t*>(_addr.data()), _addr.size());
}
// TODO remove
uint32_t raw_addr() const {
return addr().as_ipv4_address().ip;
}
sstring to_sstring() const;
friend inline bool operator==(const inet_address& x, const inet_address& y) noexcept = default;
friend inline bool operator<(const inet_address& x, const inet_address& y) noexcept {
return x.bytes() < y.bytes();
}
friend struct std::hash<inet_address>;
using opt_family = std::optional<net::inet_address::family>;
static future<inet_address> lookup(sstring, opt_family family = {}, opt_family preferred = {});
};
std::ostream& operator<<(std::ostream& os, const inet_address& x);
}
namespace std {
template<>
struct hash<gms::inet_address> {
size_t operator()(gms::inet_address a) const noexcept { return std::hash<net::inet_address>()(a._addr); }
};
}
template <>
struct fmt::formatter<gms::inet_address> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const ::gms::inet_address& x, FormatContext& ctx) const {
if (x.addr().is_ipv4()) {
return fmt::format_to(ctx.out(), "{}", x.addr());
}
// print 2 bytes in a group, and use ':' as the delimeter
fmt::format_to(ctx.out(), "{:2:}", fmt_hex(x.bytes()));
if (x.addr().scope() != seastar::net::inet_address::invalid_scope) {
return fmt::format_to(ctx.out(), "%{}", x.addr().scope());
}
return ctx.out();
}
};