From 1565dec0f4a86e4bda7095dd7415234f7c7a84e9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 11 Dec 2022 01:21:11 +0200 Subject: [PATCH] Refactor the API for defining packets in Protocol A variadic template function provides a nicer interface than chained call operators. Required C++ standard version is now C++14 due to deduced automatic return types. --- Build | 2 +- source/net/communicator.cpp | 2 +- source/net/protocol.cpp | 2 +- source/net/protocol.h | 15 ++++++++++++--- tests/protocol.cpp | 8 ++++---- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Build b/Build index 286ea39..44bf9c0 100644 --- a/Build +++ b/Build @@ -14,7 +14,7 @@ package "mspnet" build_info { - standard CXX "c++11"; + standard CXX "c++14"; }; library "mspnet" diff --git a/source/net/communicator.cpp b/source/net/communicator.cpp index f77ada2..3ac62d7 100644 --- a/source/net/communicator.cpp +++ b/source/net/communicator.cpp @@ -23,7 +23,7 @@ public: HandshakeProtocol::HandshakeProtocol(): Protocol(0x7F00) { - add()(&Handshake::hash); + add(&Handshake::hash); } diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 95ba175..ce200a2 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -15,7 +15,7 @@ Protocol::Protocol(unsigned npi): next_packet_id(npi) { PacketDefBuilder>(*this, header_def, Serializer()) - (&PacketHeader::type)(&PacketHeader::length); + .fields(&PacketHeader::type, &PacketHeader::length); } Protocol::~Protocol() diff --git a/source/net/protocol.h b/source/net/protocol.h index ae1c3c0..35d3cb2 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -171,9 +171,12 @@ private: public: PacketDefBuilder(const Protocol &, PacketTypeDef

&, const S &); - + template - PacketDefBuilder> operator()(T P::*); + PacketDefBuilder> fields(T P::*); + + template + auto fields(T1 P::*first, T2 P::*second, Rest P::*...rest) { return fields(first).fields(second, rest...); } }; struct PacketHeader @@ -209,9 +212,15 @@ protected: template PacketDefBuilder> add(unsigned); + template + auto add(unsigned id, T P::*field, Rest P::*...rest) { return add

(id).fields(field, rest...); } + template PacketDefBuilder> add(); + template + auto add(T P::*field, Rest P::*...rest) { return add

().fields(field, rest...); } + const PacketDefBase &get_packet_by_class_id(unsigned) const; const PacketDefBase &get_packet_by_id(unsigned) const; @@ -455,7 +464,7 @@ Protocol::PacketDefBuilder::PacketDefBuilder(const Protocol &p, PacketType template template -Protocol::PacketDefBuilder> Protocol::PacketDefBuilder::operator()(T P::*ptr) +Protocol::PacketDefBuilder> Protocol::PacketDefBuilder::fields(T P::*ptr) { typename S::template Next next_ser(serializer, ptr, protocol); pktdef.set_serializer(next_ser); diff --git a/tests/protocol.cpp b/tests/protocol.cpp index 0f4b7d2..bca1161 100644 --- a/tests/protocol.cpp +++ b/tests/protocol.cpp @@ -56,10 +56,10 @@ struct Packet4 Protocol::Protocol() { - add()(&Packet1::value); - add()(&Packet2::value); - add()(&Packet3::values); - add()(&Packet4::sub1)(&Packet4::sub2); + add(&Packet1::value); + add(&Packet2::value); + add(&Packet3::values); + add(&Packet4::sub1, &Packet4::sub2); } template -- 2.45.2