Skip to content

Commit b1c1b2e

Browse files
iahsmeta-codesync[bot]
authored andcommitted
Modernize CurSe API constraints
Reviewed By: Mizuchi, vitaut Differential Revision: D89903559 fbshipit-source-id: dce9ab9de907689bbdf036033169bf007cdb1d7c
1 parent b1c47dc commit b1c1b2e

1 file changed

Lines changed: 57 additions & 67 deletions

File tree

third-party/thrift/src/thrift/lib/cpp2/protocol/CursorBasedSerializer.h

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,8 @@ class StructuredCursorReader : detail::BaseCursorReader<ProtocolReader> {
278278
template <typename Ident>
279279
using view_type = detail::lift_view_t<native_type<Ident>, Contiguous>;
280280

281-
template <typename TypeClass, typename Ident>
282-
using enable_for =
283-
typename std::enable_if_t<type::is_a_v<type_tag<Ident>, TypeClass>, int>;
284-
285-
template <typename Ident>
286-
using enable_string_view = typename std::enable_if_t<
287-
type::is_a_v<type_tag<Ident>, type::string_c> && Contiguous,
288-
int>;
281+
template <typename Ident, typename TypeClass>
282+
static constexpr bool field_is = type::is_a_v<type_tag<Ident>, TypeClass>;
289283

290284
template <typename U, typename Ident>
291285
using maybe_optional = std::conditional_t<
@@ -339,27 +333,31 @@ class StructuredCursorReader : detail::BaseCursorReader<ProtocolReader> {
339333

340334
/** numeric types */
341335

342-
template <typename Ident, enable_for<type::number_c, Ident> = 0>
336+
template <typename Ident>
337+
requires field_is<Ident, type::number_c>
343338
[[nodiscard]] bool_if_optional<Ident> read(native_type<Ident>& value) {
344339
return readField<Ident>(
345340
[&] { op::decode<type_tag<Ident>>(*protocol_, value); }, value);
346341
}
347342

348343
/** string/binary */
349344

350-
template <typename Ident, enable_string_view<Ident> = 0>
345+
template <typename Ident>
346+
requires(field_is<Ident, type::string_c> && Contiguous)
351347
[[nodiscard]] bool_if_optional<Ident> read(std::string_view& value) {
352348
return readField<Ident>(
353349
[&] { value = detail::readStringView<ProtocolReader>(*protocol_); },
354350
value);
355351
}
356352

357-
template <typename Ident, enable_for<type::string_c, Ident> = 0>
353+
template <typename Ident>
354+
requires field_is<Ident, type::string_c>
358355
[[nodiscard]] bool_if_optional<Ident> read(std::string& value) {
359356
return readField<Ident>([&] { protocol_->readString(value); }, value);
360357
}
361358

362-
template <typename Ident, enable_for<type::string_c, Ident> = 0>
359+
template <typename Ident>
360+
requires field_is<Ident, type::string_c>
363361
[[nodiscard]] bool_if_optional<Ident> read(folly::IOBuf& value) {
364362
return readField<Ident>([&] { protocol_->readBinary(value); }, value);
365363
}
@@ -372,7 +370,8 @@ class StructuredCursorReader : detail::BaseCursorReader<ProtocolReader> {
372370
* endRead before any other methods on this object can be called.
373371
*/
374372

375-
template <typename Ident, enable_for<type::container_c, Ident> = 0>
373+
template <typename Ident>
374+
requires field_is<Ident, type::container_c>
376375
maybe_optional<
377376
ContainerCursorReader<type_tag<Ident>, ProtocolReader, Contiguous>,
378377
Ident>
@@ -416,7 +415,8 @@ class StructuredCursorReader : detail::BaseCursorReader<ProtocolReader> {
416415
* endRead before any other methods on this object can be called.
417416
*/
418417

419-
template <typename Ident, enable_for<type::structured_c, Ident> = 0>
418+
template <typename Ident>
419+
requires field_is<Ident, type::structured_c>
420420
maybe_optional<
421421
StructuredCursorReader<type_tag<Ident>, ProtocolReader, Contiguous>,
422422
Ident>
@@ -452,12 +452,9 @@ class StructuredCursorReader : detail::BaseCursorReader<ProtocolReader> {
452452
}
453453

454454
/** union type accessor */
455-
456-
template <
457-
typename...,
458-
typename U = T,
459-
typename = std::enable_if_t<is_thrift_union_v<U>>>
460-
auto readType() -> typename U::Type {
455+
auto /* T::Type */ readType()
456+
requires is_thrift_union_v<T>
457+
{
461458
return static_cast<typename T::Type>(readState_.fieldId);
462459
}
463460

@@ -585,12 +582,10 @@ class ContainerCursorReader : detail::BaseCursorReader<ProtocolReader> {
585582
typename detail::ContainerTraits<Tag>::ElementType,
586583
Contiguous>;
587584
using ElementTag = typename detail::ContainerTraits<Tag>::ElementTag;
588-
template <typename CTag, typename OwnTag>
589-
using enable_cursor_for = std::enable_if_t<
590-
(type::is_a_v<OwnTag, type::list_c> ||
591-
type::is_a_v<OwnTag, type::set_c>) &&
592-
type::is_a_v<ElementTag, CTag>,
593-
int>;
585+
template <typename CTag>
586+
static constexpr bool is_supported_element_of_type =
587+
(type::is_a_v<Tag, type::list_c> || type::is_a_v<Tag, type::set_c>) &&
588+
type::is_a_v<ElementTag, CTag>;
594589

595590
public:
596591
/**
@@ -649,11 +644,9 @@ class ContainerCursorReader : detail::BaseCursorReader<ProtocolReader> {
649644
* reader.endRead(std::move(outerReader));
650645
*/
651646

652-
template <
653-
typename...,
654-
typename U = Tag,
655-
enable_cursor_for<type::container_c, U> = 0>
656-
ContainerCursorReader<ElementTag, ProtocolReader, Contiguous> beginRead() {
647+
ContainerCursorReader<ElementTag, ProtocolReader, Contiguous> beginRead()
648+
requires is_supported_element_of_type<type::container_c>
649+
{
657650
checkState(State::Active);
658651
if (!remaining_) {
659652
folly::throw_exception<std::out_of_range>("No elements remaining");
@@ -709,11 +702,9 @@ class ContainerCursorReader : detail::BaseCursorReader<ProtocolReader> {
709702
* reader.endRead(std::move(outerReader));
710703
*/
711704

712-
template <
713-
typename...,
714-
typename U = Tag,
715-
enable_cursor_for<type::structured_c, U> = 0>
716-
StructuredCursorReader<ElementTag, ProtocolReader, Contiguous> beginRead() {
705+
StructuredCursorReader<ElementTag, ProtocolReader, Contiguous> beginRead()
706+
requires is_supported_element_of_type<type::structured_c>
707+
{
717708
checkState(State::Active);
718709
if (!remaining_) {
719710
folly::throw_exception<std::out_of_range>("No elements remaining");
@@ -908,27 +899,29 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
908899
template <typename Ident>
909900
using native_type = op::get_native_type<T, Ident>;
910901

911-
template <typename TypeClass, typename Ident>
912-
using enable_for =
913-
typename std::enable_if_t<type::is_a_v<type_tag<Ident>, TypeClass>, int>;
902+
template <typename Ident, typename TypeClass>
903+
static constexpr bool field_is = type::is_a_v<type_tag<Ident>, TypeClass>;
914904

915905
public:
916906
/** numeric types */
917907

918-
template <typename Ident, enable_for<type::number_c, Ident> = 0>
908+
template <typename Ident>
909+
requires field_is<Ident, type::number_c>
919910
void write(native_type<Ident> value) {
920911
writeField<Ident>(
921912
[&] { op::encode<type_tag<Ident>>(*protocol_, value); }, value);
922913
}
923914

924915
/** string/binary */
925916

926-
template <typename Ident, enable_for<type::string_c, Ident> = 0>
917+
template <typename Ident>
918+
requires field_is<Ident, type::string_c>
927919
void write(const folly::IOBuf& value) {
928920
writeField<Ident>([&] { protocol_->writeBinary(value); }, value);
929921
}
930922

931-
template <typename Ident, enable_for<type::string_c, Ident> = 0>
923+
template <typename Ident>
924+
requires field_is<Ident, type::string_c>
932925
void write(std::string_view value) {
933926
writeField<Ident>([&] { protocol_->writeBinary(value); }, value);
934927
}
@@ -940,7 +933,8 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
940933
* beginWrite() and the corresponding endWrite().
941934
*/
942935

943-
template <typename Ident, enable_for<type::string_c, Ident> = 0>
936+
template <typename Ident>
937+
requires field_is<Ident, type::string_c>
944938
StringCursorWriter<ProtocolWriter> beginWrite(int32_t maxSize) {
945939
beforeWriteField<Ident>();
946940
state_ = State::Child;
@@ -958,10 +952,8 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
958952

959953
/** containers */
960954

961-
template <
962-
typename Ident,
963-
typename Container,
964-
enable_for<type::container_c, Ident> = 0>
955+
template <typename Ident, typename Container>
956+
requires field_is<Ident, type::container_c>
965957
void write(const Container& value) {
966958
writeField<Ident>(
967959
[&] { op::encode<type_tag<Ident>>(*protocol_, value); }, value);
@@ -976,7 +968,8 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
976968
* beginWrite() and the corresponding endWrite().
977969
*/
978970

979-
template <typename Ident, enable_for<type::container_c, Ident> = 0>
971+
template <typename Ident>
972+
requires field_is<Ident, type::container_c>
980973
ContainerCursorWriter<type_tag<Ident>, ProtocolWriter> beginWrite() {
981974
beforeWriteField<Ident>();
982975
state_ = State::Child;
@@ -1004,7 +997,8 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
1004997
* beginWrite() and the corresponding endWrite().
1005998
*/
1006999

1007-
template <typename Ident, enable_for<type::structured_c, Ident> = 0>
1000+
template <typename Ident>
1001+
requires field_is<Ident, type::structured_c>
10081002
StructuredCursorWriter<type_tag<Ident>, ProtocolWriter> beginWrite() {
10091003
beforeWriteField<Ident>();
10101004
state_ = State::Child;
@@ -1026,7 +1020,8 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
10261020
state_ = State::Abandoned;
10271021
}
10281022

1029-
template <typename Ident, enable_for<type::structured_c, Ident> = 0>
1023+
template <typename Ident>
1024+
requires field_is<Ident, type::structured_c>
10301025
void write(const native_type<Ident>& value) {
10311026
writeField<Ident>(
10321027
[&] { op::encode<type_tag<Ident>>(*protocol_, value); }, value);
@@ -1040,7 +1035,8 @@ class StructuredCursorWriter : detail::BaseCursorWriter<ProtocolWriter> {
10401035
* migrating one of the fields to change the order in the struct or performing
10411036
* the computation in field order is preferable due to the added cost and
10421037
* complexity of using this API. */
1043-
template <typename Ident, enable_for<type::structured_c, Ident> = 0>
1038+
template <typename Ident>
1039+
requires field_is<Ident, type::structured_c>
10441040
void writeSerialized(
10451041
CursorSerializationWrapper<native_type<Ident>>&& cursorValue) {
10461042
beforeWriteField<Ident>();
@@ -1169,12 +1165,10 @@ class ContainerCursorWriter : detail::DelayedSizeCursorWriter<ProtocolWriter> {
11691165

11701166
using ElementType = typename detail::ContainerTraits<Tag>::ElementType;
11711167
using ElementTag = typename detail::ContainerTraits<Tag>::ElementTag;
1172-
template <typename CTag, typename OwnTag>
1173-
using enable_cursor_for = std::enable_if_t<
1174-
(type::is_a_v<OwnTag, type::list_c> ||
1175-
type::is_a_v<OwnTag, type::set_c>) &&
1176-
type::is_a_v<ElementTag, CTag>,
1177-
int>;
1168+
template <typename CTag>
1169+
static constexpr bool is_supported_element_of_type =
1170+
(type::is_a_v<Tag, type::list_c> || type::is_a_v<Tag, type::set_c>) &&
1171+
type::is_a_v<ElementTag, CTag>;
11781172

11791173
public:
11801174
void write(const ElementType& val) {
@@ -1191,11 +1185,9 @@ class ContainerCursorWriter : detail::DelayedSizeCursorWriter<ProtocolWriter> {
11911185
* Note: none of this writer's other methods may be called between
11921186
* beginWrite() and the corresponding endWrite().
11931187
*/
1194-
template <
1195-
typename...,
1196-
typename U = Tag,
1197-
enable_cursor_for<type::container_c, U> = 0>
1198-
ContainerCursorWriter<ElementTag, ProtocolWriter> beginWrite() {
1188+
ContainerCursorWriter<ElementTag, ProtocolWriter> beginWrite()
1189+
requires is_supported_element_of_type<type::container_c>
1190+
{
11991191
checkState(State::Active);
12001192
state_ = State::Child;
12011193
return ContainerCursorWriter<ElementTag, ProtocolWriter>{protocol_};
@@ -1215,11 +1207,9 @@ class ContainerCursorWriter : detail::DelayedSizeCursorWriter<ProtocolWriter> {
12151207
* Note: none of this writer's other methods may be called between
12161208
* beginWrite() and the corresponding endWrite().
12171209
*/
1218-
template <
1219-
typename...,
1220-
typename U = Tag,
1221-
enable_cursor_for<type::structured_c, U> = 0>
1222-
StructuredCursorWriter<ElementTag, ProtocolWriter> beginWrite() {
1210+
StructuredCursorWriter<ElementTag, ProtocolWriter> beginWrite()
1211+
requires is_supported_element_of_type<type::structured_c>
1212+
{
12231213
checkState(State::Active);
12241214
state_ = State::Child;
12251215
return StructuredCursorWriter<ElementTag, ProtocolWriter>{protocol_};

0 commit comments

Comments
 (0)