add more DataBuffer serialization types
All checks were successful
Linux arm64 / Build (push) Successful in 17s

This commit is contained in:
2025-07-31 14:12:33 +02:00
parent 5e9a0a9bae
commit a1a4176801
3 changed files with 124 additions and 8 deletions

View File

@@ -10,9 +10,11 @@
#include <cassert>
#include <cstdint>
#include <cstring>
#include <list>
#include <map>
#include <sp/common/VarInt.h>
#include <memory>
#include <sp/common/ByteSwapping.h>
#include <sp/common/VarInt.h>
#include <string>
#include <vector>
@@ -82,6 +84,24 @@ class DataBuffer {
*/
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
@@ -95,6 +115,19 @@ class DataBuffer {
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
@@ -102,12 +135,34 @@ class DataBuffer {
template <typename K, typename V>
DataBuffer& operator<<(const std::map<K, V>& data) {
*this << VarInt{data.size()};
for (const auto& element : data) {
*this << element.first << element.second;
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
@@ -154,6 +209,24 @@ class DataBuffer {
*/
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
@@ -170,6 +243,22 @@ class DataBuffer {
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
@@ -187,6 +276,31 @@ class DataBuffer {
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
*/