#include "ChessConverter.h" #include #include #include using namespace std; namespace rst { namespace converter { ChessConverter::ChessConverter() : Converter(rsc::runtime::typeName(), RSB_TYPE_TAG(Chess)), converter( new rsb::converter::ProtocolBufferConverter) { } ChessConverter::~ChessConverter() { } string ChessConverter::getWireSchema() const { return converter->getWireSchema(); } string ChessConverter::getClassName() const { return "ChessConverter"; } string ChessConverter::serialize(const rsb::AnnotatedData& data, string& wire) { if (data.first != getDataType()) { throw rsb::converter::SerializationException( "Called with unsupported data type " + data.first); } boost::shared_ptr chess = boost::static_pointer_cast< Chess>(data.second); boost::shared_ptr protoChess( new chess::Chess); protoChess->set_turn(rst::chess::Player(chess->getTurn())); for (vector
::iterator it = chess->getFigures().begin(); it != chess->getFigures().end(); ++it) { // convert event to notification Figure& figure = *it; chess::Figure* protoFigure = protoChess->add_figures(); FigureConverter::figureToProto(figure, *protoFigure); } return converter->serialize( make_pair( rsc::runtime::typeName< chess::Chess>(), protoChess), wire); } rsb::AnnotatedData ChessConverter::deserialize(const std::string& wireSchema, const string& wire) { if (wireSchema != getWireSchema()) { throw rsb::converter::SerializationException("Unexpected wire schema " + wireSchema); } boost::shared_ptr protoChess = boost::static_pointer_cast( converter->deserialize(wireSchema, wire).second); boost::shared_ptr chess(new Chess); chess->getFigures().clear(); chess->setTurn(Player(protoChess->turn())); // iterate over all notifications in each scope set for (int i = 0; i < protoChess->figures_size(); ++i) { // convert the notification back to an event const chess::Figure& protoFigure = protoChess->figures(i); Figure figure; FigureConverter::protoToFigure(figure, protoFigure); chess->getFigures().push_back(figure); } return make_pair(getDataType(), chess); } } }