#pragma once #include #include namespace sp { template using is_default_serializable = std::bool_constant<(std::is_class_v && std::is_aggregate_v) || !std::is_class_v>; template static constexpr bool is_default_serializable_v = is_default_serializable::value; /** * \brief Append data to the buffer (converted to big endian) */ template >> DataBuffer& operator<<(DataBuffer& a_Buffer, const T& a_Data) { if constexpr (std::is_class_v) { boost::pfr::for_each_field(a_Data, [&a_Buffer](const auto& a_Field) { a_Buffer << a_Field; }); } else { a_Buffer.Append(a_Data); SwapBytes(a_Buffer.data() + a_Buffer.GetReadOffset() - sizeof(T), a_Buffer.data() + a_Buffer.GetReadOffset()); } return a_Buffer; } /** * \brief Append a string to the buffer * \warning Don't use it for binary data ! * \param str The string to append */ DataBuffer& operator<<(DataBuffer& a_Buffer, const std::string& str); /** * \brief Operator << to write a DataBuffer to an ostream */ std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer); /** * \brief Append a pointer to the buffer * \param data The data to append */ template >> DataBuffer& operator<<(DataBuffer& a_Buffer, const std::shared_ptr& data) { return a_Buffer << *data; } /** * \brief Append a pointer to the buffer * \param data The data to append */ template >> DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unique_ptr& data) { return a_Buffer << *data; } /** * \brief Append a vector to the buffer by first writing the size * \param data The vector to append */ template DataBuffer& operator<<(DataBuffer& a_Buffer, const std::vector& data) { a_Buffer << VarInt{data.size()}; for (const auto& element : data) { a_Buffer << element; } return a_Buffer; } /** * \brief Append a list to the buffer by first writing the size * \param data The list to append */ template DataBuffer& operator<<(DataBuffer& a_Buffer, const std::list& data) { a_Buffer << VarInt{data.size()}; for (const auto& element : data) { a_Buffer << element; } return a_Buffer; } /** * \brief Append a map to the buffer by first writing the size * \param data The map to append */ template DataBuffer& operator<<(DataBuffer& a_Buffer, const std::map& data) { a_Buffer << VarInt{data.size()}; for (const auto& [key, value] : data) { a_Buffer << key << value; } return a_Buffer; } /** * \brief Append a map to the buffer by first writing the size * \param data The map to append */ template DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unordered_map& data) { a_Buffer << VarInt{data.size()}; for (const auto& [key, value] : data) { a_Buffer << key << value; } return a_Buffer; } /** * \brief Append a pair to the buffer * \param data The pair to append */ template DataBuffer& operator<<(DataBuffer& a_Buffer, const std::pair& data) { return a_Buffer << data.first << data.second; } /** * \brief Append an array to the buffer by first writing the size * \param data The buffer to append */ template DataBuffer& operator<<(DataBuffer& a_Buffer, const std::array& data) { for (const auto& element : data) { a_Buffer << element; } return a_Buffer; } /** * \brief Read some data from the buffer and assign to desired variable */ template >> DataBuffer& operator>>(DataBuffer& a_Buffer, T& a_Data) { if constexpr (std::is_class_v) { boost::pfr::for_each_field(a_Data, [&a_Buffer](auto& a_Field) { a_Buffer >> a_Field; }); } else { a_Buffer.Read(a_Data); SwapBytes(a_Data); } return a_Buffer; } /** * \brief Read some data from the buffer and assign to the new buffer * \param data The buffer to assign */ DataBuffer& operator>>(DataBuffer& a_Buffer, DataBuffer& data); /** * \brief Read a string from the buffer * \param str The string to assign in the new buffer * \warning Don't use it for binary data ! */ DataBuffer& operator>>(DataBuffer& a_Buffer, std::string& str); /** * \brief Read a pointer */ template >> DataBuffer& operator>>(DataBuffer& a_Buffer, std::shared_ptr& data) { data = std::make_shared(); return a_Buffer >> *data; } /** * \brief Read a pointer */ template >> DataBuffer& operator>>(DataBuffer& a_Buffer, std::unique_ptr& data) { data = std::make_unique(); return a_Buffer >> *data; } /** * \brief Read a vector (size + data) from the buffer * \pre The vector is assumed to be empty */ template DataBuffer& operator>>(DataBuffer& a_Buffer, std::vector& data) { VarInt arraySize; a_Buffer >> arraySize; for (std::size_t i = 0; i < arraySize.GetValue(); i++) { T newElement; a_Buffer >> newElement; data.push_back(newElement); } return a_Buffer; } /** * \brief Read a list (size + data) from the buffer * \pre The list is assumed to be empty */ template DataBuffer& operator>>(DataBuffer& a_Buffer, std::list& data) { VarInt arraySize; a_Buffer >> arraySize; for (std::size_t i = 0; i < arraySize.GetValue(); i++) { T newElement; a_Buffer >> newElement; data.push_back(newElement); } return a_Buffer; } /** * \brief Read a map (size + data) from the buffer * \pre The map is assumed to be empty */ template DataBuffer& operator>>(DataBuffer& a_Buffer, std::map& data) { VarInt mapSize; a_Buffer >> mapSize; for (std::size_t i = 0; i < mapSize.GetValue(); i++) { K newKey; V newValue; a_Buffer >> newKey >> newValue; data.emplace(newKey, newValue); } return a_Buffer; } /** * \brief Read a map (size + data) from the buffer * \pre The map is assumed to be empty */ template DataBuffer& operator>>(DataBuffer& a_Buffer, std::unordered_map& data) { VarInt mapSize; a_Buffer >> mapSize; for (std::size_t i = 0; i < mapSize.GetValue(); i++) { K newKey; V newValue; a_Buffer >> newKey >> newValue; data.emplace(newKey, newValue); } return a_Buffer; } /** * \brief Read a pair */ template DataBuffer& operator>>(DataBuffer& a_Buffer, std::pair& data) { return a_Buffer >> data.first >> data.second; } /** * \brief Read an array from the buffer */ template DataBuffer& operator>>(DataBuffer& a_Buffer, std::array& data) { for (std::size_t i = 0; i < Size; i++) { T newElement; a_Buffer >> newElement; data[i] = newElement; } return a_Buffer; } } // namespace sp