allow message serialization through DataBuffer
All checks were successful
Linux arm64 / Build (push) Successful in 15s

This commit is contained in:
2025-07-31 15:01:37 +02:00
parent a1a4176801
commit bce37f59df
3 changed files with 250 additions and 196 deletions

View File

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