Specialized save/load for vectors with default allocator

This commit is contained in:
Shane Grant 2013-06-17 11:50:41 -07:00
parent ed522117dc
commit ddcefaf15d
2 changed files with 31 additions and 6 deletions

View File

@ -6,9 +6,33 @@
namespace cereal
{
//! Serialization for std::vectors of arithmetic (but not bool) types to binary
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, default allocator
template <class T, class A> inline
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, void>::type
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
&& std::is_same<A, std::allocator<T>>::value, void>::type
save( BinaryOutputArchive & ar, std::vector<T, A> const & vector )
{
ar & vector.size(); // number of elements
ar.save_binary( vector.data(), vector.size() * sizeof(T) ); // actual data
}
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, default allocator
template <class T, class A> inline
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
&& std::is_same<A, std::allocator<T>>::value, void>::type
load( BinaryInputArchive & ar, std::vector<T, A> & vector )
{
size_t vectorSize;
ar & vectorSize;
vector.resize( vectorSize );
ar.load_binary( vector.data(), vectorSize * sizeof(T));
}
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, special allocator
template <class T, class A> inline
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
&& !std::is_same<A, std::allocator<T>>::value, void>::type
save( BinaryOutputArchive & ar, std::vector<T, A> const & vector )
{
size_t const dataSize = std::addressof(vector.back()) - std::addressof(vector.front()) + 1;
@ -18,9 +42,10 @@ namespace cereal
ar.save_binary( vector.data(), dataSize * sizeof(T) ); // actual data
}
//! Serialization for std::vectors of arithmetic (but not bool) types to binary
//! Serialization for std::vectors of arithmetic (but not bool) types to binary, special allocator
template <class T, class A> inline
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, void>::type
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value
&& !std::is_same<A, std::allocator<T>>::value, void>::type
load( BinaryInputArchive & ar, std::vector<T, A> & vector )
{
size_t vectorSize;

View File

@ -307,7 +307,7 @@ int main()
test<binary>( name.str(), data );
};
//vectorCharTest(1024*1024*1024, randomize); // 1 GB
vectorCharTest(1024*1024*1024, randomize); // 1 GB
//########################################
auto vectorPoDStructTest = [&](size_t s)
@ -323,7 +323,7 @@ int main()
vectorPoDStructTest(64);
vectorPoDStructTest(1024);
vectorPoDStructTest(1024*1024);
//vectorPoDStructTest(1024*1024*64);
vectorPoDStructTest(1024*1024*64);
//########################################
auto vectorPoDChildTest = [&](size_t s)