Skip to content

Commit 574b400

Browse files
committed
cc userver: use C++20 requires clause instead of std::enable_if_t (part 3)
Replaced `inline constexpr bool` with concepts in the remaining places where applicable. Renamed a lot of concepts by dropping the leading `k`, leaving a fallback for backward compatiblity. Renamed without fallback: * `meta::kIsUniqueMap` -> `meta::IsUniqueMap` * `meta::kIsRecursiveRange` -> `meta::IsRecursiveRange` * `meta::kIsIterator` -> `meta::IsIterator` * `meta::kIsOstreamWritable` -> `meta::IsOstreamWritable` * `meta::kIsStdHashable` -> `meta::IsStdHashable` * `meta::kIsPushBackable` -> `meta::IsPushBackable` * `meta::kIsFixedSizeContainer` -> `meta::IsFixedSizeContainer` * `utils::statistics::kHasWriterSupport` -> `utils::statistics::HasWriterSupport` Removed: * `meta::ExpectSame` (use C++20 requires expressions instead) * `meta::kIsEqualityComparable` (use `std::equality_comparable` and `std::equality_comparable_with` instead) commit_hash:df413cbeeca7b9292a748e6d084ec7f29fbea053
1 parent bea596b commit 574b400

37 files changed

Lines changed: 281 additions & 237 deletions

File tree

core/include/userver/dump/common.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ void WriteInteger(Writer& writer, std::uint64_t value);
6464
std::uint64_t ReadInteger(Reader& reader);
6565

6666
template <typename Duration>
67-
inline constexpr bool kIsDumpedAsNanoseconds =
68-
std::is_integral_v<typename Duration::rep> && (Duration::period::num == 1) &&
67+
concept IsDumpedAsNanoseconds =
68+
std::integral<typename Duration::rep> && (Duration::period::num == 1) &&
6969
(Duration{1} <= std::chrono::milliseconds{1}) && (1'000'000'000 % Duration::period::den == 0);
7070

7171
} // namespace impl
@@ -149,7 +149,7 @@ void Write(Writer& writer, std::chrono::duration<Rep, Period> value) {
149149
// Durations, which on some systems represent
150150
// `std::chrono::*_clock::duration`, are serialized as `std::nanoseconds`
151151
// to avoid system dependency
152-
if constexpr (impl::kIsDumpedAsNanoseconds<duration<Rep, Period>>) {
152+
if constexpr (impl::IsDumpedAsNanoseconds<duration<Rep, Period>>) {
153153
const auto count = std::chrono::duration_cast<nanoseconds>(value).count();
154154

155155
if (nanoseconds{count} != value) {
@@ -169,7 +169,7 @@ template <typename Rep, typename Period>
169169
std::chrono::duration<Rep, Period> Read(Reader& reader, To<std::chrono::duration<Rep, Period>>) {
170170
using std::chrono::duration, std::chrono::nanoseconds;
171171

172-
if constexpr (impl::kIsDumpedAsNanoseconds<duration<Rep, Period>>) {
172+
if constexpr (impl::IsDumpedAsNanoseconds<duration<Rep, Period>>) {
173173
const auto count = impl::ReadTrivial<nanoseconds::rep>(reader);
174174
return std::chrono::duration_cast<duration<Rep, Period>>(nanoseconds{count});
175175
} else {

core/include/userver/dump/meta.hpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,42 @@
33
/// @file userver/dump/meta.hpp
44
/// @brief Provides dump::kIsDumpable and includes userver/dump/fwd.hpp
55

6+
#include <concepts>
67
#include <type_traits>
78

89
#include <userver/dump/fwd.hpp>
9-
#include <userver/utils/meta_light.hpp>
1010

1111
USERVER_NAMESPACE_BEGIN
1212

1313
namespace dump {
1414

15-
namespace impl {
16-
17-
template <typename T>
18-
using WritableResult = decltype(Write(std::declval<Writer&>(), std::declval<const T&>()));
19-
20-
template <typename T>
21-
using ReadableResult = decltype(Read(std::declval<Reader&>(), To<T>{}));
22-
23-
} // namespace impl
24-
2515
/// Check if `writer.Write(T)` is available
2616
template <typename T>
27-
inline constexpr bool kIsWritable = std::is_same_v<meta::DetectedType<impl::WritableResult, T>, void>;
17+
// NOLINTNEXTLINE(readability-identifier-naming)
18+
concept IsWritable = requires(Writer& writer, const T& value) {
19+
{
20+
Write(writer, value)
21+
} -> std::same_as<void>;
22+
};
2823

2924
/// Check if `reader.Read<T>()` is available
3025
template <typename T>
31-
inline constexpr bool kIsReadable = std::is_same_v<meta::DetectedType<impl::ReadableResult, T>, std::remove_const_t<T>>;
26+
// NOLINTNEXTLINE(readability-identifier-naming)
27+
concept IsReadable = requires(Reader& reader) {
28+
{
29+
Read(reader, To<T>{})
30+
} -> std::same_as<std::remove_const_t<T>>;
31+
};
3232

3333
/// Check if `T` is both writable and readable
3434
template <typename T>
35-
inline constexpr bool kIsDumpable = kIsWritable<T> && kIsReadable<T>;
35+
// NOLINTNEXTLINE(readability-identifier-naming)
36+
concept IsDumpable = IsWritable<T> && IsReadable<T>;
3637

3738
template <typename T>
3839
constexpr bool CheckDumpable() {
3940
static_assert(
40-
kIsDumpable<T>,
41+
IsDumpable<T>,
4142
"Type is not dumpable. Probably you forgot to include "
4243
"<userver/dump/common.hpp>, <userver/dump/common_containers.hpp> or "
4344
"other headers with Read and Write declarations"
@@ -46,6 +47,21 @@ constexpr bool CheckDumpable() {
4647
return true;
4748
}
4849

50+
/// @deprecated Use @ref dump::IsWritable instead.
51+
template <typename T>
52+
// NOLINTNEXTLINE(readability-identifier-naming)
53+
concept kIsWritable = IsWritable<T>;
54+
55+
/// @deprecated Use @ref dump::IsReadable instead.
56+
template <typename T>
57+
// NOLINTNEXTLINE(readability-identifier-naming)
58+
concept kIsReadable = IsReadable<T>;
59+
60+
/// @deprecated Use @ref dump::IsDumpable instead.
61+
template <typename T>
62+
// NOLINTNEXTLINE(readability-identifier-naming)
63+
concept kIsDumpable = IsDumpable<T>;
64+
4965
} // namespace dump
5066

5167
USERVER_NAMESPACE_END

core/include/userver/middlewares/pipeline.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,25 @@ class MiddlewareDependencyBuilder final {
156156
namespace impl {
157157

158158
template <typename T>
159-
using IsGroup = decltype(T::kDependency);
160-
161-
template <typename T>
162-
constexpr bool kIsGroup = std::is_same_v<meta::DetectedType<IsGroup, T>, const MiddlewareDependencyBuilder>;
159+
concept IsGroup = requires {
160+
{
161+
T::kDependency
162+
} -> std::same_as<const MiddlewareDependencyBuilder&>;
163+
};
163164

164165
} // namespace impl
165166

166167
template <typename MiddlewareBefore>
167168
MiddlewareDependencyBuilder MiddlewareDependencyBuilder::Before(DependencyType type) && {
168-
if constexpr (impl::kIsGroup<MiddlewareBefore>) {
169+
if constexpr (impl::IsGroup<MiddlewareBefore>) {
169170
return std::move(*this).Before(impl::BeginOfGroup<MiddlewareBefore>(), type);
170171
}
171172
return std::move(*this).Before(MiddlewareBefore::kName, type);
172173
}
173174

174175
template <typename MiddlewareAfter>
175176
MiddlewareDependencyBuilder MiddlewareDependencyBuilder::After(DependencyType type) && {
176-
if constexpr (impl::kIsGroup<MiddlewareAfter>) {
177+
if constexpr (impl::IsGroup<MiddlewareAfter>) {
177178
return std::move(*this).After(impl::EndOfGroup<MiddlewareAfter>(), type);
178179
}
179180
return std::move(*this).After(MiddlewareAfter::kName, type);

core/include/userver/server/handlers/exceptions.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <userver/formats/json.hpp>
1111
#include <userver/utils/assert.hpp>
12-
#include <userver/utils/meta_light.hpp> // for DetectedOr
1312
#include <userver/utils/str_icase.hpp>
1413
#include <userver/utils/void_t.hpp>
1514

@@ -98,7 +97,11 @@ struct ExtraHeaders {
9897
namespace impl {
9998

10099
template <typename T>
101-
using IsExternalBodyFormatted = std::bool_constant<T::kIsExternalBodyFormatted>;
100+
concept HasExternalBodyFormatted = requires {
101+
{
102+
std::bool_constant<T::kIsExternalBodyFormatted>{}
103+
} -> std::same_as<std::true_type>;
104+
};
102105

103106
template <typename T>
104107
concept HasInternalMessage = requires(const T& t) { t.GetInternalMessage(); };
@@ -123,9 +126,7 @@ struct MessageExtractor {
123126

124127
const T& builder;
125128

126-
constexpr bool IsExternalBodyFormatted() const {
127-
return meta::DetectedOr<std::false_type, impl::IsExternalBodyFormatted, T>::value;
128-
}
129+
constexpr bool IsExternalBodyFormatted() const { return impl::HasExternalBodyFormatted<T>; }
129130

130131
std::string GetServiceCode() const {
131132
if constexpr (HasServiceCode<T>) {

core/include/userver/utils/statistics/by_label_storage.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace utils::statistics {
2828
namespace impl {
2929

3030
template <typename Metric>
31-
concept DumpableMetric = kHasWriterSupport<Metric>;
31+
concept DumpableMetric = HasWriterSupport<Metric>;
3232

3333
template <typename Metric>
3434
concept ResettableMetric = requires(Metric& m) { ResetMetric(m); };

core/include/userver/utils/statistics/metric_tag_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class MetricWrapperBase {
6262
template <typename Metric>
6363
class MetricWrapper final : public MetricWrapperBase {
6464
static_assert(
65-
HasDumpMetric<Metric> || kHasWriterSupport<Metric>,
65+
HasDumpMetric<Metric> || utils::statistics::HasWriterSupport<Metric>,
6666
"Provide a `void DumpMetric(utils::statistics::Writer&, const Metric&)`"
6767
"function in the namespace of `Metric`."
6868
);
@@ -80,19 +80,19 @@ class MetricWrapper final : public MetricWrapperBase {
8080
}
8181

8282
formats::json::ValueBuilder DeprecatedJsonDump() override {
83-
if constexpr (!kHasWriterSupport<Metric>) {
83+
if constexpr (!utils::statistics::HasWriterSupport<Metric>) {
8484
return DumpMetric(data_);
8585
}
8686
return {};
8787
}
8888

8989
void DumpToWriter(Writer& writer) override {
90-
if constexpr (kHasWriterSupport<Metric>) {
90+
if constexpr (utils::statistics::HasWriterSupport<Metric>) {
9191
writer = data_;
9292
}
9393
}
9494

95-
bool HasWriterSupport() const noexcept override { return kHasWriterSupport<Metric>; }
95+
bool HasWriterSupport() const noexcept override { return utils::statistics::HasWriterSupport<Metric>; }
9696

9797
void Reset() override {
9898
if constexpr (HasResetMetric<Metric>) {

core/src/dump/common.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ USERVER_NAMESPACE_BEGIN
1313

1414
namespace dump {
1515

16-
static_assert(impl::kIsDumpedAsNanoseconds<std::chrono::system_clock::duration>);
17-
static_assert(impl::kIsDumpedAsNanoseconds<std::chrono::steady_clock::duration>);
16+
static_assert(impl::IsDumpedAsNanoseconds<std::chrono::system_clock::duration>);
17+
static_assert(impl::IsDumpedAsNanoseconds<std::chrono::steady_clock::duration>);
1818

19-
static_assert(impl::kIsDumpedAsNanoseconds<std::chrono::nanoseconds>);
20-
static_assert(impl::kIsDumpedAsNanoseconds<std::chrono::microseconds>);
19+
static_assert(impl::IsDumpedAsNanoseconds<std::chrono::nanoseconds>);
20+
static_assert(impl::IsDumpedAsNanoseconds<std::chrono::microseconds>);
2121

2222
namespace impl {
2323

core/src/utils/statistics/http_codes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void DumpMetric(Writer& writer, const HttpCodes::Snapshot& snapshot) {
5050
}
5151
}
5252

53-
static_assert(kHasWriterSupport<HttpCodes::Snapshot>);
53+
static_assert(HasWriterSupport<HttpCodes::Snapshot>);
5454

5555
} // namespace utils::statistics
5656

core/src/utils/statistics/min_max_avg_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ USERVER_NAMESPACE_BEGIN
1616
constexpr size_t kStressNumThreads = 2;
1717
constexpr auto kStressTestDuration = std::chrono::milliseconds{500};
1818

19-
static_assert(utils::statistics::kHasWriterSupport<utils::statistics::MinMaxAvg<int>>);
19+
static_assert(utils::statistics::HasWriterSupport<utils::statistics::MinMaxAvg<int>>);
2020

2121
template <int... Values>
2222
auto GetFilledMma() {

core/src/utils/statistics/percentile_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
USERVER_NAMESPACE_BEGIN
77

8-
static_assert(utils::statistics::kHasWriterSupport<utils::statistics::Percentile<100>>);
8+
static_assert(utils::statistics::HasWriterSupport<utils::statistics::Percentile<100>>);
99

1010
TEST(Percentile, Zero) {
1111
const utils::statistics::Percentile<100> p;

0 commit comments

Comments
 (0)