serialize maps
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sp/common/VarInt.h>
|
#include <sp/common/VarInt.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
@@ -88,6 +89,19 @@ class DataBuffer {
|
|||||||
return *this;
|
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& element : data) {
|
||||||
|
*this << element.first << element.second;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Append an array to the buffer by first writing the size
|
* \brief Append an array to the buffer by first writing the size
|
||||||
* \param data The buffer to append
|
* \param data The buffer to append
|
||||||
@@ -140,6 +154,23 @@ class DataBuffer {
|
|||||||
return *this;
|
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.insert({newKey, newValue});
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Read an array from the buffer
|
* \brief Read an array from the buffer
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user