Skip to content

Commit 571b8ca

Browse files
committed
feat postgres: modernize the code
Some of the very rarely used template variables were replaced with concepts without the `k` prefix. Migration hint: ``` find . -type f -name '*pp' | xargs sed -i \ -e 's/kIsMappedToArray/IsMappedToArray/g' \ -e 's/kIsMappedToUserType/IsMappedToUserType/g' \ -e 's/io::traits::kHasParser/io::traits::HasParser/g' \ -e 's/io::traits::HasFormatter/io::traits::HasFormatter/g' \ -e 's/kCanReserve/CanReserve/g' \ -e 's/kCanResize/CanResize/g' \ -e 's/kCanClear/CanClear/g' \ -e 's/traits::kIsFixedSizeContainer/meta::kIsFixedSizeContainer/g' \ ``` Improvements of compile times is neglectable Tests: протестировано CI commit_hash:b392a44128530f45865abe735344df8faa568540
1 parent 516e21f commit 571b8ca

31 files changed

+217
-287
lines changed

postgresql/include/userver/storages/postgres/detail/is_in_namespace.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ constexpr bool IsInNamespaceImpl(std::string_view nsp) {
3232
}
3333

3434
template <typename T>
35-
constexpr bool IsInNamespace(std::string_view nsp) {
36-
return IsInNamespaceImpl<std::remove_const_t<std::decay_t<T>>>(nsp);
37-
}
38-
39-
template <typename T>
40-
inline constexpr bool kIsInStdNamespace = IsInNamespace<T>("std");
35+
concept IsInStdNamespace = detail::IsInNamespaceImpl<std::remove_cvref_t<T>>("std");
4136
template <typename T>
42-
inline constexpr bool kIsInBoostNamespace = IsInNamespace<T>("boost");
37+
concept IsInBoostNamespace = detail::IsInNamespaceImpl<std::remove_cvref_t<T>>("boost");
4338

4439
} // namespace storages::postgres::detail
4540

postgresql/include/userver/storages/postgres/field.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class FieldView final {
5050

5151
template <typename T>
5252
size_type To(T&& val) const {
53-
using ValueType = typename std::decay<T>::type;
53+
using ValueType = std::remove_cvref_t<T>;
5454
auto fb = GetBuffer();
5555
return ReadNullable(fb, std::forward<T>(val), io::traits::IsNullable<ValueType>{});
5656
}
@@ -63,7 +63,7 @@ class FieldView final {
6363

6464
template <typename T>
6565
size_type ReadNullable(const io::FieldBuffer& fb, T&& val, std::true_type) const {
66-
using ValueType = typename std::decay<T>::type;
66+
using ValueType = std::remove_cvref_t<T>;
6767
using NullSetter = io::traits::GetSetNull<ValueType>;
6868
if (fb.is_null) {
6969
NullSetter::SetNull(val);
@@ -85,7 +85,7 @@ class FieldView final {
8585

8686
template <typename T>
8787
void Read(const io::FieldBuffer& buffer, T&& val) const {
88-
using ValueType = typename std::decay<T>::type;
88+
using ValueType = std::remove_cvref_t<T>;
8989
io::traits::CheckParser<ValueType>();
9090
try {
9191
io::ReadBuffer(buffer, std::forward<T>(val), GetTypeBufferCategories());

postgresql/include/userver/storages/postgres/io/array_types.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct HasFixedDimensionsImpl {
4545
template <typename T>
4646
struct HasFixedDimensions
4747
: std::conditional_t<
48-
kIsFixedSizeContainer<T>,
48+
meta::kIsFixedSizeContainer<T>,
4949
detail::HasFixedDimensionsImpl<T>,
5050
BoolConstant<!kIsCompatibleContainer<T>>>::type {};
5151

@@ -76,7 +76,7 @@ struct JoinSequences<std::integer_sequence<T, U...>, std::integer_sequence<T, V.
7676

7777
template <typename T>
7878
struct FixedDimensionsImpl {
79-
static_assert(kIsFixedSizeContainer<T>, "Container must have fixed size");
79+
static_assert(meta::kIsFixedSizeContainer<T>, "Container must have fixed size");
8080
using type = typename JoinSequences<
8181
std::integer_sequence<std::size_t, kDimensionSize<T>>,
8282
typename FixedDimensions<typename T::value_type>::type>::type;
@@ -97,7 +97,7 @@ constexpr std::array<T, sizeof...(Values)> MakeArray(const std::integer_sequence
9797
template <typename T>
9898
struct FixedDimensions
9999
: std::conditional_t<
100-
kIsFixedSizeContainer<T>,
100+
meta::kIsFixedSizeContainer<T>,
101101
detail::FixedDimensionsImpl<T>,
102102
detail::FixedDimensionsNonContainer<T>> {};
103103

@@ -182,7 +182,7 @@ struct ArrayBinaryParser : BufferParserBase<Container> {
182182
}
183183
template <typename Element>
184184
bool CheckDimensions([[maybe_unused]] DimensionConstIterator dim) const {
185-
if constexpr (traits::kIsFixedSizeContainer<Element>) {
185+
if constexpr (meta::kIsFixedSizeContainer<Element>) {
186186
// check subdimensions
187187
if (*dim != traits::kDimensionSize<Element>) {
188188
return false;
@@ -221,10 +221,10 @@ struct ArrayBinaryParser : BufferParserBase<Container> {
221221
Element& elem
222222
) {
223223
if constexpr (traits::kIsCompatibleContainer<Element>) {
224-
if constexpr (traits::kCanClear<Element>) {
224+
if constexpr (traits::CanClear<Element>) {
225225
elem.clear();
226226
}
227-
if constexpr (traits::kCanReserve<Element>) {
227+
if constexpr (traits::CanReserve<Element>) {
228228
elem.reserve(*dim);
229229
}
230230
auto it = GetInserter(elem);
@@ -394,7 +394,7 @@ constexpr bool EnableArrayParser() {
394394
return false;
395395
} else {
396396
using ElementType = typename traits::ContainerFinalElement<Container>::type;
397-
return traits::kHasParser<ElementType>;
397+
return traits::HasParser<ElementType>;
398398
}
399399
}
400400
template <typename Container>
@@ -406,7 +406,7 @@ constexpr bool EnableArrayFormatter() {
406406
return false;
407407
} else {
408408
using ElementType = typename traits::ContainerFinalElement<Container>::type;
409-
return traits::kHasFormatter<ElementType>;
409+
return traits::HasFormatter<ElementType>;
410410
}
411411
}
412412
template <typename Container>
@@ -427,12 +427,12 @@ constexpr bool IsTypeMappedToSystemArray() {
427427
namespace traits {
428428

429429
template <typename T>
430-
struct Input<T, std::enable_if_t<!detail::kCustomParserDefined<T> && io::detail::kEnableArrayParser<T>>> {
430+
struct Input<T, std::enable_if_t<!detail::CustomParserDefined<T> && io::detail::kEnableArrayParser<T>>> {
431431
using type = io::detail::ArrayBinaryParser<T>;
432432
};
433433

434434
template <typename T>
435-
struct Output<T, std::enable_if_t<!detail::kCustomFormatterDefined<T> && io::detail::kEnableArrayFormatter<T>>> {
435+
struct Output<T, std::enable_if_t<!detail::CustomFormatterDefined<T> && io::detail::kEnableArrayFormatter<T>>> {
436436
using type = io::detail::ArrayBinaryFormatter<T>;
437437
};
438438

postgresql/include/userver/storages/postgres/io/bitstring.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ template <typename BitContainerRef, BitContainerInterface, BitStringType>
7878
struct BitStringRefWrapper {
7979
static_assert(std::is_reference<BitContainerRef>::value, "The container must be passed by reference");
8080

81-
using BitContainer = std::decay_t<BitContainerRef>;
81+
using BitContainer = std::remove_cvref_t<BitContainerRef>;
8282
static_assert(
8383
io::traits::kIsBitStringCompatible<BitContainer>,
8484
"This C++ type cannot be used with PostgreSQL 'bit' and 'bit "
@@ -155,7 +155,7 @@ struct BufferParser<postgres::detail::BitStringRefWrapper<
155155
BitContainerRef,
156156
postgres::detail::BitContainerInterface::kCommon,
157157
kBitStringType>&&> {
158-
using BitContainer = std::decay_t<BitContainerRef>;
158+
using BitContainer = std::remove_cvref_t<BitContainerRef>;
159159
using BaseType = detail::BufferParserBase<postgres::detail::BitStringRefWrapper<
160160
BitContainerRef,
161161
postgres::detail::BitContainerInterface::kCommon,
@@ -196,7 +196,7 @@ struct BufferParser<postgres::detail::BitStringRefWrapper<
196196
BitContainerRef,
197197
postgres::detail::BitContainerInterface::kFlags,
198198
kBitStringType>&&> {
199-
using BitContainer = std::decay_t<BitContainerRef>;
199+
using BitContainer = std::remove_cvref_t<BitContainerRef>;
200200
using BaseType = detail::BufferParserBase<postgres::detail::BitStringRefWrapper<
201201
BitContainerRef,
202202
postgres::detail::BitContainerInterface::kFlags,
@@ -236,7 +236,7 @@ struct BufferFormatter<postgres::detail::BitStringRefWrapper<
236236
BitContainerRef,
237237
postgres::detail::BitContainerInterface::kCommon,
238238
kBitStringType>> {
239-
using BitContainer = std::decay_t<BitContainerRef>;
239+
using BitContainer = std::remove_cvref_t<BitContainerRef>;
240240
using BaseType = detail::BufferFormatterBase<postgres::detail::BitStringRefWrapper<
241241
BitContainerRef,
242242
postgres::detail::BitContainerInterface::kCommon,
@@ -273,7 +273,7 @@ struct BufferFormatter<postgres::detail::BitStringRefWrapper<
273273
BitContainerRef,
274274
postgres::detail::BitContainerInterface::kFlags,
275275
kBitStringType>> {
276-
using BitContainer = std::decay_t<BitContainerRef>;
276+
using BitContainer = std::remove_cvref_t<BitContainerRef>;
277277
using BaseType = detail::BufferFormatterBase<postgres::detail::BitStringRefWrapper<
278278
BitContainerRef,
279279
postgres::detail::BitContainerInterface::kFlags,

postgresql/include/userver/storages/postgres/io/buffer_io.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct CheckForBufferWriterODR final {
6868
/// @brief Read a value from input buffer
6969
template <typename T>
7070
void ReadBuffer(const FieldBuffer& buffer, T&& value) {
71-
using ValueType = std::decay_t<T>;
71+
using ValueType = std::remove_cvref_t<T>;
7272
traits::CheckParser<ValueType>();
7373
using BufferReader = typename traits::IO<ValueType>::ParserType;
7474
static_assert(
@@ -91,7 +91,7 @@ void ReadBuffer(const FieldBuffer& buffer, T&& value) {
9191

9292
template <typename T>
9393
void ReadBuffer(const FieldBuffer& buffer, T&& value, const TypeBufferCategory& categories) {
94-
using ValueType = std::decay_t<T>;
94+
using ValueType = std::remove_cvref_t<T>;
9595
traits::CheckParser<ValueType>();
9696
using BufferReader = typename traits::IO<ValueType>::ParserType;
9797
if (traits::kParserBufferCategory<BufferReader> != buffer.category) {

postgresql/include/userver/storages/postgres/io/composite_types.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct CompositeBinaryParser : BufferParserBase<T> {
3636
using BaseType::BaseType;
3737

3838
void operator()(FieldBuffer buffer, const TypeBufferCategory& categories) {
39-
if constexpr (!traits::kIsMappedToUserType<T>) {
39+
if constexpr (!traits::IsMappedToUserType<T>) {
4040
InitRecordParser();
4141
}
4242

@@ -169,9 +169,8 @@ struct AssertTupleHasParsers;
169169
template <typename... Members>
170170
struct AssertTupleHasParsers<std::tuple<Members...>> : std::true_type {
171171
static_assert(
172-
(HasParser<std::decay_t<Members>>::value && ...),
173-
"No parser for member. Probably you forgot to include "
174-
"file with parser or to define your own. Please see page "
172+
(HasParser<std::remove_cvref_t<Members>> && ...),
173+
"No parser for member. Probably you forgot to include file with parser or to define your own. Please see page "
175174
"`uPg: Supported data types` for more information"
176175
);
177176
};
@@ -182,10 +181,9 @@ struct AssertTupleHasFormatters;
182181
template <typename... Members>
183182
struct AssertTupleHasFormatters<std::tuple<Members...>> : std::true_type {
184183
static_assert(
185-
(HasFormatter<std::decay_t<Members>>::value && ...),
186-
"No formatter for member. Probably you forgot to "
187-
"include file with formatter or to define your own. Please see page "
188-
"`uPg: Supported data types` for more information"
184+
(HasFormatter<std::remove_cvref_t<Members>> && ...),
185+
"No formatter for member. Probably you forgot to include file with formatter or to define your own. "
186+
"Please see page `uPg: Supported data types` for more information"
189187
);
190188
};
191189

@@ -204,13 +202,13 @@ constexpr bool AssertHasCompositeFormatters() {
204202
} // namespace detail
205203

206204
template <typename T>
207-
struct Input<T, std::enable_if_t<!detail::kCustomParserDefined<T> && kIsRowType<T>>> {
205+
struct Input<T, std::enable_if_t<!detail::CustomParserDefined<T> && kIsRowType<T>>> {
208206
static_assert(detail::AssertHasCompositeParsers<T>());
209207
using type = io::detail::CompositeBinaryParser<T>;
210208
};
211209

212210
template <typename T>
213-
struct Output<T, std::enable_if_t<!detail::kCustomFormatterDefined<T> && kIsMappedToUserType<T> && kIsRowType<T>>> {
211+
struct Output<T, std::enable_if_t<!detail::CustomFormatterDefined<T> && IsMappedToUserType<T> && kIsRowType<T>>> {
214212
static_assert(detail::AssertHasCompositeFormatters<T>());
215213
using type = io::detail::CompositeBinaryFormatter<T>;
216214
};

postgresql/include/userver/storages/postgres/io/enum_types.hpp

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,13 @@ struct Codegen {};
7575

7676
namespace detail {
7777

78-
template <typename Enum, typename Enable = USERVER_NAMESPACE::utils::void_t<>>
79-
struct AreEnumeratorsDefined : std::false_type {};
80-
81-
template <typename Enum>
82-
struct AreEnumeratorsDefined<Enum, USERVER_NAMESPACE::utils::void_t<decltype(CppToUserPg<Enum>::enumerators)*>>
83-
: std::true_type {};
84-
8578
template <typename Enum>
8679
struct Enumerators {
8780
static_assert(std::is_enum<Enum>(), "Type must be an enumeration");
8881
static_assert(
89-
AreEnumeratorsDefined<Enum>(),
90-
"CppToUserPg for an enumeration must contain a static "
91-
"`enumerators` member of `utils::TrivialBiMap` type or "
92-
"`storages::postgres::io::detail::Enumerator[]`"
82+
requires { CppToUserPg<Enum>::enumerators; },
83+
"CppToUserPg for an enumeration must contain a static `enumerators` member of `utils::TrivialBiMap` type or "
84+
"`storages::postgres::io::detail::Enumerator[]`."
9385
);
9486
using Type = decltype(CppToUserPg<Enum>::enumerators);
9587
};
@@ -223,31 +215,18 @@ struct EnumConverter {
223215
PostgresType operator()(const Enum& user_data) const { return ToString(user_data); }
224216
};
225217

226-
template <typename>
227-
auto HasParseImpl(...) -> std::false_type;
228-
229-
template <typename Enum>
230-
auto HasParseImpl(int
231-
) -> decltype(Parse(std::declval<std::string_view>(), std::declval<formats::parse::To<Enum>>()), std::true_type{});
232-
233-
template <typename Enum>
234-
struct HasParse : decltype(storages::postgres::io::detail::HasParseImpl<Enum>(0)) {};
235-
236-
template <typename>
237-
auto HasToStringImpl(...) -> std::false_type;
238-
239218
template <typename Enum>
240-
auto HasToStringImpl(int) -> decltype(ToString(std::declval<Enum>()), std::true_type{});
219+
concept HasParse = requires(std::string_view sw) { Parse(sw, formats::parse::To<Enum>{}); };
241220

242221
template <typename Enum>
243-
struct HasToString : decltype(storages::postgres::io::detail::HasToStringImpl<Enum>(0)) {};
222+
concept HasToString = requires(Enum e) { ToString(e); };
244223

245224
} // namespace detail
246225

247226
namespace traits {
248227

249228
template <typename T>
250-
struct Input<T, std::enable_if_t<std::is_enum<T>() && !detail::kCustomParserDefined<T> && IsMappedToUserType<T>()>> {
229+
struct Input<T, std::enable_if_t<std::is_enum_v<T> && !detail::CustomParserDefined<T> && IsMappedToUserType<T>>> {
251230
using type = io::detail::EnumParser<T>;
252231
};
253232

@@ -256,8 +235,7 @@ struct ParserBufferCategory<io::detail::EnumParser<T>>
256235
: std::integral_constant<BufferCategory, BufferCategory::kPlainBuffer> {};
257236

258237
template <typename T>
259-
struct
260-
Output<T, std::enable_if_t<std::is_enum<T>() && !detail::kCustomFormatterDefined<T> && IsMappedToUserType<T>()>> {
238+
struct Output<T, std::enable_if_t<std::is_enum_v<T> && !detail::CustomFormatterDefined<T> && IsMappedToUserType<T>>> {
261239
using type = io::detail::EnumFormatter<T>;
262240
};
263241

@@ -268,8 +246,8 @@ template <typename Enum>
268246
struct Input<
269247
Enum,
270248
std::enable_if_t<
271-
std::is_enum<Enum>() && !detail::kCustomParserDefined<Enum> && !IsMappedToUserType<Enum>() &&
272-
storages::postgres::io::detail::HasToString<Enum>::value>> {
249+
std::is_enum_v<Enum> && !detail::CustomParserDefined<Enum> && !IsMappedToUserType<Enum> &&
250+
storages::postgres::io::detail::HasToString<Enum>>> {
273251
using type = TransformParser<Enum, std::string, storages::postgres::io::detail::EnumConverter<Enum>>;
274252
};
275253

@@ -280,8 +258,8 @@ template <typename Enum>
280258
struct Output<
281259
Enum,
282260
std::enable_if_t<
283-
std::is_enum<Enum>() && !detail::kCustomFormatterDefined<Enum> && !IsMappedToUserType<Enum>() &&
284-
storages::postgres::io::detail::HasParse<Enum>::value>> {
261+
std::is_enum_v<Enum> && !detail::CustomFormatterDefined<Enum> && !IsMappedToUserType<Enum> &&
262+
storages::postgres::io::detail::HasParse<Enum>>> {
285263
using type = TransformFormatter<Enum, std::string, storages::postgres::io::detail::EnumConverter<Enum>>;
286264
};
287265

postgresql/include/userver/storages/postgres/io/field_buffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ std::size_t FieldBuffer::Read(T&& value, const TypeBufferCategory& categories, s
4444

4545
template <typename T>
4646
std::size_t FieldBuffer::ReadRaw(T&& value, const TypeBufferCategory& categories, BufferCategory cat) {
47-
using ValueType = std::decay_t<T>;
47+
using ValueType = std::remove_cvref_t<T>;
4848
Integer field_length{0};
4949
auto consumed = Read(field_length, BufferCategory::kPlainBuffer);
5050
if (field_length == kPgNullBufferSize) {

postgresql/include/userver/storages/postgres/io/ip.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ inline constexpr char kIsCidr = 1;
4141
inline constexpr char kIsInet = 0;
4242

4343
template <typename T>
44-
inline constexpr bool kIsNetworkType =
44+
concept NetworkType =
4545
std::is_same_v<T, USERVER_NAMESPACE::utils::ip::NetworkV4> ||
4646
std::is_same_v<T, USERVER_NAMESPACE::utils::ip::NetworkV6>;
4747

4848
template <typename T>
49-
inline constexpr bool kIsAddressType =
49+
concept AddressType =
5050
std::is_same_v<T, USERVER_NAMESPACE::utils::ip::AddressV4> ||
5151
std::is_same_v<T, USERVER_NAMESPACE::utils::ip::AddressV6>;
5252

@@ -76,7 +76,7 @@ struct IpBufferFormatterBase : BufferFormatterBase<T> {
7676
}
7777
};
7878

79-
template <typename T, typename = std::enable_if_t<kIsAddressType<T>>>
79+
template <AddressType T>
8080
struct AddressNetworkBuffer : IpBufferFormatterBase<T> {
8181
using BaseType = IpBufferFormatterBase<T>;
8282
using BaseType::BaseType;
@@ -96,7 +96,7 @@ struct AddressNetworkBuffer : IpBufferFormatterBase<T> {
9696
}
9797
};
9898

99-
template <typename T, typename = std::enable_if_t<kIsNetworkType<T>>>
99+
template <NetworkType T>
100100
struct NetworkBufferFormatter : IpBufferFormatterBase<T> {
101101
using BaseType = IpBufferFormatterBase<T>;
102102
using BaseType::BaseType;
@@ -186,7 +186,7 @@ struct IpBufferParserBase : BufferParserBase<T> {
186186
}
187187
};
188188

189-
template <typename T, typename = std::enable_if_t<kIsNetworkType<T>>>
189+
template <NetworkType T>
190190
struct NetworkBufferParser : IpBufferParserBase<T> {
191191
using BaseType = IpBufferParserBase<T>;
192192
using BaseType::BaseType;
@@ -206,7 +206,7 @@ struct NetworkBufferParser : IpBufferParserBase<T> {
206206
}
207207
};
208208

209-
template <typename T, typename = std::enable_if_t<kIsAddressType<T>>>
209+
template <AddressType T>
210210
struct AddressBufferParser : detail::IpBufferParserBase<T> {
211211
using BaseType = detail::IpBufferParserBase<T>;
212212
using BaseType::BaseType;

0 commit comments

Comments
 (0)