refactor DataBuffer
All checks were successful
Linux arm64 / Build (push) Successful in 16s

This commit is contained in:
2025-08-01 12:44:05 +02:00
parent c5b3281be7
commit b8dafa4eb1
8 changed files with 301 additions and 273 deletions

View File

@@ -61,29 +61,6 @@ class DataBuffer {
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 Read data into a_Data
* \warning No endian checks
@@ -95,29 +72,6 @@ class DataBuffer {
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 Write some data to the buffer
* \param buffer The buffer to write
@@ -169,7 +123,6 @@ class DataBuffer {
m_Buffer.reserve(amount);
}
/**
* \brief Clear the buffer
*/
@@ -251,6 +204,8 @@ class DataBuffer {
return m_Buffer == other.m_Buffer;
}
void insert(iterator a_DestBegin, const_iterator a_SrcBegin, const_iterator a_SrcEnd);
iterator begin();
iterator end();
const_iterator begin() const;
@@ -258,10 +213,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
#include "DataBuffer.inl"

View File

@@ -1,201 +0,0 @@
#pragma once
namespace sp {
/**
* \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& buffer, const std::shared_ptr<T>& data) {
return 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& buffer, const std::unique_ptr<T>& data) {
return 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& buffer, const std::vector<T>& data) {
buffer << VarInt{data.size()};
for (const auto& element : data) {
buffer << element;
}
return 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& buffer, const std::list<T>& data) {
buffer << VarInt{data.size()};
for (const auto& element : data) {
buffer << element;
}
return 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& buffer, const std::map<K, V>& data) {
buffer << VarInt{data.size()};
for (const auto& [key, value] : data) {
buffer << key << value;
}
return 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& buffer, const std::unordered_map<K, V>& data) {
buffer << VarInt{data.size()};
for (const auto& [key, value] : data) {
buffer << key << value;
}
return buffer;
}
/**
* \brief Append a pair to the buffer
* \param data The pair to append
*/
template <typename K, typename V>
DataBuffer& operator<<(DataBuffer& buffer, const std::pair<K, V>& data) {
return 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& buffer, const std::array<T, Size>& data) {
for (const auto& element : data) {
buffer << element;
}
return buffer;
}
/**
* \brief Read a pointer
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator>>(DataBuffer& buffer, std::shared_ptr<T>& data) {
data = std::make_shared<T>();
return buffer >> *data;
}
/**
* \brief Read a pointer
*/
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
DataBuffer& operator>>(DataBuffer& buffer, std::unique_ptr<T>& data) {
data = std::make_unique<T>();
return 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& buffer, std::vector<T>& data) {
VarInt arraySize;
buffer >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
buffer >> newElement;
data.push_back(newElement);
}
return buffer;
}
/**
* \brief Read a list (size + data) from the buffer
* \pre The list is assumed to be empty
*/
template <typename T>
DataBuffer& operator>>(DataBuffer& buffer, std::list<T>& data) {
VarInt arraySize;
buffer >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
buffer >> newElement;
data.push_back(newElement);
}
return 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& buffer, std::map<K, V>& data) {
VarInt mapSize;
buffer >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
buffer >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return 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& buffer, std::unordered_map<K, V>& data) {
VarInt mapSize;
buffer >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
buffer >> newKey >> newValue;
data.emplace(newKey, newValue);
}
return buffer;
}
/**
* \brief Read a pair
*/
template <typename K, typename V>
DataBuffer& operator>>(DataBuffer& buffer, std::pair<K, V>& data) {
return buffer >> data.first >> data.second;
}
/**
* \brief Read an array from the buffer
*/
template <std::size_t Size, typename T>
DataBuffer& operator>>(DataBuffer& buffer, std::array<T, Size>& data) {
for (std::size_t i = 0; i < Size; i++) {
T newElement;
buffer >> newElement;
data[i] = newElement;
}
return buffer;
}
} // namespace sp

View File

@@ -0,0 +1,268 @@
#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

View File

@@ -1,7 +1,7 @@
#pragma once
#include <boost/pfr.hpp>
#include <sp/io/BitBuffer.h>
#include <sp/common/DataBufferOperators.h>
namespace sp {
namespace details {

View File

@@ -8,7 +8,10 @@ namespace sp {
template <typename TMessageFactory>
DataBuffer MessageStream<TMessageFactory>::ReadAndDecapsulate() {
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();
DataBuffer buffer = m_Stream->Read(amount);