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
74 lines
1.5 KiB
C++
74 lines
1.5 KiB
C++
/*
|
|
* Copyright (C) 2016-present ScyllaDB
|
|
*
|
|
* Modified by ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string_view>
|
|
#include <functional>
|
|
#include <optional>
|
|
|
|
#include <seastar/core/sstring.hh>
|
|
|
|
#include "seastarx.hh"
|
|
|
|
namespace auth {
|
|
|
|
///
|
|
/// A type-safe wrapper for the name of a logged-in user, or a nameless (anonymous) user.
|
|
///
|
|
class authenticated_user final {
|
|
public:
|
|
///
|
|
/// An anonymous user has no name.
|
|
///
|
|
std::optional<sstring> name{};
|
|
|
|
///
|
|
/// An anonymous user.
|
|
///
|
|
authenticated_user() = default;
|
|
explicit authenticated_user(std::string_view name);
|
|
friend bool operator==(const authenticated_user&, const authenticated_user&) noexcept = default;
|
|
};
|
|
|
|
const authenticated_user& anonymous_user() noexcept;
|
|
|
|
inline bool is_anonymous(const authenticated_user& u) noexcept {
|
|
return u == anonymous_user();
|
|
}
|
|
|
|
}
|
|
|
|
///
|
|
/// The user name, or "anonymous".
|
|
///
|
|
template <>
|
|
struct fmt::formatter<auth::authenticated_user> : fmt::formatter<std::string_view> {
|
|
template <typename FormatContext>
|
|
auto format(const auth::authenticated_user& u, FormatContext& ctx) const {
|
|
if (u.name) {
|
|
return fmt::format_to(ctx.out(), "{}", *u.name);
|
|
} else {
|
|
return fmt::format_to(ctx.out(), "{}", "anonymous");
|
|
}
|
|
}
|
|
};
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
struct hash<auth::authenticated_user> final {
|
|
size_t operator()(const auth::authenticated_user &u) const {
|
|
return std::hash<std::optional<sstring>>()(u.name);
|
|
}
|
|
};
|
|
|
|
}
|