From: Mikko Rasa Date: Fri, 9 Dec 2022 21:00:40 +0000 (+0200) Subject: Use a static local variable for assigning packet IDs X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ee01d294f9078fbf17dc6a0a645ad3a342c49b1a;p=libs%2Fnet.git Use a static local variable for assigning packet IDs This avoids the need of a definition for the static member variable of a template class. --- diff --git a/source/net/communicator.cpp b/source/net/communicator.cpp index c473d53..f74f4d3 100644 --- a/source/net/communicator.cpp +++ b/source/net/communicator.cpp @@ -1,6 +1,5 @@ #include #include "communicator.h" -#include "protocol_impl.h" #include "streamsocket.h" using namespace std; diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 17e4310..db66382 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -5,7 +5,6 @@ #include #include #include "protocol.h" -#include "protocol_impl.h" using namespace std; @@ -26,6 +25,12 @@ Protocol::~Protocol() delete i->second; } +unsigned Protocol::get_next_packet_class_id() +{ + static unsigned next_id = 1; + return next_id++; +} + void Protocol::add_packet(PacketDefBase *pdef) { PacketDefBase *&ptr = packet_class_defs[pdef->get_class_id()]; @@ -154,8 +159,6 @@ const char *Protocol::StringSerializer::deserialize(string &str, const char *buf } -unsigned Protocol::PacketDefBase::next_class_id = 1; - Protocol::PacketDefBase::PacketDefBase(unsigned i): id(i) { } diff --git a/source/net/protocol.h b/source/net/protocol.h index fa90e6d..cd68d51 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -163,8 +163,6 @@ private: protected: unsigned id; - static unsigned next_class_id; - PacketDefBase(unsigned); public: virtual ~PacketDefBase() { } @@ -180,14 +178,11 @@ private: private: CompoundTypeDef

*compound; - static unsigned class_id; - public: PacketTypeDef(unsigned); ~PacketTypeDef(); - static unsigned get_static_class_id() { return class_id; } - virtual unsigned get_class_id() const { return class_id; } + virtual unsigned get_class_id() const { return get_packet_class_id

(); } template void set_serializer(const S &); @@ -237,6 +232,11 @@ public: ~Protocol(); private: + static unsigned get_next_packet_class_id(); + + template + static unsigned get_packet_class_id(); + void add_packet(PacketDefBase *); protected: @@ -267,6 +267,13 @@ private: }; +template +unsigned Protocol::get_packet_class_id() +{ + static unsigned id = get_next_packet_class_id(); + return id; +} + template Protocol::PacketDefBuilder > Protocol::add(unsigned id) { @@ -284,7 +291,7 @@ Protocol::PacketDefBuilder > Protocol::add() template const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const { - const PacketDefBase &pdef = get_packet_by_class_id(PacketTypeDef

::get_static_class_id()); + const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id

()); return static_cast &>(pdef); } @@ -460,10 +467,7 @@ template Protocol::PacketTypeDef

::PacketTypeDef(unsigned i): PacketDefBase(i), compound(new CompoundDef >(NullSerializer

())) -{ - if(!class_id) - class_id = next_class_id++; -} +{ } template Protocol::PacketTypeDef

::~PacketTypeDef() diff --git a/source/net/protocol_impl.h b/source/net/protocol_impl.h index 9ddf818..4eb2861 100644 --- a/source/net/protocol_impl.h +++ b/source/net/protocol_impl.h @@ -1,15 +1,6 @@ #ifndef MSP_NET_PROTOCOL_IMPL_H_ #define MSP_NET_PROTOCOL_IMPL_H_ -#include "protocol.h" - -namespace Msp { -namespace Net { - -template -unsigned Protocol::PacketTypeDef

::class_id = 0; - -} // namespace Net -} // namespace Msp +#warning "This header is deprected and should not be used" #endif