mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-02 21:28:06 +02:00
Having trouble getting non_member versions to work
This commit is contained in:
117
cereal.hpp
117
cereal.hpp
@@ -2,91 +2,62 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include "traits.hpp"
|
||||
|
||||
namespace cereal
|
||||
{
|
||||
namespace detail
|
||||
|
||||
class BinaryOutputArchive
|
||||
{
|
||||
template<typename> struct Void { typedef void type; };
|
||||
public:
|
||||
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_serialize: std::false_type {};
|
||||
BinaryOutputArchive() {}
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_serialize< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().serialize( std::declval<A&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_load: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_load< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().load( std::declval<A&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_save: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_save< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().save( std::declval<A&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
template <class T, class A>
|
||||
constexpr bool has_split()
|
||||
BinaryOutputArchive(std::ostream & stream)
|
||||
{
|
||||
return has_load<T, A>() && has_save<T, A>();
|
||||
}
|
||||
|
||||
template <class T, class A>
|
||||
constexpr bool is_serializable()
|
||||
{
|
||||
return has_split<T, A>() ^ has_serialize<T, A>();
|
||||
}
|
||||
}
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
//t.serialize(*this, traits::version<T>::value)
|
||||
return *this;
|
||||
}
|
||||
|
||||
class BinaryOutputArchive
|
||||
{
|
||||
public:
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
//serialize(*this, t, traits::version<T>::value)
|
||||
return *this;
|
||||
}
|
||||
|
||||
BinaryOutputArchive() {}
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_member_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
//t.save(*this, traits::version<T>::value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
BinaryOutputArchive(std::ostream & stream)
|
||||
{
|
||||
}
|
||||
template<class T>
|
||||
typename std::enable_if<traits::is_serializable<T, BinaryOutputArchive>() && traits::has_non_member_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
//save(*this, t, traits::version<T>::value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<detail::is_serializable<T, BinaryOutputArchive>() && detail::has_serialize<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
template<class T>
|
||||
typename std::enable_if<!traits::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Saving non-split member" << std::endl;
|
||||
//t.serialize(*this, detail::version<T>::value)
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<detail::is_serializable<T, BinaryOutputArchive>() && detail::has_split<T, BinaryOutputArchive>(),
|
||||
BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
std::cout << "Saving split member" << std::endl;
|
||||
//t.save(*this, detail::version<T>::value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<!detail::is_serializable<T, BinaryOutputArchive>(), BinaryOutputArchive &>::type
|
||||
operator & (T const & t)
|
||||
{
|
||||
static_assert(detail::is_serializable<T, BinaryOutputArchive>(), "Trying to serialize an unserializable type.\n\n"
|
||||
static_assert(traits::is_serializable<T, BinaryOutputArchive>(), "Trying to serialize an unserializable type.\n\n"
|
||||
"Types must either have a serialize function, or separate save/load functions (but not both).\n"
|
||||
"Serialize functions generally have the following signature:\n\n"
|
||||
"template<class Archive>\n"
|
||||
@@ -96,8 +67,6 @@ namespace cereal
|
||||
" }\n\n" );
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
37
test.cpp
37
test.cpp
@@ -38,13 +38,20 @@ struct Test3
|
||||
int a;
|
||||
std::string b;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(int & ar, unsigned int version)
|
||||
{
|
||||
ar & a;
|
||||
}
|
||||
};
|
||||
|
||||
#if 1
|
||||
//namespace cereal
|
||||
//{
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, Test3 & t, unsigned int version)
|
||||
{
|
||||
//ar & t.a;
|
||||
//ar & t.b;
|
||||
}
|
||||
//}
|
||||
#endif
|
||||
|
||||
// ###################################
|
||||
struct Test4
|
||||
{
|
||||
@@ -70,6 +77,19 @@ struct Test4
|
||||
}
|
||||
};
|
||||
|
||||
//// ######################################################################
|
||||
//template<typename> struct Void { typedef void type; };
|
||||
//
|
||||
//template<typename T, class A, typename Sfinae = void>
|
||||
//struct has_non_member_serialize2: std::false_type {};
|
||||
//
|
||||
//template<typename T, class A>
|
||||
//struct has_non_member_serialize2< T, A,
|
||||
// typename Void<
|
||||
//decltype( cereal::serialize( std::declval<A&>(), std::declval<T&>(), 0 ) )
|
||||
// >::type
|
||||
// >: std::true_type {};
|
||||
|
||||
// ######################################################################
|
||||
int main()
|
||||
{
|
||||
@@ -80,13 +100,16 @@ int main()
|
||||
Test3 t3;
|
||||
Test4 t4;
|
||||
|
||||
archive & t1;
|
||||
archive & t2;
|
||||
//archive & t1;
|
||||
//archive & t2;
|
||||
//archive & t3;
|
||||
//archive & t4;
|
||||
|
||||
//cereal::serialize(archive, t3, 0);
|
||||
|
||||
|
||||
std::cout << cereal::traits::has_non_member_serialize<Test2, cereal::BinaryOutputArchive>() << std::endl;
|
||||
//std::cout << has_non_member_serialize2<Test3, cereal::BinaryOutputArchive>() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
112
traits.hpp
Normal file
112
traits.hpp
Normal file
@@ -0,0 +1,112 @@
|
||||
namespace cereal
|
||||
{
|
||||
struct BogusType {};
|
||||
|
||||
//template<class X, class Y, class Z>
|
||||
// BogusType serialize(X&,Y&,Z&);
|
||||
void load();
|
||||
void save();
|
||||
|
||||
namespace traits
|
||||
{
|
||||
template<typename> struct Void { typedef void type; };
|
||||
|
||||
// ######################################################################
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_member_serialize: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_member_serialize< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().serialize( std::declval<A&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
// ######################################################################
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_non_member_serialize: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_non_member_serialize< T, A,
|
||||
typename Void<
|
||||
typename std::enable_if<!
|
||||
std::is_same<decltype(serialize( std::declval<A&>(), std::declval<T&>(), 0 )), BogusType>::value, void>::type
|
||||
//decltype( ::cereal::serialize( std::declval<A&>(), std::declval<T&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_non_member_serialize< T, A,
|
||||
typename Void<
|
||||
typename std::enable_if<
|
||||
std::is_same<decltype(serialize( std::declval<A&>(), std::declval<T&>(), 0 )), BogusType>::value, void>::type
|
||||
//decltype( ::cereal::serialize( std::declval<A&>(), std::declval<T&>(), 0 ) )
|
||||
>::type
|
||||
>: std::false_type {};
|
||||
|
||||
// ######################################################################
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_member_load: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_member_load< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().load( std::declval<A&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
// ######################################################################
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_non_member_load: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_non_member_load< T, A,
|
||||
typename Void<
|
||||
decltype( ::cereal::load( std::declval<A&>(), std::declval<T&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
// ######################################################################
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_member_save: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_member_save< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().save( std::declval<A&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
// ######################################################################
|
||||
template<typename T, class A, typename Sfinae = void>
|
||||
struct has_non_member_save: std::false_type {};
|
||||
|
||||
template<typename T, class A>
|
||||
struct has_non_member_save< T, A,
|
||||
typename Void<
|
||||
decltype( ::cereal::save( std::declval<A&>(), std::declval<T&>(), 0 ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
// ######################################################################
|
||||
template <class T, class A>
|
||||
constexpr bool has_member_split()
|
||||
{
|
||||
return has_member_load<T, A>() && has_member_save<T, A>();
|
||||
}
|
||||
|
||||
// ######################################################################
|
||||
template <class T, class A>
|
||||
constexpr bool has_non_member_split()
|
||||
{
|
||||
return has_non_member_load<T, A>() && has_non_member_save<T, A>();
|
||||
}
|
||||
|
||||
// ######################################################################
|
||||
template <class T, class A>
|
||||
constexpr bool is_serializable()
|
||||
{
|
||||
return has_member_split<T, A>() ^ has_member_serialize<T, A>() ^ has_non_member_split<T, A>() ^ has_non_member_serialize<T,A>();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user