raw serialization of std::map

This commit is contained in:
Daniel Larimer 2014-05-19 15:22:19 -04:00
parent 013b9a867f
commit 87c314facb
2 changed files with 26 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <fc/io/raw_fwd.hpp>
#include <fc/filesystem.hpp>
#include <fc/exception/exception.hpp>
#include <map>
#define MAX_ARRAY_ALLOC_SIZE (1024*1024*10)
@ -319,6 +320,29 @@ namespace fc {
value.insert( std::move(tmp) );
}
}
template<typename Stream, typename K, typename V>
inline void pack( Stream& s, const std::map<K,V>& value ) {
pack( s, unsigned_int(value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
fc::raw::pack( s, *itr );
++itr;
}
}
template<typename Stream, typename K, typename V>
inline void unpack( Stream& s, std::map<K,V>& value )
{
unsigned_int size; unpack( s, size );
value.clear();
FC_ASSERT( size.value*(sizeof(K)+sizeof(V)) < MAX_ARRAY_ALLOC_SIZE );
for( uint32_t i = 0; i < size.value; ++i )
{
std::pair<K,V> tmp;
fc::raw::unpack( s, tmp );
value.insert( std::move(tmp) );
}
}
template<typename Stream, typename T>

View file

@ -30,6 +30,8 @@ namespace fc {
template<typename Stream, typename K, typename V> inline void pack( Stream& s, const std::unordered_map<K,V>& value );
template<typename Stream, typename K, typename V> inline void unpack( Stream& s, std::unordered_map<K,V>& value );
template<typename Stream, typename K, typename V> inline void pack( Stream& s, const std::map<K,V>& value );
template<typename Stream, typename K, typename V> inline void unpack( Stream& s, std::map<K,V>& value );
template<typename Stream, typename K, typename V> inline void pack( Stream& s, const std::pair<K,V>& value );
template<typename Stream, typename K, typename V> inline void unpack( Stream& s, std::pair<K,V>& value );