Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
0c82680af0
|
|||
|
695d15588f
|
|||
|
7eb96163ab
|
|||
|
b8dafa4eb1
|
|||
|
c5b3281be7
|
|||
|
9374332cd2
|
|||
|
bce37f59df
|
@@ -61,120 +61,6 @@ class DataBuffer {
|
|||||||
std::memcpy(&m_Buffer[end_pos], &a_Data, size);
|
std::memcpy(&m_Buffer[end_pos], &a_Data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append data to the buffer (converted to big endian)
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator<<(T a_Data) {
|
|
||||||
SwapBytes(a_Data);
|
|
||||||
Append(a_Data);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a string to the buffer
|
|
||||||
* \warning Don't use it for binary data !
|
|
||||||
* \param str The string to append
|
|
||||||
*/
|
|
||||||
DataBuffer& operator<<(const std::string& str);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append data to the buffer from another const buffer
|
|
||||||
* \param data The buffer to append
|
|
||||||
*/
|
|
||||||
DataBuffer& operator<<(const DataBuffer& data);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a pointer to the buffer
|
|
||||||
* \param data The data to append
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator<<(const std::shared_ptr<T>& data) {
|
|
||||||
return *this << *data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a pointer to the buffer
|
|
||||||
* \param data The data to append
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator<<(const std::unique_ptr<T>& data) {
|
|
||||||
return *this << *data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a vector to the buffer by first writing the size
|
|
||||||
* \param data The vector to append
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator<<(const std::vector<T>& data) {
|
|
||||||
*this << VarInt{data.size()};
|
|
||||||
for (const auto& element : data) {
|
|
||||||
*this << element;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a list to the buffer by first writing the size
|
|
||||||
* \param data The list to append
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator<<(const std::list<T>& data) {
|
|
||||||
*this << VarInt{data.size()};
|
|
||||||
for (const auto& element : data) {
|
|
||||||
*this << element;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \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<<(const std::map<K, V>& data) {
|
|
||||||
*this << VarInt{data.size()};
|
|
||||||
for (const auto& [key, value] : data) {
|
|
||||||
*this << key << value;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \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<<(const std::unordered_map<K, V>& data) {
|
|
||||||
*this << VarInt{data.size()};
|
|
||||||
for (const auto& [key, value] : data) {
|
|
||||||
*this << key << value;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a pair to the buffer
|
|
||||||
* \param data The pair to append
|
|
||||||
*/
|
|
||||||
template <typename K, typename V>
|
|
||||||
DataBuffer& operator<<(const std::pair<K, V>& data) {
|
|
||||||
return *this << 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<<(const std::array<T, Size>& data) {
|
|
||||||
for (const auto& element : data) {
|
|
||||||
*this << element;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Read data into a_Data
|
* \brief Read data into a_Data
|
||||||
* \warning No endian checks
|
* \warning No endian checks
|
||||||
@@ -186,134 +72,6 @@ class DataBuffer {
|
|||||||
m_ReadOffset += sizeof(T);
|
m_ReadOffset += sizeof(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read some data from the buffer and assign to desired variable
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator>>(T& a_Data) {
|
|
||||||
Read(a_Data);
|
|
||||||
SwapBytes(a_Data);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read some data from the buffer and assign to the new buffer
|
|
||||||
* \param data The buffer to assign
|
|
||||||
*/
|
|
||||||
DataBuffer& operator>>(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>>(std::string& str);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a pointer
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator>>(std::shared_ptr<T>& data) {
|
|
||||||
data = std::make_shared<T>();
|
|
||||||
return *this >> *data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a pointer
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator>>(std::unique_ptr<T>& data) {
|
|
||||||
data = std::make_unique<T>();
|
|
||||||
return *this >> *data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a vector (size + data) from the buffer
|
|
||||||
* \pre The vector is assumed to be empty
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator>>(std::vector<T>& data) {
|
|
||||||
VarInt arraySize;
|
|
||||||
*this >> arraySize;
|
|
||||||
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
|
||||||
T newElement;
|
|
||||||
*this >> newElement;
|
|
||||||
data.push_back(newElement);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a list (size + data) from the buffer
|
|
||||||
* \pre The list is assumed to be empty
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
DataBuffer& operator>>(std::list<T>& data) {
|
|
||||||
VarInt arraySize;
|
|
||||||
*this >> arraySize;
|
|
||||||
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
|
||||||
T newElement;
|
|
||||||
*this >> newElement;
|
|
||||||
data.push_back(newElement);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a map (size + data) from the buffer
|
|
||||||
* \pre The map is assumed to be empty
|
|
||||||
*/
|
|
||||||
template <typename K, typename V>
|
|
||||||
DataBuffer& operator>>(std::map<K, V>& data) {
|
|
||||||
VarInt mapSize;
|
|
||||||
*this >> mapSize;
|
|
||||||
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
|
||||||
K newKey;
|
|
||||||
V newValue;
|
|
||||||
*this >> newKey >> newValue;
|
|
||||||
data.emplace(newKey, newValue);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a map (size + data) from the buffer
|
|
||||||
* \pre The map is assumed to be empty
|
|
||||||
*/
|
|
||||||
template <typename K, typename V>
|
|
||||||
DataBuffer& operator>>(std::unordered_map<K, V>& data) {
|
|
||||||
VarInt mapSize;
|
|
||||||
*this >> mapSize;
|
|
||||||
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
|
||||||
K newKey;
|
|
||||||
V newValue;
|
|
||||||
*this >> newKey >> newValue;
|
|
||||||
data.emplace(newKey, newValue);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a pair
|
|
||||||
*/
|
|
||||||
template <typename K, typename V>
|
|
||||||
DataBuffer& operator>>(std::pair<K, V>& data) {
|
|
||||||
return *this >> data.first >> data.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read an array from the buffer
|
|
||||||
*/
|
|
||||||
template <std::size_t Size, typename T>
|
|
||||||
DataBuffer& operator>>(std::array<T, Size>& data) {
|
|
||||||
for (std::size_t i = 0; i < Size; i++) {
|
|
||||||
T newElement;
|
|
||||||
*this >> newElement;
|
|
||||||
data[i] = newElement;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Write some data to the buffer
|
* \brief Write some data to the buffer
|
||||||
* \param buffer The buffer to write
|
* \param buffer The buffer to write
|
||||||
@@ -365,7 +123,6 @@ class DataBuffer {
|
|||||||
m_Buffer.reserve(amount);
|
m_Buffer.reserve(amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Clear the buffer
|
* \brief Clear the buffer
|
||||||
*/
|
*/
|
||||||
@@ -454,8 +211,9 @@ class DataBuffer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Operator << to write a DataBuffer to an ostream
|
* \brief Append data to the buffer from another const buffer
|
||||||
|
* \param data The buffer to append
|
||||||
*/
|
*/
|
||||||
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const DataBuffer& data);
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
455
include/sp/common/DataBufferOperators.h
Normal file
455
include/sp/common/DataBufferOperators.h
Normal file
@@ -0,0 +1,455 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/pfr.hpp>
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteRaw(DataBuffer& a_Buffer, T a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteFields(DataBuffer& a_Buffer, const T& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteSharedPtr(DataBuffer& a_Buffer, const std::shared_ptr<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteUniquePtr(DataBuffer& a_Buffer, const std::unique_ptr<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteVector(DataBuffer& a_Buffer, const std::vector<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteList(DataBuffer& a_Buffer, const std::list<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void WriteMap(DataBuffer& a_Buffer, const std::map<K, V>& a_Data);
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void WriteUnorderedMap(DataBuffer& a_Buffer, const std::unordered_map<K, V>& a_Data);
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void WritePair(DataBuffer& a_Buffer, const std::pair<K, V>& a_Data);
|
||||||
|
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
void WriteArray(DataBuffer& a_Buffer, const std::array<T, S>& a_Data);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadRaw(DataBuffer& a_Buffer, T& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadFields(DataBuffer& a_Buffer, T& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadSharedPtr(DataBuffer& a_Buffer, std::shared_ptr<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadUniquePtr(DataBuffer& a_Buffer, std::unique_ptr<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadVector(DataBuffer& a_Buffer, std::vector<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadList(DataBuffer& a_Buffer, std::list<T>& a_Data);
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void ReadMap(DataBuffer& a_Buffer, std::map<K, V>& a_Data);
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void ReadUnorderedMap(DataBuffer& a_Buffer, std::unordered_map<K, V>& a_Data);
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void ReadPair(DataBuffer& a_Buffer, std::pair<K, V>& a_Data);
|
||||||
|
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
void ReadArray(DataBuffer& a_Buffer, std::array<T, S>& a_Data);
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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>) {
|
||||||
|
details::WriteFields(a_Buffer, a_Data);
|
||||||
|
} else {
|
||||||
|
details::WriteRaw(a_Buffer, a_Data);
|
||||||
|
}
|
||||||
|
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>& a_Data) {
|
||||||
|
details::WriteSharedPtr(a_Buffer, a_Data);
|
||||||
|
return a_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::unique_ptr<T>& a_Data) {
|
||||||
|
details::WriteUniquePtr(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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>& a_Data) {
|
||||||
|
details::WriteVector(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::WriteList(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::WriteMap(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::WriteUnorderedMap(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::WritePair(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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>& a_Data) {
|
||||||
|
details::WriteArray(a_Buffer, a_Data);
|
||||||
|
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>) {
|
||||||
|
details::ReadFields(a_Buffer, a_Data);
|
||||||
|
} else {
|
||||||
|
details::ReadRaw(a_Buffer, 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& a_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>& a_Data) {
|
||||||
|
details::ReadSharedPtr(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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>& a_Data) {
|
||||||
|
details::ReadUniquePtr(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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>& a_Data) {
|
||||||
|
details::ReadVector(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::ReadList(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::ReadMap(a_Buffer, a_Data);
|
||||||
|
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>& a_Data) {
|
||||||
|
details::ReadUnorderedMap(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Read a pair
|
||||||
|
*/
|
||||||
|
template <typename K, typename V>
|
||||||
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::pair<K, V>& a_Data) {
|
||||||
|
details::ReadPair(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Read an array from the buffer
|
||||||
|
*/
|
||||||
|
template <std::size_t Size, typename T>
|
||||||
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::array<T, Size>& a_Data) {
|
||||||
|
details::ReadArray(a_Buffer, a_Data);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteRaw(DataBuffer& a_Buffer, T a_Data) {
|
||||||
|
a_Buffer.Append(a_Data);
|
||||||
|
SwapBytes(a_Buffer.data() + a_Buffer.GetReadOffset() - sizeof(T), a_Buffer.data() + a_Buffer.GetReadOffset());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteFields(DataBuffer& a_Buffer, const T& a_Data) {
|
||||||
|
boost::pfr::for_each_field(a_Data, [&a_Buffer](const auto& a_Field) { a_Buffer << a_Field; });
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteSharedPtr(DataBuffer& a_Buffer, const std::shared_ptr<T>& a_Data) {
|
||||||
|
a_Buffer << *a_Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteUniquePtr(DataBuffer& a_Buffer, const std::unique_ptr<T>& a_Data) {
|
||||||
|
a_Buffer << *a_Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteVector(DataBuffer& a_Buffer, const std::vector<T>& a_Data) {
|
||||||
|
a_Buffer << VarInt{a_Data.size()};
|
||||||
|
for (const auto& element : a_Data) {
|
||||||
|
a_Buffer << element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteList(DataBuffer& a_Buffer, const std::list<T>& a_Data) {
|
||||||
|
a_Buffer << VarInt{a_Data.size()};
|
||||||
|
for (const auto& element : a_Data) {
|
||||||
|
a_Buffer << element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void WriteMap(DataBuffer& a_Buffer, const std::map<K, V>& a_Data) {
|
||||||
|
a_Buffer << VarInt{a_Data.size()};
|
||||||
|
for (const auto& [key, value] : a_Data) {
|
||||||
|
a_Buffer << key << value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void WriteUnorderedMap(DataBuffer& a_Buffer, const std::unordered_map<K, V>& a_Data) {
|
||||||
|
a_Buffer << VarInt{a_Data.size()};
|
||||||
|
for (const auto& [key, value] : a_Data) {
|
||||||
|
a_Buffer << key << value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void WritePair(DataBuffer& a_Buffer, const std::pair<K, V>& a_Data) {
|
||||||
|
a_Buffer << a_Data.first << a_Data.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
void WriteArray(DataBuffer& a_Buffer, const std::array<T, S>& a_Data) {
|
||||||
|
for (const auto& element : a_Data) {
|
||||||
|
a_Buffer << element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadRaw(DataBuffer& a_Buffer, T& a_Data) {
|
||||||
|
a_Buffer.Read(a_Data);
|
||||||
|
SwapBytes(a_Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadFields(DataBuffer& a_Buffer, T& a_Data) {
|
||||||
|
boost::pfr::for_each_field(a_Data, [&a_Buffer](auto& a_Field) { a_Buffer >> a_Field; });
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadSharedPtr(DataBuffer& a_Buffer, std::shared_ptr<T>& a_Data) {
|
||||||
|
a_Data = std::make_shared<T>();
|
||||||
|
a_Buffer >> *a_Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadUniquePtr(DataBuffer& a_Buffer, std::unique_ptr<T>& a_Data) {
|
||||||
|
a_Data = std::make_unique<T>();
|
||||||
|
a_Buffer >> *a_Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadVector(DataBuffer& a_Buffer, std::vector<T>& a_Data) {
|
||||||
|
VarInt arraySize;
|
||||||
|
a_Buffer >> arraySize;
|
||||||
|
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
||||||
|
T newElement;
|
||||||
|
a_Buffer >> newElement;
|
||||||
|
a_Data.push_back(newElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ReadList(DataBuffer& a_Buffer, std::list<T>& a_Data) {
|
||||||
|
VarInt arraySize;
|
||||||
|
a_Buffer >> arraySize;
|
||||||
|
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
||||||
|
T newElement;
|
||||||
|
a_Buffer >> newElement;
|
||||||
|
a_Data.push_back(newElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void ReadMap(DataBuffer& a_Buffer, std::map<K, V>& a_Data) {
|
||||||
|
VarInt mapSize;
|
||||||
|
a_Buffer >> mapSize;
|
||||||
|
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
||||||
|
K newKey;
|
||||||
|
V newValue;
|
||||||
|
a_Buffer >> newKey >> newValue;
|
||||||
|
a_Data.emplace(newKey, newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void ReadUnorderedMap(DataBuffer& a_Buffer, std::unordered_map<K, V>& a_Data) {
|
||||||
|
VarInt mapSize;
|
||||||
|
a_Buffer >> mapSize;
|
||||||
|
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
||||||
|
K newKey;
|
||||||
|
V newValue;
|
||||||
|
a_Buffer >> newKey >> newValue;
|
||||||
|
a_Data.emplace(newKey, newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename K, typename V>
|
||||||
|
void ReadPair(DataBuffer& a_Buffer, std::pair<K, V>& a_Data) {
|
||||||
|
a_Buffer >> a_Data.first >> a_Data.a_Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
void ReadArray(DataBuffer& a_Buffer, std::array<T, S>& a_Data) {
|
||||||
|
for (std::size_t i = 0; i < S; i++) {
|
||||||
|
T newElement;
|
||||||
|
a_Buffer >> newElement;
|
||||||
|
a_Data[i] = newElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
} // namespace sp
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <sp/common/NonCopyable.h>
|
|
||||||
#include <sp/io/IoInterface.h>
|
#include <sp/io/IoInterface.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/pfr.hpp>
|
|
||||||
#include <sp/io/BitBuffer.h>
|
#include <sp/io/BitBuffer.h>
|
||||||
|
#include <sp/common/DataBufferOperators.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ namespace sp {
|
|||||||
template <typename TMessageFactory>
|
template <typename TMessageFactory>
|
||||||
DataBuffer MessageStream<TMessageFactory>::ReadAndDecapsulate() {
|
DataBuffer MessageStream<TMessageFactory>::ReadAndDecapsulate() {
|
||||||
VarInt messageLength;
|
VarInt messageLength;
|
||||||
messageLength.Read([this](std::uint8_t& data) { m_Stream->Read(1) >> data; });
|
messageLength.Read([this](std::uint8_t& data) {
|
||||||
|
DataBuffer buffer = m_Stream->Read(1);
|
||||||
|
data = *buffer.data();
|
||||||
|
});
|
||||||
std::size_t amount = messageLength.GetValue();
|
std::size_t amount = messageLength.GetValue();
|
||||||
DataBuffer buffer = m_Stream->Read(amount);
|
DataBuffer buffer = m_Stream->Read(amount);
|
||||||
|
|
||||||
|
|||||||
56
include/sp/io/SerializableMessage.h
Normal file
56
include/sp/io/SerializableMessage.h
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
|
||||||
|
template <typename TMessageFactory>
|
||||||
|
class SerializableMessage {
|
||||||
|
using MessageBaseType = typename TMessageFactory::MessageBaseType;
|
||||||
|
using HandlerType = typename MessageBaseType::HandlerType;
|
||||||
|
using MessageIdType = typename MessageBaseType::MessageIdType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::shared_ptr<MessageBaseType> m_Message;
|
||||||
|
|
||||||
|
SerializableMessage() {}
|
||||||
|
SerializableMessage(std::shared_ptr<MessageBaseType>&& a_Message) : m_Message(a_Message) {}
|
||||||
|
SerializableMessage(const std::shared_ptr<MessageBaseType>& a_Message) : m_Message(a_Message) {}
|
||||||
|
|
||||||
|
MessageBaseType* operator->() {
|
||||||
|
return m_Message.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
operator bool() {
|
||||||
|
return m_Message.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
const MessageBaseType* operator->() const {
|
||||||
|
return m_Message.get();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <typename TMessageFactory>
|
||||||
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const SerializableMessage<TMessageFactory>& a_Message) {
|
||||||
|
return a_Buffer << VarInt{static_cast<std::uint64_t>(a_Message->GetId())} << a_Message->Write();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TMessageFactory>
|
||||||
|
DataBuffer& operator>>(DataBuffer& a_Buffer, SerializableMessage<TMessageFactory>& a_Message) {
|
||||||
|
using MsgId = typename TMessageFactory::IdType;
|
||||||
|
using MsgBase = typename TMessageFactory::MessageBaseType;
|
||||||
|
static TMessageFactory factory;
|
||||||
|
|
||||||
|
VarInt msgId;
|
||||||
|
a_Buffer >> msgId;
|
||||||
|
|
||||||
|
auto msgPtr = std::shared_ptr<MsgBase>(factory.CreateMessage(MsgId(msgId.GetValue())).release());
|
||||||
|
|
||||||
|
a_Message = SerializableMessage<TMessageFactory>(msgPtr);
|
||||||
|
a_Message->Read(a_Buffer);
|
||||||
|
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sp
|
||||||
@@ -21,33 +21,35 @@ DataBuffer::DataBuffer(const DataBuffer& other, Data::difference_type offset) :
|
|||||||
std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer));
|
std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer& DataBuffer::operator<<(const std::string& str) {
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::string& str) {
|
||||||
std::size_t strlen = str.length() + 1; // including null character
|
std::size_t strlen = str.length() + 1; // including null character
|
||||||
Resize(GetSize() + strlen);
|
a_Buffer.Resize(a_Buffer.GetSize() + strlen);
|
||||||
std::memcpy(m_Buffer.data() + GetSize() - strlen, str.data(), strlen);
|
std::memcpy(a_Buffer.data() + a_Buffer.GetSize() - strlen, str.data(), strlen);
|
||||||
return *this;
|
return a_Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer& DataBuffer::operator<<(const DataBuffer& data) {
|
DataBuffer& operator<<(DataBuffer& a_Buffer, const DataBuffer& data) {
|
||||||
m_Buffer.insert(m_Buffer.end(), data.begin(), data.end());
|
std::size_t end = a_Buffer.GetSize();
|
||||||
return *this;
|
a_Buffer.Resize(a_Buffer.GetSize() + data.GetSize());
|
||||||
|
std::copy(data.begin(), data.end(), a_Buffer.begin() + end);
|
||||||
|
return a_Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer& DataBuffer::operator>>(std::string& str) {
|
DataBuffer& operator>>(DataBuffer& a_Buffer, std::string& str) {
|
||||||
std::size_t stringSize = strlen(reinterpret_cast<const char*>(m_Buffer.data()) + m_ReadOffset) + 1; // including null character
|
std::size_t stringSize = strlen(reinterpret_cast<const char*>(a_Buffer.data()) + a_Buffer.GetReadOffset()) + 1; // including null character
|
||||||
str.resize(stringSize);
|
str.resize(stringSize);
|
||||||
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset),
|
std::copy(a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset()),
|
||||||
m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset + stringSize), str.begin());
|
a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset() + stringSize), str.begin());
|
||||||
m_ReadOffset += stringSize;
|
a_Buffer.SetReadOffset(a_Buffer.GetReadOffset() + stringSize);
|
||||||
str.resize(stringSize - 1);
|
str.resize(stringSize - 1);
|
||||||
return *this;
|
return a_Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBuffer& DataBuffer::operator>>(DataBuffer& data) {
|
DataBuffer& operator>>(DataBuffer& a_Buffer, DataBuffer& data) {
|
||||||
data.Resize(GetSize() - m_ReadOffset);
|
data.Resize(a_Buffer.GetSize() - a_Buffer.GetReadOffset());
|
||||||
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset), m_Buffer.end(), data.begin());
|
std::copy(a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset()), a_Buffer.end(), data.begin());
|
||||||
m_ReadOffset = m_Buffer.size();
|
a_Buffer.SetReadOffset(a_Buffer.GetSize());
|
||||||
return *this;
|
return a_Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataBuffer::WriteSome(const char* buffer, std::size_t amount) {
|
void DataBuffer::WriteSome(const char* buffer, std::size_t amount) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <sp/common/VarInt.h>
|
#include <sp/common/VarInt.h>
|
||||||
|
|
||||||
#include <sp/common/DataBuffer.h>
|
#include <sp/common/DataBuffer.h>
|
||||||
|
#include <sp/common/DataBufferOperators.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|||||||
@@ -71,7 +71,6 @@ int main() {
|
|||||||
d.Dispatch(*message);
|
d.Dispatch(*message);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// write tests
|
// write tests
|
||||||
|
|
||||||
auto compress = std::make_shared<sp::ZlibCompress>();
|
auto compress = std::make_shared<sp::ZlibCompress>();
|
||||||
|
|||||||
Reference in New Issue
Block a user