mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
134 lines
5.1 KiB
C++
134 lines
5.1 KiB
C++
#ifndef CEREAL_DETAILS_TRAITS_HPP_
|
|
#define CEREAL_DETAILS_TRAITS_HPP_
|
|
|
|
#include <type_traits>
|
|
#include <memory>
|
|
|
|
namespace cereal
|
|
{
|
|
namespace traits
|
|
{
|
|
template<typename> struct Void { typedef void type; };
|
|
|
|
// ######################################################################
|
|
// Member Serialize
|
|
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&>() ) )
|
|
>::type
|
|
>: std::true_type {};
|
|
|
|
// ######################################################################
|
|
// Non Member Serialize
|
|
template<typename T, typename A> char & serialize(A&, T&);
|
|
template<typename T, typename A>
|
|
bool constexpr has_non_member_serialize()
|
|
{ return std::is_void<decltype(serialize(std::declval<A&>(), std::declval<T&>()))>::value; };
|
|
|
|
// ######################################################################
|
|
// Member Load
|
|
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&>() ) )
|
|
>::type
|
|
>: std::true_type {};
|
|
|
|
// ######################################################################
|
|
// Non Member Load
|
|
template<typename T, typename A> char & load(A&, T&);
|
|
template<typename T, typename A>
|
|
bool constexpr has_non_member_load()
|
|
{ return std::is_void<decltype(load(std::declval<A&>(), std::declval<T&>()))>::value; };
|
|
|
|
// ######################################################################
|
|
// Member Save
|
|
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 const &>().save( std::declval<A&>() ) )
|
|
>::type
|
|
>: std::true_type {};
|
|
|
|
// ######################################################################
|
|
// Non Member Save
|
|
template<typename T, typename A> char & save(A&, T const &);
|
|
template<typename T, typename A>
|
|
bool constexpr has_non_member_save()
|
|
{ return std::is_void<decltype(save(std::declval<A&>(), std::declval<T&>()))>::value; };
|
|
|
|
// ######################################################################
|
|
template <class T, class InputArchive, class OutputArchive>
|
|
constexpr bool has_member_split()
|
|
{ return has_member_load<T, InputArchive>() && has_member_save<T, OutputArchive>(); }
|
|
|
|
// ######################################################################
|
|
template <class T, class InputArchive, class OutputArchive>
|
|
constexpr bool has_non_member_split()
|
|
{ return has_non_member_load<T, InputArchive>() && has_non_member_save<T, OutputArchive>(); }
|
|
|
|
// ######################################################################
|
|
template <class T, class OutputArchive>
|
|
constexpr bool is_output_serializable()
|
|
{
|
|
return
|
|
has_member_save<T, OutputArchive>() ^
|
|
has_non_member_save<T, OutputArchive>() ^
|
|
has_member_serialize<T, OutputArchive>() ^
|
|
has_non_member_serialize<T, OutputArchive>();
|
|
}
|
|
|
|
// ######################################################################
|
|
template <class T, class InputArchive>
|
|
constexpr bool is_input_serializable()
|
|
{
|
|
return
|
|
has_member_load<T, InputArchive>() ^
|
|
has_non_member_load<T, InputArchive>() ^
|
|
has_member_serialize<T, InputArchive>() ^
|
|
has_non_member_serialize<T, InputArchive>();
|
|
}
|
|
|
|
// ######################################################################
|
|
template <class T>
|
|
constexpr size_t sizeofArray( size_t rank = std::rank<T>::value )
|
|
{
|
|
return rank == 0 ? 1 : std::extent<T>::value * sizeofArray<typename std::remove_extent<T>::type>( rank - 1 );
|
|
}
|
|
|
|
// ######################################################################
|
|
//! A macro to use to restrict which types of archives your serialize function will work for.
|
|
/*! This requires you to have a template class parameter named Archive and replaces the void return
|
|
type for your serialize function.
|
|
|
|
INTYPE refers to the input archive type you wish to restrict on.
|
|
OUTTYPE refers to the output archive type you wish to restrict on.
|
|
|
|
For example, if we want to limit a serialize to only work with binary serialization:
|
|
|
|
@code{.cpp}
|
|
template <class Archive>
|
|
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
|
|
serialize( Archive & ar, MyCoolType & m )
|
|
{
|
|
ar & m;
|
|
}
|
|
@endcode
|
|
*/
|
|
#define CEREAL_ARCHIVE_RESTRICT_SERIALIZE(INTYPE, OUTTYPE) \
|
|
typename std::enable_if<std::is_same<Archive, INTYPE>::value || std::is_same<Archive, OUTTYPE>::value, void>::type
|
|
}
|
|
}
|
|
|
|
#endif // CEREAL_DETAILS_TRAITS_HPP_
|