From 3e071dd32ddeeb9f1c4ec3e31989effea69fd9ac Mon Sep 17 00:00:00 2001 From: Samuel Debionne Date: Tue, 4 Jun 2019 10:16:46 +0200 Subject: [PATCH 1/2] Add preliminary support for generic serialization --- include/boost/serialization/serialization.hpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/include/boost/serialization/serialization.hpp b/include/boost/serialization/serialization.hpp index a4d04723c7..aec37fc25c 100644 --- a/include/boost/serialization/serialization.hpp +++ b/include/boost/serialization/serialization.hpp @@ -13,6 +13,10 @@ #include #include +#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) +#include // For std::is_same, std::enable_if +#endif + /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 // serialization.hpp: interface for serialization system. @@ -60,6 +64,44 @@ namespace serialization { BOOST_STRONG_TYPEDEF(unsigned int, version_type) +#if __cplusplus==201103L + +namespace detail { + +struct has_serialize +{ + +private: + template + static constexpr auto check(T*) + -> typename + std::is_same< + decltype(std::declval().serialize(std::declval(), std::declval())), + void + >::type; + + template + static constexpr std::false_type check(...); + + typedef decltype(check(0)) type; + +public: + static constexpr bool value = type::value; +}; + +} //namespace detail + +// default implementation - call the member function "serialize" if it exists, +// else removed from overload resolution. +template +inline typename std::enable_if::value>::type serialize( + Archive & ar, T & t, const unsigned int file_version +){ + access::serialize(ar, t, static_cast(file_version)); +} + +#else //__cplusplus==201103L + // default implementation - call the member function "serialize" template inline void serialize( @@ -68,6 +110,8 @@ inline void serialize( access::serialize(ar, t, static_cast(file_version)); } +#endif //__cplusplus==201103L + // save data required for construction template inline void save_construct_data( From 0ed85772656a8fbe5a2a78f3300a865395f94e1f Mon Sep 17 00:00:00 2001 From: Samuel Debionne Date: Mon, 8 Jul 2019 14:39:38 +0200 Subject: [PATCH 2/2] Re-implement has_serialize trait in pure C++03 and move it to the serialization namespace --- include/boost/serialization/serialization.hpp | 56 +++++++------------ 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/include/boost/serialization/serialization.hpp b/include/boost/serialization/serialization.hpp index aec37fc25c..2ecf1d79db 100644 --- a/include/boost/serialization/serialization.hpp +++ b/include/boost/serialization/serialization.hpp @@ -11,12 +11,9 @@ #endif #include +#include #include -#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) -#include // For std::is_same, std::enable_if -#endif - /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 // serialization.hpp: interface for serialization system. @@ -64,54 +61,41 @@ namespace serialization { BOOST_STRONG_TYPEDEF(unsigned int, version_type) -#if __cplusplus==201103L - -namespace detail { - -struct has_serialize +// Check whether Type has a serialize() member function that accepts Archive +template +class has_serialize { + // This type won't compile if the second template parameter isn't of type T. + template struct type_check; -private: - template - static constexpr auto check(T*) - -> typename - std::is_same< - decltype(std::declval().serialize(std::declval(), std::declval())), - void - >::type; + typedef char yes; + typedef long no; - template - static constexpr std::false_type check(...); + template struct serialize + { + typedef void (T::* fptr)(Archive&, const unsigned int); + }; - typedef decltype(check(0)) type; + template + static yes check_has_serialize( + type_check< typename serialize::fptr, + &T::serialize/**/ >*); + template + static no check_has_serialize(...); public: - static constexpr bool value = type::value; + static bool const value = (sizeof(check_has_serialize(0)) == sizeof(yes)); }; -} //namespace detail - // default implementation - call the member function "serialize" if it exists, // else removed from overload resolution. template -inline typename std::enable_if::value>::type serialize( +inline typename boost::enable_if::value>::type serialize( Archive & ar, T & t, const unsigned int file_version ){ access::serialize(ar, t, static_cast(file_version)); } -#else //__cplusplus==201103L - -// default implementation - call the member function "serialize" -template -inline void serialize( - Archive & ar, T & t, const unsigned int file_version -){ - access::serialize(ar, t, static_cast(file_version)); -} - -#endif //__cplusplus==201103L - // save data required for construction template inline void save_construct_data(