/*! * serializes this object into byte vector v, reallocating it if needed * returns the size of serialized object */ ssize_t serialize_to (std::vector<byte_t>& v) const { size_t const old_size (v.size()); size_t const new_size (serial_size() + old_size); try { v.resize (new_size, 0); } catch (std::length_error& l) { gu_throw_error(EMSGSIZE) << "length_error: " << l.what(); } catch (...) { gu_throw_error(ENOMEM) << "could not resize to " << new_size << " bytes"; } try { return serialize_to (&v[old_size], new_size - old_size); } catch (...) { v.resize (old_size); throw; } }
void serialize_to(OutputIterator output, BombermanGame& gamestate) { serialize_to(output, static_cast<uint32_t>(gamestate.remaining_time())); for(auto& x : gamestate.current_level) { char deserialized_entity; dispatch(functions( [&](const EmptySpace&){ deserialized_entity = 0; }, [&](const DestructibleWall&){ deserialized_entity = 1; }, [&](const NondestructibleWall&){ deserialized_entity = 2; }, [&](const Bomb&){ deserialized_entity = 3; }), x); *output++ = deserialized_entity; } for(auto& player : gamestate.players) { Point pos = player.position(); assert(in_range(pos.x(), std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max())); assert(in_range(pos.y(), std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max())); serialize_to(output, static_cast<uint16_t>(pos.x())); serialize_to(output, static_cast<uint16_t>(pos.y())); // przerobione pola serialize_to(output, static_cast<uint8_t>(player.move_progress_percent())); assert(in_range(player.direction(), -4, 4)); serialize_to(output, static_cast<uint8_t>(player.direction())); // TODO: bomby serialize_to(output, static_cast<uint8_t>(0)); serialize_to(output, static_cast<uint8_t>(!player.is_hurt())); } }