From bf3b20f58d400c3961ce4d162cf314ff3ed4895e Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Tue, 30 Nov 2010 23:44:26 +0100 Subject: Simplified Message API. All messages are now serialized by Boost.Serialize. --- common/message.h | 113 +++++++++++++++++++++++++++---------------------------- 1 file changed, 56 insertions(+), 57 deletions(-) (limited to 'common/message.h') diff --git a/common/message.h b/common/message.h index 1c9276d..d2eb5d7 100644 --- a/common/message.h +++ b/common/message.h @@ -36,109 +36,96 @@ namespace Message { class Base { protected: - Base(Type t); + Base(Type t) : type(t) {} + virtual ~Base() {} public: const Type type; - - virtual std::pair serialize() = 0; - virtual void deserialize(uint8_t* data, std::size_t bytes) = 0; - }; - - class NullBase : public Base { - protected: - NullBase(Type t); - - public: - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); - }; - - template - class BoostBase : public Base { - protected: - BoostBase(Type t); - - public: - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); - - template - void serialize(Archive & ar, const unsigned int version) { - ar & dynamic_cast(*this); - } }; class Hello : public Base { public: typedef boost::shared_ptr p; - Hello(); - Hello(const std::string& v); + Hello(const std::string& v = "") : Base(Types::Hello), version(v) {} std::string version; - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + template + void serialize(Archive & ar, const unsigned int v) { + ar & version; + } }; class Login : public Base { public: typedef boost::shared_ptr p; - Login(); - Login(const std::string& n); + Login(const std::string& n = "") : Base(Types::Login), nick(n) {} std::string nick; - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + template + void serialize(Archive & ar, const unsigned int v) { + ar & nick; + } }; class LoginResponse : public Base { public: typedef boost::shared_ptr p; - LoginResponse(); - LoginResponse(bool ok); + LoginResponse(bool ok = false) : Base(Types::LoginResponse), login_ok(ok) {} bool login_ok; - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + template + void serialize(Archive & ar, const unsigned int v) { + ar & login_ok; + } }; - class GameStart : public BoostBase { + class GameStart : public Base { public: typedef boost::shared_ptr p; - GameStart(); + GameStart() : Base(Types::GameStart) {} std::vector players; int player_id; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & players; ar & player_id; } }; - class Ready : public NullBase { + class Ready : public Base { public: typedef boost::shared_ptr p; - Ready(); + Ready() : Base(Types::Ready) {} + + template + void serialize(Archive & ar, const unsigned int v) { + + } }; - class RoundStart : public NullBase { + class RoundStart : public Base { public: typedef boost::shared_ptr p; - RoundStart(); + RoundStart() : Base(Types::RoundStart) {} + + template + void serialize(Archive & ar, const unsigned int v) { + + } }; - class RoundState : public BoostBase { + class RoundState : public Base { public: typedef boost::shared_ptr p; @@ -152,15 +139,21 @@ namespace Message { Tiles pond; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & hand; ar & open; ar & pond; } }; - RoundState(); - RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a); + RoundState() : Base(Types::RoundState) {} + RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a) + : Base(Types::RoundState), dora(d), possible_actions(a) { + players[0] = pl_d; + players[1] = pl_r; + players[2] = pl_u; + players[3] = pl_l; + } Player players[4]; @@ -169,33 +162,39 @@ namespace Message { Actions possible_actions; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & players; ar & dora; ar & possible_actions; } }; - class RoundAction : public BoostBase { + class RoundAction : public Base { public: typedef boost::shared_ptr p; - RoundAction(); + RoundAction() : Base(Types::RoundAction) {} Action action; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & action; } }; - class RoundEnd : public NullBase { + class RoundEnd : public Base { public: typedef boost::shared_ptr p; - RoundEnd(); + RoundEnd() : Base(Types::RoundEnd) {} + + template + void serialize(Archive & ar, const unsigned int v) { + + } }; + typedef boost::shared_ptr p; }; -- cgit v1.2.3