Skip to content

Commit f084bc2

Browse files
iahsmeta-codesync[bot]
authored andcommitted
Deduplicate endRead/Write methods
Summary: Constrained template allows sharing the bodies while preserving clarity about what is accepted. Reviewed By: islamismailov Differential Revision: D89935354 fbshipit-source-id: 1a1a598ae25cc624374926763b10646403f68822
1 parent 12116c3 commit f084bc2

2 files changed

Lines changed: 20 additions & 39 deletions

File tree

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

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ class StructuredCursorReader : detail::BaseCursorReader<ProtocolReader> {
538538
}
539539
}
540540

541-
private:
542541
// Last field id the caller tried to read.
543542
FieldId fieldId_{0};
544543
// Contains last field id read from the buffer.
@@ -658,28 +657,6 @@ class ContainerCursorReader : detail::BaseCursorReader<ProtocolReader> {
658657
protocol_};
659658
}
660659

661-
template <typename CTag>
662-
void endRead(
663-
ContainerCursorReader<CTag, ProtocolReader, Contiguous>&& child) {
664-
checkState(State::Child);
665-
child.finalize();
666-
state_ = State::Active;
667-
--remaining_;
668-
}
669-
670-
/**
671-
* A faster version of endRead for when the caller doesn't intend to read any
672-
* more. Once a child reader has been abandoned, abandonRead is the only
673-
* method that can be called on any of its parents.
674-
*/
675-
template <typename CTag>
676-
void abandonRead(
677-
ContainerCursorReader<CTag, ProtocolReader, Contiguous>&& child) {
678-
checkState(State::Child);
679-
child.abandon();
680-
state_ = State::Abandoned;
681-
}
682-
683660
/**
684661
* Manual read path for containers of structured types.
685662
*
@@ -716,9 +693,8 @@ class ContainerCursorReader : detail::BaseCursorReader<ProtocolReader> {
716693
protocol_};
717694
}
718695

719-
template <typename CTag>
720-
void endRead(
721-
StructuredCursorReader<CTag, ProtocolReader, Contiguous>&& child) {
696+
template <detail::CursorReader ChildReader>
697+
void endRead(ChildReader&& child) {
722698
checkState(State::Child);
723699
child.finalize();
724700
state_ = State::Active;
@@ -730,9 +706,8 @@ class ContainerCursorReader : detail::BaseCursorReader<ProtocolReader> {
730706
* more. Once a child reader has been abandoned, abandonRead is the only
731707
* method that can be called on any of its parents.
732708
*/
733-
template <typename CTag>
734-
void abandonRead(
735-
StructuredCursorReader<CTag, ProtocolReader, Contiguous>&& child) {
709+
template <detail::CursorReader ChildReader>
710+
void abandonRead(ChildReader&& child) {
736711
checkState(State::Child);
737712
child.abandon();
738713
state_ = State::Abandoned;
@@ -1193,14 +1168,6 @@ class ContainerCursorWriter : detail::DelayedSizeCursorWriter<ProtocolWriter> {
11931168
return ContainerCursorWriter<ElementTag, ProtocolWriter>{protocol_};
11941169
}
11951170

1196-
template <typename CTag, typename PW>
1197-
void endWrite(ContainerCursorWriter<CTag, PW>&& child) {
1198-
checkState(State::Child);
1199-
child.finalize();
1200-
++n;
1201-
state_ = State::Active;
1202-
}
1203-
12041171
/**
12051172
* structured types
12061173
*
@@ -1215,8 +1182,8 @@ class ContainerCursorWriter : detail::DelayedSizeCursorWriter<ProtocolWriter> {
12151182
return StructuredCursorWriter<ElementTag, ProtocolWriter>{protocol_};
12161183
}
12171184

1218-
template <typename CTag, typename PW>
1219-
void endWrite(StructuredCursorWriter<CTag, PW>&& child) {
1185+
template <detail::CursorWriter ChildWriter>
1186+
void endWrite(ChildWriter&& child) {
12201187
checkState(State::Child);
12211188
child.finalize();
12221189
++n;

third-party/thrift/src/thrift/lib/cpp2/protocol/detail/CursorBasedSerialization.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,5 +452,19 @@ constexpr bool validateCppTypes() {
452452
return true;
453453
}
454454

455+
template <typename T>
456+
concept CursorReader = requires(T t) {
457+
t.finalize();
458+
t.abandon();
459+
typename T::State;
460+
};
461+
462+
template <typename T>
463+
concept CursorWriter = requires(T t) {
464+
t.finalize();
465+
t.abandon();
466+
typename T::State;
467+
};
468+
455469
} // namespace detail
456470
} // namespace apache::thrift

0 commit comments

Comments
 (0)