From b01701370dafbc85fdbb50f8ba35b61b4d7db3d7 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 27 Nov 2010 11:47:35 +0100 Subject: Change Message::BoostBase API. --- common/message.cpp | 53 ++++++++++++++--------------------------------------- common/message.h | 37 +++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 53 deletions(-) (limited to 'common') diff --git a/common/message.cpp b/common/message.cpp index df687e4..1429736 100644 --- a/common/message.cpp +++ b/common/message.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include @@ -23,15 +25,17 @@ void Message::NullBase::deserialize(uint8_t* data, std::size_t bytes) { } -Message::BoostBase::BoostBase(Type t) : Base(t) { +template +Message::BoostBase::BoostBase(Type t) : Base(t) { } -std::pair Message::BoostBase::serialize() { +template +std::pair Message::BoostBase::serialize() { std::ostringstream os; boost::archive::text_oarchive oa(os); - serialize(oa); + oa & *this; std::size_t s = os.str().size(); uint8_t* buf = new uint8_t[s]; @@ -41,11 +45,12 @@ std::pair Message::BoostBase::serialize() { return std::pair(buf, s); } -void Message::BoostBase::deserialize(uint8_t* data, std::size_t bytes) { +template +void Message::BoostBase::deserialize(uint8_t* data, std::size_t bytes) { std::istringstream is(std::string((char*)data, bytes)); boost::archive::text_iarchive ia(is); - deserialize(ia); + ia & *this; } Message::Hello::Hello() : Base(Types::Hello) { @@ -110,20 +115,10 @@ void Message::LoginResponse::deserialize(uint8_t* data, std::size_t bytes) { login_ok = bool(data[0]); } -Message::GameStart::GameStart() : BoostBase(Types::GameStart) { +Message::GameStart::GameStart() : BoostBase(Types::GameStart) { } -void Message::GameStart::serialize(boost::archive::text_oarchive& ar) { - ar & players; - ar & player_id; -} - -void Message::GameStart::deserialize(boost::archive::text_iarchive& ar) { - ar & players; - ar & player_id; -} - Message::Ready::Ready() : NullBase(Types::Ready) { } @@ -132,42 +127,22 @@ Message::RoundStart::RoundStart() : NullBase(Types::RoundStart) { } -Message::RoundState::RoundState() : BoostBase(Types::RoundState) { +Message::RoundState::RoundState() : BoostBase(Types::RoundState) { } Message::RoundState::RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a) - : BoostBase(Types::RoundState), dora(d), possible_actions(a) { + : BoostBase(Types::RoundState), dora(d), possible_actions(a) { players[0] = pl_d; players[1] = pl_r; players[2] = pl_u; players[3] = pl_l; } -void Message::RoundState::serialize(boost::archive::text_oarchive& ar) { - ar & players; - ar & dora; - ar & possible_actions; -} - -void Message::RoundState::deserialize(boost::archive::text_iarchive& ar) { - ar & players; - ar & dora; - ar & possible_actions; -} - -Message::RoundAction::RoundAction() : BoostBase(Types::RoundAction) { +Message::RoundAction::RoundAction() : BoostBase(Types::RoundAction) { } -void Message::RoundAction::serialize(boost::archive::text_oarchive& ar) { - ar & action; -} - -void Message::RoundAction::deserialize(boost::archive::text_iarchive& ar) { - ar & action; -} - Message::RoundEnd::RoundEnd() : NullBase(Types::RoundEnd) { } diff --git a/common/message.h b/common/message.h index 0d11f02..1c9276d 100644 --- a/common/message.h +++ b/common/message.h @@ -6,9 +6,6 @@ using boost::make_shared; using boost::dynamic_pointer_cast; -#include -#include - #include #include #include @@ -57,6 +54,7 @@ namespace Message { virtual void deserialize(uint8_t* data, std::size_t bytes); }; + template class BoostBase : public Base { protected: BoostBase(Type t); @@ -65,8 +63,10 @@ namespace Message { virtual std::pair serialize(); virtual void deserialize(uint8_t* data, std::size_t bytes); - virtual void serialize(boost::archive::text_oarchive& ar) = 0; - virtual void deserialize(boost::archive::text_iarchive& ar) = 0; + template + void serialize(Archive & ar, const unsigned int version) { + ar & dynamic_cast(*this); + } }; class Hello : public Base { @@ -108,7 +108,7 @@ namespace Message { virtual void deserialize(uint8_t* data, std::size_t bytes); }; - class GameStart : public BoostBase { + class GameStart : public BoostBase { public: typedef boost::shared_ptr p; @@ -117,8 +117,11 @@ namespace Message { std::vector players; int player_id; - virtual void serialize(boost::archive::text_oarchive& ar); - virtual void deserialize(boost::archive::text_iarchive& ar); + template + void serialize(Archive & ar, const unsigned int version) { + ar & players; + ar & player_id; + } }; class Ready : public NullBase { @@ -135,7 +138,7 @@ namespace Message { RoundStart(); }; - class RoundState : public BoostBase { + class RoundState : public BoostBase { public: typedef boost::shared_ptr p; @@ -165,11 +168,15 @@ namespace Message { Actions possible_actions; - virtual void serialize(boost::archive::text_oarchive& ar); - virtual void deserialize(boost::archive::text_iarchive& ar); + template + void serialize(Archive & ar, const unsigned int version) { + ar & players; + ar & dora; + ar & possible_actions; + } }; - class RoundAction : public BoostBase { + class RoundAction : public BoostBase { public: typedef boost::shared_ptr p; @@ -177,8 +184,10 @@ namespace Message { Action action; - virtual void serialize(boost::archive::text_oarchive& ar); - virtual void deserialize(boost::archive::text_iarchive& ar); + template + void serialize(Archive & ar, const unsigned int version) { + ar & action; + } }; class RoundEnd : public NullBase { -- cgit v1.2.3