summaryrefslogtreecommitdiff
path: root/common/message.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/message.cpp')
-rw-r--r--common/message.cpp53
1 files changed, 14 insertions, 39 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) {
}