From 1c300cbe77a7084951a080020361fc45bd190253 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 10 Dec 2022 21:54:59 +0200 Subject: [PATCH] Further simplify the protocol serializers --- source/net/protocol.h | 63 +++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/source/net/protocol.h b/source/net/protocol.h index f9a2f37..ae1c3c0 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -41,8 +41,6 @@ private: class BasicSerializer { public: - typedef T ValueType; - BasicSerializer(const Protocol &) { } std::uint64_t get_hash() const { return Traits::signature; } @@ -52,9 +50,6 @@ private: class StringSerializer { - public: - typedef std::string ValueType; - private: BasicSerializer length_serializer; @@ -69,9 +64,6 @@ private: template class ArraySerializer { - public: - typedef A ValueType; - private: BasicSerializer length_serializer; typename Traits::Serializer element_serializer; @@ -87,9 +79,6 @@ private: template class CompoundSerializer { - public: - typedef C ValueType; - private: const Serializer &serializer; @@ -101,24 +90,19 @@ private: const char *deserialize(C &, const char *, const char *) const; }; - template + template class FieldSerializer: public Head { public: template - struct Next - { - typedef FieldSerializer, typename Traits::Serializer> Type; - }; + using Next = FieldSerializer, N>; private: - typedef typename S::ValueType C::*Pointer; - - Pointer ptr; - S ser; + T C::*ptr; + typename Traits::Serializer ser; public: - FieldSerializer(const Head &, Pointer, const Protocol &); + FieldSerializer(const Head &, T C::*, const Protocol &); std::uint64_t get_hash() const; char *serialize(const C &, char *, char *) const; @@ -130,10 +114,7 @@ private: { public: template - struct Next - { - typedef FieldSerializer, typename Traits::Serializer> Type; - }; + using Next = FieldSerializer, N>; virtual ~Serializer() = default; @@ -192,7 +173,7 @@ private: PacketDefBuilder(const Protocol &, PacketTypeDef

&, const S &); template - PacketDefBuilder::Type> operator()(T P::*); + PacketDefBuilder> operator()(T P::*); }; struct PacketHeader @@ -392,31 +373,31 @@ const char *Protocol::CompoundSerializer::deserialize(C &com, const char *buf } -template -Protocol::FieldSerializer::FieldSerializer(const Head &h, Pointer p, const Protocol &proto): +template +Protocol::FieldSerializer::FieldSerializer(const Head &h, T C::*p, const Protocol &proto): Head(h), ptr(p), ser(proto) { } -template -std::uint64_t Protocol::FieldSerializer::get_hash() const +template +std::uint64_t Protocol::FieldSerializer::get_hash() const { return hash_update<64>(Head::get_hash(), ser.get_hash()); } -template -char *Protocol::FieldSerializer::serialize(const P &pkt, char *buf, char *end) const +template +char *Protocol::FieldSerializer::serialize(const C &com, char *buf, char *end) const { - buf = Head::serialize(pkt, buf, end); - return ser.serialize(pkt.*ptr, buf, end); + buf = Head::serialize(com, buf, end); + return ser.serialize(com.*ptr, buf, end); } -template -const char *Protocol::FieldSerializer::deserialize(P &pkt, const char *buf, const char *end) const +template +const char *Protocol::FieldSerializer::deserialize(C &com, const char *buf, const char *end) const { - buf = Head::deserialize(pkt, buf, end); - return ser.deserialize(pkt.*ptr, buf, end); + buf = Head::deserialize(com, buf, end); + return ser.deserialize(com.*ptr, buf, end); } @@ -474,11 +455,11 @@ Protocol::PacketDefBuilder::PacketDefBuilder(const Protocol &p, PacketType template template -Protocol::PacketDefBuilder::Type> Protocol::PacketDefBuilder::operator()(T P::*ptr) +Protocol::PacketDefBuilder> Protocol::PacketDefBuilder::operator()(T P::*ptr) { - typename S::template Next::Type next_ser(serializer, ptr, protocol); + typename S::template Next next_ser(serializer, ptr, protocol); pktdef.set_serializer(next_ser); - return PacketDefBuilder::Type>(protocol, pktdef, next_ser); + return PacketDefBuilder>(protocol, pktdef, next_ser); } } // namespace Net -- 2.45.2