]> git.tdb.fi Git - libs/net.git/commitdiff
Refactor the API for defining packets in Protocol
authorMikko Rasa <tdb@tdb.fi>
Sat, 10 Dec 2022 23:21:11 +0000 (01:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 10 Dec 2022 23:21:11 +0000 (01:21 +0200)
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
source/net/communicator.cpp
source/net/protocol.cpp
source/net/protocol.h
tests/protocol.cpp

diff --git a/Build b/Build
index 286ea39da0faac3a12c62914e627cd421abda9ef..44bf9c0bcba88d59f726b1f29b63c23ebe7d88f8 100644 (file)
--- a/Build
+++ b/Build
@@ -14,7 +14,7 @@ package "mspnet"
 
        build_info
        {
-               standard CXX "c++11";
+               standard CXX "c++14";
        };
 
        library "mspnet"
index f77ada2159d427788a74a414285aaa67a3ad1ee4..3ac62d7e4be0906f270582ddd2576f32b346a381 100644 (file)
@@ -23,7 +23,7 @@ public:
 HandshakeProtocol::HandshakeProtocol():
        Protocol(0x7F00)
 {
-       add<Handshake>()(&Handshake::hash);
+       add<Handshake>(&Handshake::hash);
 }
 
 
index 95ba175932722a7e92c55ebdc603ff7494ce860e..ce200a2c0ea49afed1f320b0914c252a094669bd 100644 (file)
@@ -15,7 +15,7 @@ Protocol::Protocol(unsigned npi):
        next_packet_id(npi)
 {
        PacketDefBuilder<PacketHeader, Serializer<PacketHeader>>(*this, header_def, Serializer<PacketHeader>())
-               (&PacketHeader::type)(&PacketHeader::length);
+               .fields(&PacketHeader::type, &PacketHeader::length);
 }
 
 Protocol::~Protocol()
index ae1c3c010c0671a8c88968b359220f9142449a92..35d3cb2cbfe177de4214816d70643f9ff97e32bd 100644 (file)
@@ -171,9 +171,12 @@ private:
 
        public:
                PacketDefBuilder(const Protocol &, PacketTypeDef<P> &, const S &);
-               
+
                template<typename T>
-               PacketDefBuilder<P, typename S::template Next<T>> operator()(T P::*);
+               PacketDefBuilder<P, typename S::template Next<T>> fields(T P::*);
+
+               template<typename T1, typename T2, typename... Rest>
+               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<typename P>
        PacketDefBuilder<P, Serializer<P>> add(unsigned);
 
+       template<typename P, typename T, typename... Rest>
+       auto add(unsigned id, T P::*field, Rest P::*...rest) { return add<P>(id).fields(field, rest...); }
+
        template<typename P>
        PacketDefBuilder<P, Serializer<P>> add();
 
+       template<typename P, typename T, typename... Rest>
+       auto add(T P::*field, Rest P::*...rest) { return add<P>().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<P, S>::PacketDefBuilder(const Protocol &p, PacketType
 
 template<typename P, typename S>
 template<typename T>
-Protocol::PacketDefBuilder<P, typename S::template Next<T>> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
+Protocol::PacketDefBuilder<P, typename S::template Next<T>> Protocol::PacketDefBuilder<P, S>::fields(T P::*ptr)
 {
        typename S::template Next<T> next_ser(serializer, ptr, protocol);
        pktdef.set_serializer(next_ser);
index 0f4b7d2ce8b766d56476e535f524ba6d8ccf8112..bca1161fd9b727f9e694b73d610ff22c29a52c10 100644 (file)
@@ -56,10 +56,10 @@ struct Packet4
 
 Protocol::Protocol()
 {
-       add<Packet1>()(&Packet1::value);
-       add<Packet2>()(&Packet2::value);
-       add<Packet3>()(&Packet3::values);
-       add<Packet4>()(&Packet4::sub1)(&Packet4::sub2);
+       add<Packet1>(&Packet1::value);
+       add<Packet2>(&Packet2::value);
+       add<Packet3>(&Packet3::values);
+       add<Packet4>(&Packet4::sub1, &Packet4::sub2);
 }
 
 template<typename T>