268 lines
6.9 KiB
C++
268 lines
6.9 KiB
C++
#pragma once
|
|
|
|
#include <sp/common/DataBuffer.h>
|
|
#include <boost/pfr.hpp>
|
|
|
|
namespace sp {
|
|
|
|
template<typename T>
|
|
using is_default_serializable = std::bool_constant<(std::is_class_v<T> && std::is_aggregate_v<T>) || !std::is_class_v<T>>;
|
|
|
|
template<typename T>
|
|
static constexpr bool is_default_serializable_v = is_default_serializable<T>::value;
|
|
|
|
|
|
/**
|
|
* \brief Append data to the buffer (converted to big endian)
|
|
*/
|
|
template <typename T, typename = typename std::enable_if_t<is_default_serializable_v<T>>>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const T& a_Data) {
|
|
if constexpr (std::is_class_v<T>) {
|
|
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 <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::shared_ptr<T>& data) {
|
|
return a_Buffer << *data;
|
|
}
|
|
|
|
/**
|
|
* \brief Append a pointer to the buffer
|
|
* \param data The data to append
|
|
*/
|
|
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unique_ptr<T>& data) {
|
|
return a_Buffer << *data;
|
|
}
|
|
|
|
/**
|
|
* \brief Append a vector to the buffer by first writing the size
|
|
* \param data The vector to append
|
|
*/
|
|
template <typename T>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::vector<T>& 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 <typename T>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::list<T>& 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 <typename K, typename V>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::map<K, V>& 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 <typename K, typename V>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unordered_map<K, V>& 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 <typename K, typename V>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::pair<K, V>& 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 <typename T, std::size_t Size>
|
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::array<T, Size>& 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 <typename T, typename = typename std::enable_if_t<is_default_serializable_v<T>>>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, T& a_Data) {
|
|
if constexpr (std::is_class_v<T>) {
|
|
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 <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::shared_ptr<T>& data) {
|
|
data = std::make_shared<T>();
|
|
return a_Buffer >> *data;
|
|
}
|
|
|
|
/**
|
|
* \brief Read a pointer
|
|
*/
|
|
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unique_ptr<T>& data) {
|
|
data = std::make_unique<T>();
|
|
return a_Buffer >> *data;
|
|
}
|
|
|
|
/**
|
|
* \brief Read a vector (size + data) from the buffer
|
|
* \pre The vector is assumed to be empty
|
|
*/
|
|
template <typename T>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::vector<T>& 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 <typename T>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::list<T>& 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 <typename K, typename V>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::map<K, V>& 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 <typename K, typename V>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unordered_map<K, V>& 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 <typename K, typename V>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::pair<K, V>& data) {
|
|
return a_Buffer >> data.first >> data.second;
|
|
}
|
|
|
|
/**
|
|
* \brief Read an array from the buffer
|
|
*/
|
|
template <std::size_t Size, typename T>
|
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::array<T, Size>& data) {
|
|
for (std::size_t i = 0; i < Size; i++) {
|
|
T newElement;
|
|
a_Buffer >> newElement;
|
|
data[i] = newElement;
|
|
}
|
|
return a_Buffer;
|
|
}
|
|
|
|
} // namespace sp
|