]> git.tdb.fi Git - libs/net.git/commitdiff
Use a static local variable for assigning packet IDs
authorMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 21:00:40 +0000 (23:00 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 21:00:40 +0000 (23:00 +0200)
This avoids the need of a definition for the static member variable of
a template class.

source/net/communicator.cpp
source/net/protocol.cpp
source/net/protocol.h
source/net/protocol_impl.h

index c473d53a7e06a5ead67d4e8ca76cdbd826cc478d..f74f4d3f3c831bfdf42c414e4cffdbde111781ab 100644 (file)
@@ -1,6 +1,5 @@
 #include <cstring>
 #include "communicator.h"
-#include "protocol_impl.h"
 #include "streamsocket.h"
 
 using namespace std;
index 17e4310f5196c2f5c599cc103c8d8d5c6cf36430..db663821c370cef914676eb1da698cdb473f5c50 100644 (file)
@@ -5,7 +5,6 @@
 #include <msp/strings/format.h>
 #include <msp/strings/lexicalcast.h>
 #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)
 { }
index fa90e6d250d87531fdc01069a90fe0cab6a90352..cd68d5148735024eaa77f9ef1ebe06a16064de72 100644 (file)
@@ -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<P> *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<P>(); }
 
                template<typename S>
                void set_serializer(const S &);
@@ -237,6 +232,11 @@ public:
        ~Protocol();
 
 private:
+       static unsigned get_next_packet_class_id();
+
+       template<typename P>
+       static unsigned get_packet_class_id();
+
        void add_packet(PacketDefBase *);
 
 protected:
@@ -267,6 +267,13 @@ private:
 };
 
 
+template<typename P>
+unsigned Protocol::get_packet_class_id()
+{
+       static unsigned id = get_next_packet_class_id();
+       return id;
+}
+
 template<typename P>
 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
 {
@@ -284,7 +291,7 @@ Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
 template<typename P>
 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
 {
-       const PacketDefBase &pdef = get_packet_by_class_id(PacketTypeDef<P>::get_static_class_id());
+       const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id<P>());
        return static_cast<const PacketTypeDef<P> &>(pdef);
 }
 
@@ -460,10 +467,7 @@ template<typename P>
 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
        PacketDefBase(i),
        compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
-{
-       if(!class_id)
-               class_id = next_class_id++;
-}
+{ }
 
 template<typename P>
 Protocol::PacketTypeDef<P>::~PacketTypeDef()
index 9ddf818853c1710b195e3cfeb512d7fb50aaa6a6..4eb28612ded0cfdbb549e79010fb3723c55e56b2 100644 (file)
@@ -1,15 +1,6 @@
 #ifndef MSP_NET_PROTOCOL_IMPL_H_
 #define MSP_NET_PROTOCOL_IMPL_H_
 
-#include "protocol.h"
-
-namespace Msp {
-namespace Net {
-
-template<typename P>
-unsigned Protocol::PacketTypeDef<P>::class_id = 0;
-
-} // namespace Net
-} // namespace Msp
+#warning "This header is deprected and should not be used"
 
 #endif