/* * ===================================================================================== * * OpenMiner * * Copyright (C) 2018-2020 Unarelith, Quentin Bazin * Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) * * This file is part of OpenMiner. * * OpenMiner is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * OpenMiner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with OpenMiner; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * ===================================================================================== */ #ifndef NETWORKUTILS_HPP_ #define NETWORKUTILS_HPP_ #include //====================================================================================== // std::vector //====================================================================================== #include template sf::Packet &operator<<(sf::Packet &packet, const std::vector &vec) { packet << (unsigned int)vec.size(); for (auto &it : vec) packet << it; return packet; } template sf::Packet &operator>>(sf::Packet &packet, std::vector &vec) { unsigned int size; packet >> size; for (unsigned int i = 0 ; i < size ; ++i) { T v; packet >> v; vec.emplace_back(v); } return packet; } //====================================================================================== // std::unordered_map //====================================================================================== #include template sf::Packet &operator<<(sf::Packet &packet, const std::unordered_map &map) { packet << (unsigned int)map.size(); for (auto &it : map) packet << it.first << it.second; return packet; } template sf::Packet &operator>>(sf::Packet &packet, std::unordered_map &map) { unsigned int size; packet >> size; for (unsigned int i = 0 ; i < size ; ++i) { T k; U v; packet >> k >> v; map.emplace(k, v); } return packet; } //====================================================================================== // gk::Rect //====================================================================================== #include template sf::Packet &operator<<(sf::Packet &packet, const gk::Rect &rect) { packet << rect.x << rect.y << rect.sizeX << rect.sizeY; return packet; } template sf::Packet &operator>>(sf::Packet &packet, gk::Rect &rect) { packet >> rect.x >> rect.y >> rect.sizeX >> rect.sizeY; return packet; } //====================================================================================== // gk::Box //====================================================================================== #include template sf::Packet &operator<<(sf::Packet &packet, const gk::Box &box) { packet << box.x << box.y << box.z << box.sizeX << box.sizeY << box.sizeZ; return packet; } template sf::Packet &operator>>(sf::Packet &packet, gk::Box &box) { packet >> box.x >> box.y >> box.z >> box.sizeX >> box.sizeY >> box.sizeZ; return packet; } //====================================================================================== // gk::Vector3 //====================================================================================== #include template sf::Packet &operator<<(sf::Packet &packet, const gk::Vector3 &vec) { packet << vec.x << vec.y << vec.z; return packet; } template sf::Packet &operator>>(sf::Packet &packet, gk::Vector3 &vec) { packet >> vec.x >> vec.y >> vec.z; return packet; } //====================================================================================== // gk::Color //====================================================================================== #include sf::Packet &operator<<(sf::Packet &packet, const gk::Color &color); sf::Packet &operator>>(sf::Packet &packet, gk::Color &color); //====================================================================================== // entt::entity //====================================================================================== #include sf::Packet &operator<<(sf::Packet &packet, const entt::entity &entity); sf::Packet &operator>>(sf::Packet &packet, entt::entity &entity); #endif // NETWORKUTILS_HPP_