X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnet%2Fprotocol.h;h=ff18e85f905b83852a3dba5f5816032e4ae3b2da;hb=f97f21c17f24e1d426e5b5863676071a5809b185;hp=3457a9d7288cf47b915d1e4249cecbc6db3f9c55;hpb=2fbfce0c327c852d33c6713af646abf07b241108;p=libs%2Fnet.git diff --git a/source/net/protocol.h b/source/net/protocol.h index 3457a9d..ff18e85 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -14,7 +14,6 @@ class bad_packet: public std::runtime_error { public: bad_packet(const std::string &w): std::runtime_error(w) { } - virtual ~bad_packet() throw() { } }; @@ -22,7 +21,6 @@ class buffer_error: public std::runtime_error { public: buffer_error(const std::string &w): std::runtime_error(w) { } - virtual ~buffer_error() throw() { } }; @@ -38,7 +36,7 @@ private: template struct CompoundTypeDef { - virtual ~CompoundTypeDef() { } + virtual ~CompoundTypeDef() = default; virtual std::string describe() const = 0; virtual char *serialize(const C &, char *, char *) const = 0; @@ -52,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 @@ -165,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; @@ -182,24 +179,21 @@ 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; } + unsigned get_class_id() const override { return get_packet_class_id

(); } template void set_serializer(const S &); const CompoundTypeDef

&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 @@ -239,6 +233,11 @@ public: ~Protocol(); private: + static unsigned get_next_packet_class_id(); + + template + static unsigned get_packet_class_id(); + void add_packet(PacketDefBase *); protected: @@ -269,6 +268,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) { @@ -286,7 +292,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); } @@ -462,10 +468,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()