diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-27 11:47:35 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-30 21:36:41 +0100 |
commit | b01701370dafbc85fdbb50f8ba35b61b4d7db3d7 (patch) | |
tree | de1bdb54e2617ceaf6d0f160cafafb129bd7944c | |
parent | 3111e83d416d0bbba278759ae095040c7994f949 (diff) |
Change Message::BoostBase API.
-rw-r--r-- | common/message.cpp | 53 | ||||
-rw-r--r-- | common/message.h | 37 |
2 files changed, 37 insertions, 53 deletions
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 <boost/serialization/vector.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/shared_ptr.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> #include <cstring> #include <sstream> @@ -23,15 +25,17 @@ void Message::NullBase::deserialize(uint8_t* data, std::size_t bytes) { } -Message::BoostBase::BoostBase(Type t) : Base(t) { +template<class Sub> +Message::BoostBase<Sub>::BoostBase(Type t) : Base(t) { } -std::pair<uint8_t*, std::size_t> Message::BoostBase::serialize() { +template<class Sub> +std::pair<uint8_t*, std::size_t> Message::BoostBase<Sub>::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<uint8_t*, std::size_t> Message::BoostBase::serialize() { return std::pair<uint8_t*, std::size_t>(buf, s); } -void Message::BoostBase::deserialize(uint8_t* data, std::size_t bytes) { +template<class Sub> +void Message::BoostBase<Sub>::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<GameStart>(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<RoundState>(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<RoundState>(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<RoundAction>(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 <boost/archive/text_oarchive.hpp> -#include <boost/archive/text_iarchive.hpp> - #include <stdint.h> #include <vector> #include <string> @@ -57,6 +54,7 @@ namespace Message { virtual void deserialize(uint8_t* data, std::size_t bytes); }; + template<class Sub> class BoostBase : public Base { protected: BoostBase(Type t); @@ -65,8 +63,10 @@ namespace Message { virtual std::pair<uint8_t*, std::size_t> 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<class Archive> + void serialize(Archive & ar, const unsigned int version) { + ar & dynamic_cast<Sub&>(*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<GameStart> { public: typedef boost::shared_ptr<GameStart> p; @@ -117,8 +117,11 @@ namespace Message { std::vector<std::string> players; int player_id; - virtual void serialize(boost::archive::text_oarchive& ar); - virtual void deserialize(boost::archive::text_iarchive& ar); + template<class Archive> + 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<RoundState> { public: typedef boost::shared_ptr<RoundState> 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<class Archive> + void serialize(Archive & ar, const unsigned int version) { + ar & players; + ar & dora; + ar & possible_actions; + } }; - class RoundAction : public BoostBase { + class RoundAction : public BoostBase<RoundAction> { public: typedef boost::shared_ptr<RoundAction> 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<class Archive> + void serialize(Archive & ar, const unsigned int version) { + ar & action; + } }; class RoundEnd : public NullBase { |