]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/protocol.h
Use a static local variable for assigning packet IDs
[libs/net.git] / source / net / protocol.h
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()