array support

This commit is contained in:
Awesome Robot
2013-06-13 16:44:19 -07:00
parent a723dfb47a
commit bb39747ca9
3 changed files with 44 additions and 22 deletions

View File

@@ -83,6 +83,20 @@ namespace cereal
ar & t.value;
}
template <class T>
typename std::enable_if<std::is_array<T>::value, void>::type
save(BinaryOutputArchive & ar, T const & array)
{
ar.save_binary(array, traits::sizeofArray<T>() * sizeof(typename std::remove_all_extents<T>::type));
}
template <class T>
typename std::enable_if<std::is_array<T>::value, void>::type
load(BinaryInputArchive & ar, T & array)
{
ar.load_binary(array, traits::sizeofArray<T>() * sizeof(typename std::remove_all_extents<T>::type));
}
template <class Archive, class T>
void serialize( Archive & ar, T * & t )
{

View File

@@ -100,29 +100,10 @@ namespace cereal
}
// ######################################################################
constexpr std::false_type is_smart_ptr(...)
{
return {};
}
template <class T, class D>
constexpr std::true_type is_smart_ptr( std::unique_ptr<T, D> const & p )
{
return {};
}
template <class T>
constexpr std::true_type is_smart_ptr( std::shared_ptr<T> const & p )
constexpr size_t sizeofArray( size_t rank = std::rank<T>::value )
{
return {};
}
// ######################################################################
//! Returns true if the type T is a pointer or smart pointer (in std library)
template <class T>
constexpr bool is_any_pointer()
{
return std::is_pointer<T>() || std::is_same<std::true_type, decltype(is_smart_ptr( std::declval<T&>() ))>::value;
return rank == 0 ? 1 : std::extent<T>::value * sizeofArray<typename std::remove_extent<T>::type>( rank - 1 );
}
}
}

View File

@@ -154,8 +154,8 @@ int main()
archive & xptr2;
archive & wptr2;
archive & uptr;
}
{
std::ifstream is("ptr.txt");
cereal::BinaryInputArchive archive(is);
@@ -176,7 +176,34 @@ int main()
std::cout << uptr->a << std::endl;
}
{
std::ofstream os("arr.txt");
cereal::BinaryOutputArchive archive(os);
int a1[] = {1, 2, 3};
int a2[][2] = {{4, 5}, {6, 7}};
archive & a1;
archive & a2;
}
{
std::ifstream is("arr.txt");
cereal::BinaryInputArchive archive(is);
int a1[3];
int a2[2][2];
archive & a1;
archive & a2;
for(auto i : a1)
std::cout << i << " ";
std::cout << std::endl;
for( auto const & i : a2 )
{
for( auto j : i )
std::cout << j << " ";
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}