]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/protocol.h
Use the override specifier when overriding
[libs/net.git] / source / net / protocol.h
index fa90e6d250d87531fdc01069a90fe0cab6a90352..ff18e85f905b83852a3dba5f5816032e4ae3b2da 100644 (file)
@@ -36,7 +36,7 @@ private:
        template<typename C>
        struct CompoundTypeDef
        {
-               virtual ~CompoundTypeDef() { }
+               virtual ~CompoundTypeDef() = default;
 
                virtual std::string describe() const = 0;
                virtual char *serialize(const C &, char *, char *) const = 0;
@@ -50,9 +50,9 @@ private:
 
                CompoundDef(const S &);
 
-               virtual std::string describe() const;
-               virtual char *serialize(const C &, char *, char *) const;
-               virtual const char *deserialize(C &, const char *, const char *) const;
+               std::string describe() const override;
+               char *serialize(const C &, char *, char *) const override;
+               const char *deserialize(C &, const char *, const char *) const override;
        };
 
        template<typename T>
@@ -163,11 +163,10 @@ private:
        protected:
                unsigned id;
 
-               static unsigned next_class_id;
-
                PacketDefBase(unsigned);
        public:
-               virtual ~PacketDefBase() { }
+               virtual ~PacketDefBase() = default;
+
                virtual unsigned get_class_id() const = 0;
                unsigned get_id() const { return id; }
                virtual std::string describe() const = 0;
@@ -180,24 +179,21 @@ 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; }
+               unsigned get_class_id() const override { return get_packet_class_id<P>(); }
 
                template<typename S>
                void set_serializer(const S &);
 
                const CompoundTypeDef<P> &get_compound() const { return *compound; }
 
-               virtual std::string describe() const { return compound->describe(); }
+               std::string describe() const override { return compound->describe(); }
                char *serialize(const P &, char *, char *) const;
                const char *deserialize(P &, const char *, const char *) const;
-               virtual const char *dispatch(ReceiverBase &, const char *, const char *) const;
+               const char *dispatch(ReceiverBase &, const char *, const char *) const override;
        };
 
        template<typename P, typename S>
@@ -237,6 +233,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 +268,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 +292,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 +468,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()