working on making generic

This commit is contained in:
Shane Grant 2013-06-19 18:03:54 -07:00
parent c67bc00bb7
commit d3a53fbcb4
5 changed files with 75 additions and 55 deletions

View File

@ -27,43 +27,47 @@
#ifndef CEREAL_BINARY_ARCHIVE_ARRAY_HPP_
#define CEREAL_BINARY_ARCHIVE_ARRAY_HPP_
#include <cereal/binary_archive/binary_archive.hpp>
#include <cereal/cereal.hpp>
#include <array>
namespace cereal
{
//! Saving for std::array primitive types to binary
template <class T, size_t N> inline
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
save( BinaryOutputArchive & ar, std::array<T, N> const & array )
template <class Archive, class T, size_t N> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>()
&& std::is_arithmetic<T>::value, void>::type
save( Archive & ar, std::array<T, N> const & array )
{
ar( binary_data( array.data(), N * sizeof(T) ) );
ar( make_nvp( "data", binary_data( array.data(), N * sizeof(T) ) ) );
}
//! Loading for std::array primitive types to binary
template <class T, size_t N> inline
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
load( BinaryInputArchive & ar, std::array<T, N> & array )
template <class Archive, class T, size_t N> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>()
&& std::is_arithmetic<T>::value, void>::type
load( Archive & ar, std::array<T, N> & array )
{
ar( binary_data( array.data(), N * sizeof(T) ) );
ar( make_nvp( "data", binary_data( array.data(), N * sizeof(T) ) ) );
}
//! Saving for std::array all other types to binary
template <class T, size_t N> inline
typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
save( BinaryOutputArchive & ar, std::array<T, N> const & array )
template <class Archive, class T, size_t N> inline
typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>()
|| !std::is_arithmetic<T>::value, void>::type
save( Archive & ar, std::array<T, N> const & array )
{
for( auto const & i : array )
ar( i );
ar( make_nvp( "item", i ) );
}
//! Loading for std::array all other types to binary
template <class T, size_t N> inline
typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
load( BinaryInputArchive & ar, std::array<T, N> & array )
template <class Archive, class T, size_t N> inline
typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>()
|| !std::is_arithmetic<T>::value, void>::type
load( Archive & ar, std::array<T, N> & array )
{
for( auto & i : array )
ar( i );
ar( make_nvp( "item", i ) );
}
} // namespace cereal

View File

@ -27,7 +27,7 @@
#ifndef CEREAL_BINARY_ARCHIVE_BITSET_HPP_
#define CEREAL_BINARY_ARCHIVE_BITSET_HPP_
#include <cereal/binary_archive/binary_archive.hpp>
#include <cereal/cereal.hpp>
#include <bitset>
namespace cereal

View File

@ -27,57 +27,61 @@
#ifndef CEREAL_BINARY_ARCHIVE_VECTOR_HPP_
#define CEREAL_BINARY_ARCHIVE_VECTOR_HPP_
#include <cereal/binary_archive/binary_archive.hpp>
#include <cereal/cereal.hpp>
#include <vector>
namespace cereal
{
//! 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
template <class Archive, class T, class A> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>()
&& 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 )
save( Archive & ar, std::vector<T, A> const & vector )
{
ar( vector.size() ); // number of elements
ar( binary_data( vector.data(), vector.size() * sizeof(T) ) );
ar( make_nvp( "size", vector.size() ) ); // number of elements
ar( make_nvp( "data", binary_data( vector.data(), vector.size() * sizeof(T) ) ) );
}
//! 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
template <class Archive, class T, class A> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>()
&& 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 )
load( Archive & ar, std::vector<T, A> & vector )
{
size_t vectorSize;
ar( vectorSize );
ar( make_nvp( "size", vectorSize) );
vector.resize( vectorSize );
ar( binary_data( vector.data(), vectorSize * sizeof(T) ) );
ar( make_nvp( "data", binary_data( 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
template <class Archive, class T, class A> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>()
&& 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 )
save( Archive & ar, std::vector<T, A> const & vector )
{
size_t const dataSize = std::addressof(vector.back()) - std::addressof(vector.front()) + 1;
ar( vector.size() ); // number of elements
ar( dataSize ); // size of data (may be larger due to allocator strategy)
ar( binary_data( vector.data(), dataSize * sizeof(T) ) );
ar( make_nvp( "size", vector.size() ) ); // number of elements
ar( make_nvp( "datasize", dataSize) ); // size of data (may be larger due to allocator strategy)
ar( make_nvp( "data", binary_data( vector.data(), dataSize * 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
template <class Archive, class T, class A> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>()
&& 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 )
load( Archive & ar, std::vector<T, A> & vector )
{
size_t vectorSize;
size_t dataSize;
ar( vectorSize,
dataSize );
ar( make_nvp( "size", vectorSize ),
make_nvp( "datasize", dataSize ) );
vector.resize( vectorSize );
@ -85,26 +89,30 @@ namespace cereal
}
//! Serialization for non-arithmetic (and bool) vector types to binary
template <class T, class A> inline
typename std::enable_if<!std::is_arithmetic<T>::value || std::is_same<T, bool>::value, void>::type
save( BinaryOutputArchive & ar, std::vector<T, A> const & vector )
template <class Archive, class T, class A> inline
typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>()
|| !std::is_arithmetic<T>::value
|| std::is_same<T, bool>::value, void>::type
save( Archive & ar, std::vector<T, A> const & vector )
{
ar( vector.size() ); // number of elements
ar( make_nvp( "size", vector.size() ) ); // number of elements
for( auto it = vector.begin(), end = vector.end(); it != end; ++it )
ar( *it );
ar( make_nvp( "item", *it) );
}
//! Serialization for non-arithmetic (and bool) vector types from binary
template <class T, class A> inline
typename std::enable_if<!std::is_arithmetic<T>::value || std::is_same<T, bool>::value, void>::type
load( BinaryInputArchive & ar, std::vector<T, A> & vector )
template <class Archive, class T, class A> inline
typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>()
|| !std::is_arithmetic<T>::value
|| std::is_same<T, bool>::value, void>::type
load( Archive & ar, std::vector<T, A> & vector )
{
size_t size;
ar( size );
ar( CEREAL_NVP(size) );
vector.resize( size );
for( auto it = vector.begin(), end = vector.end(); it != end; ++it )
ar( *it );
ar( make_nvp( "item", *it) );
}
} // namespace cereal

View File

@ -69,15 +69,23 @@ namespace cereal
the NameValuePair will store a copy of it instead of a reference. Thus you should
only pass r-values in cases where this makes sense, such as the result of some
size() call. In either case, any constness will be stripped away */
NameValuePair( std::string const & n, T && v ) : name(n), value(const_cast<Type>(v)) {}
NameValuePair( char const * n, T && v ) : name(n), value(const_cast<Type>(v)) {}
std::string name;
//std::string name;
char const * name;
Type value;
};
//! Creates a name value pair
template <class T> inline
NameValuePair<T> make_nvp( std::string const & name, T && value )
{
return {name.c_str(), std::forward<T>(value)};
}
//! Creates a name value pair
template <class T> inline
NameValuePair<T> make_nvp( const char * name, T && value )
{
return {name, std::forward<T>(value)};
}

View File

@ -135,7 +135,7 @@ struct binary
template <class SerializationT, class DataT>
void test( std::string const & name,
DataT const & data,
size_t numAverages = 10,
size_t numAverages = 100,
bool validateData = false )
{
std::cout << "-----------------------------------" << std::endl;
@ -332,7 +332,7 @@ int main()
test<binary>( name.str(), data );
};
vectorCharTest(1024*1024*1024, randomize); // 1 GB
vectorCharTest(1024*1024*64, randomize); // 1 GB
//########################################
auto vectorPoDStructTest = [&](size_t s)
@ -348,7 +348,7 @@ int main()
vectorPoDStructTest(64);
vectorPoDStructTest(1024);
vectorPoDStructTest(1024*1024);
vectorPoDStructTest(1024*1024*64);
vectorPoDStructTest(1024*1024*4);
//########################################
auto vectorPoDChildTest = [&](size_t s)