From b628e065261950a98b61d2dd7679febc75d477ae Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 8 Apr 2020 17:22:19 +0800 Subject: [PATCH] crimson/os: port abseil::Layout to C++17 and adapt it to our needs * use C++17 instead of homebrew facilities * use boost::beast::span for Span Signed-off-by: Kefu Chai --- src/crimson/common/layout.h | 50 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/crimson/common/layout.h b/src/crimson/common/layout.h index 69cc85dd6679d..9d54ecd1d7649 100644 --- a/src/crimson/common/layout.h +++ b/src/crimson/common/layout.h @@ -174,10 +174,9 @@ #include #endif -#include "absl/meta/type_traits.h" -#include "absl/strings/str_cat.h" -#include "absl/types/span.h" -#include "absl/utility/utility.h" +// for C++20 std::span +#include +#include #if defined(__GXX_RTTI) #define ABSL_INTERNAL_HAS_CXA_DEMANGLE @@ -188,7 +187,6 @@ #endif namespace absl { -ABSL_NAMESPACE_BEGIN namespace container_internal { // A type wrapper that instructs `Layout` to use the specific alignment for the @@ -248,16 +246,16 @@ struct AlignOf> { // Does `Ts...` contain `T`? template -using Contains = absl::disjunction...>; +using Contains = std::disjunction...>; template using CopyConst = - typename std::conditional::value, const To, To>::type; + typename std::conditional_t, const To, To>; // Note: We're not qualifying this with absl:: because it doesn't compile under // MSVC. template -using SliceType = Span; +using SliceType = boost::beast::span; // This namespace contains no types. It prevents functions defined in it from // being found by ADL. @@ -298,11 +296,11 @@ std::string TypeName() { demangled = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status); #endif if (status == 0 && demangled != nullptr) { // Demangling succeeded. - absl::StrAppend(&out, "<", demangled, ">"); + out = fmt::format("<{}>", demangled); free(demangled); } else { #if defined(__GXX_RTTI) || defined(_CPPRTTI) - absl::StrAppend(&out, "<", typeid(T).name(), ">"); + out = fmt::format("<{}>", typeid(T).name()); #endif } return out; @@ -311,14 +309,14 @@ std::string TypeName() { } // namespace adl_barrier template -using EnableIf = typename std::enable_if::type; +using EnableIf = typename std::enable_if_t; // Can `T` be a template argument of `Layout`? template using IsLegalElementType = std::integral_constant< - bool, !std::is_reference::value && !std::is_volatile::value && - !std::is_reference::type>::value && - !std::is_volatile::type>::value && + bool, !std::is_reference_v && !std::is_volatile_v && + !std::is_reference_v::type> && + !std::is_volatile_v::type> && adl_barrier::IsPow2(AlignOf::value)>; template @@ -336,11 +334,11 @@ class LayoutImpl; // `Min(sizeof...(Elements), NumSizes + 1)` (the number of arrays for which we // can compute offsets). template -class LayoutImpl, absl::index_sequence, - absl::index_sequence> { +class LayoutImpl, std::index_sequence, + std::index_sequence> { private: static_assert(sizeof...(Elements) > 0, "At least one field is required"); - static_assert(absl::conjunction...>::value, + static_assert(std::conjunction_v...>, "Invalid element type (see IsLegalElementType)"); enum { @@ -646,16 +644,15 @@ class LayoutImpl, absl::index_sequence, const size_t sizes[] = {SizeOf>()...}; const std::string types[] = { adl_barrier::TypeName>()...}; - std::string res = absl::StrCat("@0", types[0], "(", sizes[0], ")"); + std::string res = fmt::format("@0{}({})", types[0], sizes[0]); for (size_t i = 0; i != NumOffsets - 1; ++i) { - absl::StrAppend(&res, "[", size_[i], "]; @", offsets[i + 1], types[i + 1], - "(", sizes[i + 1], ")"); + res += fmt::format("[{}]; @({})", size_[i], offsets[i + 1], types[i + 1], sizes[i + 1]); } // NumSizes is a constant that may be zero. Some compilers cannot see that // inside the if statement "size_[NumSizes - 1]" must be valid. int last = static_cast(NumSizes) - 1; if (NumTypes == NumSizes && last >= 0) { - absl::StrAppend(&res, "[", size_[last], "]"); + res += fmt::format("[{}]", size_[last]); } return res; } @@ -667,8 +664,8 @@ class LayoutImpl, absl::index_sequence, template using LayoutType = LayoutImpl< - std::tuple, absl::make_index_sequence, - absl::make_index_sequence>; + std::tuple, std::make_index_sequence, + std::make_index_sequence>; } // namespace internal_layout @@ -683,7 +680,7 @@ class Layout : public internal_layout::LayoutType { public: static_assert(sizeof...(Ts) > 0, "At least one field is required"); static_assert( - absl::conjunction...>::value, + std::conjunction_v...>, "Invalid element type (see IsLegalElementType)"); // The result type of `Partial()` with `NumSizes` arguments. @@ -718,8 +715,8 @@ class Layout : public internal_layout::LayoutType { // Requires: all arguments are convertible to `size_t`. template static constexpr PartialType Partial(Sizes&&... sizes) { - static_assert(sizeof...(Sizes) <= sizeof...(Ts), ""); - return PartialType(absl::forward(sizes)...); + static_assert(sizeof...(Sizes) <= sizeof...(Ts)); + return PartialType(std::forward(sizes)...); } // Creates a layout with the sizes of all arrays specified. If you know @@ -735,7 +732,6 @@ class Layout : public internal_layout::LayoutType { }; } // namespace container_internal -ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_CONTAINER_INTERNAL_LAYOUT_H_ -- 2.39.5