You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1389 lines
94 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*! \file traits.hpp
  2. \brief Internal type trait support
  3. \ingroup Internal */
  4. /*
  5. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. * Neither the name of cereal nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef CEREAL_DETAILS_TRAITS_HPP_
  29. #define CEREAL_DETAILS_TRAITS_HPP_
  30. #ifndef __clang__
  31. #if (__GNUC__ == 4 && __GNUC_MINOR__ <= 7)
  32. #define CEREAL_OLDER_GCC
  33. #endif // gcc 4.7 or earlier
  34. #endif // __clang__
  35. #include <type_traits>
  36. #include <typeindex>
  37. #include "cereal/macros.hpp"
  38. #include "cereal/access.hpp"
  39. namespace cereal
  40. {
  41. namespace traits
  42. {
  43. using yes = std::true_type;
  44. using no = std::false_type;
  45. namespace detail
  46. {
  47. // ######################################################################
  48. //! Used to delay a static_assert until template instantiation
  49. template <class T>
  50. struct delay_static_assert : std::false_type {};
  51. // ######################################################################
  52. // SFINAE Helpers
  53. #ifdef CEREAL_OLDER_GCC // when VS supports better SFINAE, we can use this as the default
  54. template<typename> struct Void { typedef void type; };
  55. #endif // CEREAL_OLDER_GCC
  56. //! Return type for SFINAE Enablers
  57. enum class sfinae {};
  58. // ######################################################################
  59. // Helper functionality for boolean integral constants and Enable/DisableIf
  60. template <bool H, bool ... T> struct meta_bool_and : std::integral_constant<bool, H && meta_bool_and<T...>::value> {};
  61. template <bool B> struct meta_bool_and<B> : std::integral_constant<bool, B> {};
  62. template <bool H, bool ... T> struct meta_bool_or : std::integral_constant<bool, H || meta_bool_or<T...>::value> {};
  63. template <bool B> struct meta_bool_or<B> : std::integral_constant<bool, B> {};
  64. // workaround needed due to bug in MSVC 2013, see
  65. // http://connect.microsoft.com/VisualStudio/feedback/details/800231/c-11-alias-template-issue
  66. template <bool ... Conditions>
  67. struct EnableIfHelper : std::enable_if<meta_bool_and<Conditions...>::value, sfinae> {};
  68. template <bool ... Conditions>
  69. struct DisableIfHelper : std::enable_if<!meta_bool_or<Conditions...>::value, sfinae> {};
  70. } // namespace detail
  71. //! Used as the default value for EnableIf and DisableIf template parameters
  72. /*! @relates EnableIf
  73. @relates DisableIf */
  74. static const detail::sfinae sfinae = {};
  75. // ######################################################################
  76. //! Provides a way to enable a function if conditions are met
  77. /*! This is intended to be used in a near identical fashion to std::enable_if
  78. while being significantly easier to read at the cost of not allowing for as
  79. complicated of a condition.
  80. This will compile (allow the function) if every condition evaluates to true.
  81. at compile time. This should be used with SFINAE to ensure that at least
  82. one other candidate function works when one fails due to an EnableIf.
  83. This should be used as the las template parameter to a function as
  84. an unnamed parameter with a default value of cereal::traits::sfinae:
  85. @code{cpp}
  86. // using by making the last template argument variadic
  87. template <class T, EnableIf<std::is_same<T, bool>::value> = sfinae>
  88. void func(T t );
  89. @endcode
  90. Note that this performs a logical AND of all conditions, so you will need
  91. to construct more complicated requirements with this fact in mind.
  92. @relates DisableIf
  93. @relates sfinae
  94. @tparam Conditions The conditions which will be logically ANDed to enable the function. */
  95. template <bool ... Conditions>
  96. using EnableIf = typename detail::EnableIfHelper<Conditions...>::type;
  97. // ######################################################################
  98. //! Provides a way to disable a function if conditions are met
  99. /*! This is intended to be used in a near identical fashion to std::enable_if
  100. while being significantly easier to read at the cost of not allowing for as
  101. complicated of a condition.
  102. This will compile (allow the function) if every condition evaluates to false.
  103. This should be used with SFINAE to ensure that at least one other candidate
  104. function works when one fails due to a DisableIf.
  105. This should be used as the las template parameter to a function as
  106. an unnamed parameter with a default value of cereal::traits::sfinae:
  107. @code{cpp}
  108. // using by making the last template argument variadic
  109. template <class T, DisableIf<std::is_same<T, bool>::value> = sfinae>
  110. void func(T t );
  111. @endcode
  112. This is often used in conjunction with EnableIf to form an enable/disable pair of
  113. overloads.
  114. Note that this performs a logical AND of all conditions, so you will need
  115. to construct more complicated requirements with this fact in mind. If all conditions
  116. hold, the function will be disabled.
  117. @relates EnableIf
  118. @relates sfinae
  119. @tparam Conditions The conditions which will be logically ANDed to disable the function. */
  120. template <bool ... Conditions>
  121. using DisableIf = typename detail::DisableIfHelper<Conditions...>::type;
  122. // ######################################################################
  123. namespace detail
  124. {
  125. template <class InputArchive>
  126. struct get_output_from_input : no
  127. {
  128. static_assert( detail::delay_static_assert<InputArchive>::value,
  129. "Could not find an associated output archive for input archive." );
  130. };
  131. template <class OutputArchive>
  132. struct get_input_from_output : no
  133. {
  134. static_assert( detail::delay_static_assert<OutputArchive>::value,
  135. "Could not find an associated input archive for output archive." );
  136. };
  137. }
  138. //! Sets up traits that relate an input archive to an output archive
  139. #define CEREAL_SETUP_ARCHIVE_TRAITS(InputArchive, OutputArchive) \
  140. namespace cereal { namespace traits { namespace detail { \
  141. template <> struct get_output_from_input<InputArchive> \
  142. { using type = OutputArchive; }; \
  143. template <> struct get_input_from_output<OutputArchive> \
  144. { using type = InputArchive; }; } } } /* end namespaces */
  145. // ######################################################################
  146. //! Used to convert a MAKE_HAS_XXX macro into a versioned variant
  147. #define CEREAL_MAKE_VERSIONED_TEST ,0
  148. // ######################################################################
  149. //! Creates a test for whether a non const member function exists
  150. /*! This creates a class derived from std::integral_constant that will be true if
  151. the type has the proper member function for the given archive.
  152. @param name The name of the function to test for (e.g. serialize, load, save)
  153. @param test_name The name to give the test for the function being tested for (e.g. serialize, versioned_serialize)
  154. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  155. #ifdef CEREAL_OLDER_GCC
  156. #define CEREAL_MAKE_HAS_MEMBER_TEST(name, test_name, versioned) \
  157. template <class T, class A, class SFINAE = void> \
  158. struct has_member_##test_name : no {}; \
  159. template <class T, class A> \
  160. struct has_member_##test_name<T, A, \
  161. typename detail::Void< decltype( cereal::access::member_##name( std::declval<A&>(), std::declval<T&>() versioned ) ) >::type> : yes {}
  162. #else // NOT CEREAL_OLDER_GCC
  163. #define CEREAL_MAKE_HAS_MEMBER_TEST(name, test_name, versioned) \
  164. namespace detail \
  165. { \
  166. template <class T, class A> \
  167. struct has_member_##name##_##versioned##_impl \
  168. { \
  169. template <class TT, class AA> \
  170. static auto test(int) -> decltype( cereal::access::member_##name( std::declval<AA&>(), std::declval<TT&>() versioned ), yes()); \
  171. template <class, class> \
  172. static no test(...); \
  173. static const bool value = std::is_same<decltype(test<T, A>(0)), yes>::value; \
  174. }; \
  175. } /* end namespace detail */ \
  176. template <class T, class A> \
  177. struct has_member_##test_name : std::integral_constant<bool, detail::has_member_##name##_##versioned##_impl<T, A>::value> {}
  178. #endif // NOT CEREAL_OLDER_GCC
  179. // ######################################################################
  180. //! Creates a test for whether a non const non-member function exists
  181. /*! This creates a class derived from std::integral_constant that will be true if
  182. the type has the proper non-member function for the given archive. */
  183. #define CEREAL_MAKE_HAS_NON_MEMBER_TEST(test_name, func, versioned) \
  184. namespace detail \
  185. { \
  186. template <class T, class A> \
  187. struct has_non_member_##test_name##_impl \
  188. { \
  189. template <class TT, class AA> \
  190. static auto test(int) -> decltype( func( std::declval<AA&>(), std::declval<TT&>() versioned ), yes()); \
  191. template <class, class> \
  192. static no test( ... ); \
  193. static const bool value = std::is_same<decltype( test<T, A>( 0 ) ), yes>::value; \
  194. }; \
  195. } /* end namespace detail */ \
  196. template <class T, class A> \
  197. struct has_non_member_##test_name : std::integral_constant<bool, detail::has_non_member_##test_name##_impl<T, A>::value> {}
  198. // ######################################################################
  199. // Member Serialize
  200. CEREAL_MAKE_HAS_MEMBER_TEST(serialize, serialize,);
  201. // ######################################################################
  202. // Member Serialize (versioned)
  203. CEREAL_MAKE_HAS_MEMBER_TEST(serialize, versioned_serialize, CEREAL_MAKE_VERSIONED_TEST);
  204. // ######################################################################
  205. // Non Member Serialize
  206. CEREAL_MAKE_HAS_NON_MEMBER_TEST(serialize, CEREAL_SERIALIZE_FUNCTION_NAME,);
  207. // ######################################################################
  208. // Non Member Serialize (versioned)
  209. CEREAL_MAKE_HAS_NON_MEMBER_TEST(versioned_serialize, CEREAL_SERIALIZE_FUNCTION_NAME, CEREAL_MAKE_VERSIONED_TEST);
  210. // ######################################################################
  211. // Member Load
  212. CEREAL_MAKE_HAS_MEMBER_TEST(load, load,);
  213. // ######################################################################
  214. // Member Load (versioned)
  215. CEREAL_MAKE_HAS_MEMBER_TEST(load, versioned_load, CEREAL_MAKE_VERSIONED_TEST);
  216. // ######################################################################
  217. // Non Member Load
  218. CEREAL_MAKE_HAS_NON_MEMBER_TEST(load, CEREAL_LOAD_FUNCTION_NAME,);
  219. // ######################################################################
  220. // Non Member Load (versioned)
  221. CEREAL_MAKE_HAS_NON_MEMBER_TEST(versioned_load, CEREAL_LOAD_FUNCTION_NAME, CEREAL_MAKE_VERSIONED_TEST);
  222. // ######################################################################
  223. #undef CEREAL_MAKE_HAS_NON_MEMBER_TEST
  224. #undef CEREAL_MAKE_HAS_MEMBER_TEST
  225. // ######################################################################
  226. //! Creates a test for whether a member save function exists
  227. /*! This creates a class derived from std::integral_constant that will be true if
  228. the type has the proper member function for the given archive.
  229. @param test_name The name to give the test (e.g. save or versioned_save)
  230. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  231. #ifdef CEREAL_OLDER_GCC
  232. #define CEREAL_MAKE_HAS_MEMBER_SAVE_IMPL(test_name, versioned) \
  233. namespace detail \
  234. { \
  235. template <class T, class A> \
  236. struct has_member_##test_name##_impl \
  237. { \
  238. template <class TT, class AA, class SFINAE = void> struct test : no {}; \
  239. template <class TT, class AA> \
  240. struct test<TT, AA, \
  241. typename detail::Void< decltype( cereal::access::member_save( std::declval<AA&>(), \
  242. std::declval<TT const &>() versioned ) ) >::type> : yes {}; \
  243. static const bool value = test<T, A>(); \
  244. \
  245. template <class TT, class AA, class SFINAE = void> struct test2 : no {}; \
  246. template <class TT, class AA> \
  247. struct test2<TT, AA, \
  248. typename detail::Void< decltype( cereal::access::member_save_non_const( \
  249. std::declval<AA&>(), \
  250. std::declval<typename std::remove_const<TT>::type&>() versioned ) ) >::type> : yes {}; \
  251. static const bool not_const_type = test2<T, A>(); \
  252. }; \
  253. } /* end namespace detail */
  254. #else /* NOT CEREAL_OLDER_GCC =================================== */
  255. #define CEREAL_MAKE_HAS_MEMBER_SAVE_IMPL(test_name, versioned) \
  256. namespace detail \
  257. { \
  258. template <class T, class A> \
  259. struct has_member_##test_name##_impl \
  260. { \
  261. template <class TT, class AA> \
  262. static auto test(int) -> decltype( cereal::access::member_save( std::declval<AA&>(), \
  263. std::declval<TT const &>() versioned ), yes()); \
  264. template <class, class> static no test(...); \
  265. static const bool value = std::is_same<decltype(test<T, A>(0)), yes>::value; \
  266. \
  267. template <class TT, class AA> \
  268. static auto test2(int) -> decltype( cereal::access::member_save_non_const( \
  269. std::declval<AA &>(), \
  270. std::declval<typename std::remove_const<TT>::type&>() versioned ), yes()); \
  271. template <class, class> static no test2(...); \
  272. static const bool not_const_type = std::is_same<decltype(test2<T, A>(0)), yes>::value; \
  273. }; \
  274. } /* end namespace detail */
  275. #endif /* NOT CEREAL_OLDER_GCC */
  276. // ######################################################################
  277. // Member Save
  278. CEREAL_MAKE_HAS_MEMBER_SAVE_IMPL(save, )
  279. template <class T, class A>
  280. struct has_member_save : std::integral_constant<bool, detail::has_member_save_impl<T, A>::value>
  281. {
  282. typedef typename detail::has_member_save_impl<T, A> check;
  283. static_assert( check::value || !check::not_const_type,
  284. "cereal detected a non-const save. \n "
  285. "save member functions must always be const" );
  286. };
  287. // ######################################################################
  288. // Member Save (versioned)
  289. CEREAL_MAKE_HAS_MEMBER_SAVE_IMPL(versioned_save, CEREAL_MAKE_VERSIONED_TEST)
  290. template <class T, class A>
  291. struct has_member_versioned_save : std::integral_constant<bool, detail::has_member_versioned_save_impl<T, A>::value>
  292. {
  293. typedef typename detail::has_member_versioned_save_impl<T, A> check;
  294. static_assert( check::value || !check::not_const_type,
  295. "cereal detected a versioned non-const save. \n "
  296. "save member functions must always be const" );
  297. };
  298. // ######################################################################
  299. #undef CEREAL_MAKE_HAS_MEMBER_SAVE_IMPL
  300. // ######################################################################
  301. //! Creates a test for whether a non-member save function exists
  302. /*! This creates a class derived from std::integral_constant that will be true if
  303. the type has the proper non-member function for the given archive.
  304. @param test_name The name to give the test (e.g. save or versioned_save)
  305. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  306. #define CEREAL_MAKE_HAS_NON_MEMBER_SAVE_TEST(test_name, versioned) \
  307. namespace detail \
  308. { \
  309. template <class T, class A> \
  310. struct has_non_member_##test_name##_impl \
  311. { \
  312. template <class TT, class AA> \
  313. static auto test(int) -> decltype( CEREAL_SAVE_FUNCTION_NAME( \
  314. std::declval<AA&>(), \
  315. std::declval<TT const &>() versioned ), yes()); \
  316. template <class, class> static no test(...); \
  317. static const bool value = std::is_same<decltype(test<T, A>(0)), yes>::value; \
  318. \
  319. template <class TT, class AA> \
  320. static auto test2(int) -> decltype( CEREAL_SAVE_FUNCTION_NAME( \
  321. std::declval<AA &>(), \
  322. std::declval<typename std::remove_const<TT>::type&>() versioned ), yes()); \
  323. template <class, class> static no test2(...); \
  324. static const bool not_const_type = std::is_same<decltype(test2<T, A>(0)), yes>::value; \
  325. }; \
  326. } /* end namespace detail */ \
  327. \
  328. template <class T, class A> \
  329. struct has_non_member_##test_name : std::integral_constant<bool, detail::has_non_member_##test_name##_impl<T, A>::value> \
  330. { \
  331. using check = typename detail::has_non_member_##test_name##_impl<T, A>; \
  332. static_assert( check::value || !check::not_const_type, \
  333. "cereal detected a non-const type parameter in non-member " #test_name ". \n " \
  334. #test_name " non-member functions must always pass their types as const" ); \
  335. };
  336. // ######################################################################
  337. // Non Member Save
  338. CEREAL_MAKE_HAS_NON_MEMBER_SAVE_TEST(save, )
  339. // ######################################################################
  340. // Non Member Save (versioned)
  341. CEREAL_MAKE_HAS_NON_MEMBER_SAVE_TEST(versioned_save, CEREAL_MAKE_VERSIONED_TEST)
  342. // ######################################################################
  343. #undef CEREAL_MAKE_HAS_NON_MEMBER_SAVE_TEST
  344. // ######################################################################
  345. // Minimal Utilities
  346. namespace detail
  347. {
  348. // Determines if the provided type is an std::string
  349. template <class> struct is_string : std::false_type {};
  350. template <class CharT, class Traits, class Alloc>
  351. struct is_string<std::basic_string<CharT, Traits, Alloc>> : std::true_type {};
  352. }
  353. // Determines if the type is valid for use with a minimal serialize function
  354. template <class T>
  355. struct is_minimal_type : std::integral_constant<bool,
  356. detail::is_string<T>::value || std::is_arithmetic<T>::value> {};
  357. // ######################################################################
  358. //! Creates implementation details for whether a member save_minimal function exists
  359. /*! This creates a class derived from std::integral_constant that will be true if
  360. the type has the proper member function for the given archive.
  361. @param test_name The name to give the test (e.g. save_minimal or versioned_save_minimal)
  362. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  363. #ifdef CEREAL_OLDER_GCC
  364. #define CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_IMPL(test_name, versioned) \
  365. namespace detail \
  366. { \
  367. template <class T, class A> \
  368. struct has_member_##test_name##_impl \
  369. { \
  370. template <class TT, class AA, class SFINAE = void> struct test : no {}; \
  371. template <class TT, class AA> \
  372. struct test<TT, AA, typename detail::Void< decltype( \
  373. cereal::access::member_save_minimal( std::declval<AA const &>(), \
  374. std::declval<TT const &>() versioned ) ) >::type> : yes {}; \
  375. \
  376. static const bool value = test<T, A>(); \
  377. \
  378. template <class TT, class AA, class SFINAE = void> struct test2 : no {}; \
  379. template <class TT, class AA> \
  380. struct test2<TT, AA, typename detail::Void< decltype( \
  381. cereal::access::member_save_minimal_non_const( std::declval<AA const &>(), \
  382. std::declval<typename std::remove_const<TT>::type&>() versioned ) ) >::type> : yes {}; \
  383. static const bool not_const_type = test2<T, A>(); \
  384. \
  385. static const bool valid = value || !not_const_type; \
  386. }; \
  387. } /* end namespace detail */
  388. #else /* NOT CEREAL_OLDER_GCC =================================== */
  389. #define CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_IMPL(test_name, versioned) \
  390. namespace detail \
  391. { \
  392. template <class T, class A> \
  393. struct has_member_##test_name##_impl \
  394. { \
  395. template <class TT, class AA> \
  396. static auto test(int) -> decltype( cereal::access::member_save_minimal( \
  397. std::declval<AA const &>(), \
  398. std::declval<TT const &>() versioned ), yes()); \
  399. template <class, class> static no test(...); \
  400. static const bool value = std::is_same<decltype(test<T, A>(0)), yes>::value; \
  401. \
  402. template <class TT, class AA> \
  403. static auto test2(int) -> decltype( cereal::access::member_save_minimal_non_const( \
  404. std::declval<AA const &>(), \
  405. std::declval<typename std::remove_const<TT>::type&>() versioned ), yes()); \
  406. template <class, class> static no test2(...); \
  407. static const bool not_const_type = std::is_same<decltype(test2<T, A>(0)), yes>::value; \
  408. \
  409. static const bool valid = value || !not_const_type; \
  410. }; \
  411. } /* end namespace detail */
  412. #endif // NOT CEREAL_OLDER_GCC
  413. // ######################################################################
  414. //! Creates helpers for minimal save functions
  415. /*! The get_member_*_type structs allow access to the return type of a save_minimal,
  416. assuming that the function actually exists. If the function does not
  417. exist, the type will be void.
  418. @param test_name The name to give the test (e.g. save_minimal or versioned_save_minimal)
  419. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  420. #define CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_HELPERS_IMPL(test_name, versioned) \
  421. namespace detail \
  422. { \
  423. template <class T, class A, bool Valid> \
  424. struct get_member_##test_name##_type { using type = void; }; \
  425. \
  426. template <class T, class A> \
  427. struct get_member_##test_name##_type<T, A, true> \
  428. { \
  429. using type = decltype( cereal::access::member_save_minimal( std::declval<A const &>(), \
  430. std::declval<T const &>() versioned ) ); \
  431. }; \
  432. } /* end namespace detail */
  433. // ######################################################################
  434. //! Creates a test for whether a member save_minimal function exists
  435. /*! This creates a class derived from std::integral_constant that will be true if
  436. the type has the proper member function for the given archive.
  437. @param test_name The name to give the test (e.g. save_minimal or versioned_save_minimal) */
  438. #define CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_TEST(test_name) \
  439. template <class T, class A> \
  440. struct has_member_##test_name : std::integral_constant<bool, detail::has_member_##test_name##_impl<T, A>::value> \
  441. { \
  442. using check = typename detail::has_member_##test_name##_impl<T, A>; \
  443. static_assert( check::valid, \
  444. "cereal detected a non-const member " #test_name ". \n " \
  445. #test_name " member functions must always be const" ); \
  446. \
  447. using type = typename detail::get_member_##test_name##_type<T, A, check::value>::type; \
  448. static_assert( (check::value && is_minimal_type<type>::value) || !check::value, \
  449. "cereal detected a member " #test_name " with an invalid return type. \n " \
  450. "return type must be arithmetic or string" ); \
  451. };
  452. // ######################################################################
  453. // Member Save Minimal
  454. CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_IMPL(save_minimal, )
  455. CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_HELPERS_IMPL(save_minimal, )
  456. CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_TEST(save_minimal)
  457. // ######################################################################
  458. // Member Save Minimal (versioned)
  459. CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_IMPL(versioned_save_minimal, CEREAL_MAKE_VERSIONED_TEST)
  460. CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_HELPERS_IMPL(versioned_save_minimal, CEREAL_MAKE_VERSIONED_TEST)
  461. CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_TEST(versioned_save_minimal)
  462. // ######################################################################
  463. #undef CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_IMPL
  464. #undef CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_HELPERS_IMPL
  465. #undef CEREAL_MAKE_HAS_MEMBER_SAVE_MINIMAL_TEST
  466. // ######################################################################
  467. //! Creates a test for whether a non-member save_minimal function exists
  468. /*! This creates a class derived from std::integral_constant that will be true if
  469. the type has the proper member function for the given archive.
  470. @param test_name The name to give the test (e.g. save_minimal or versioned_save_minimal)
  471. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  472. #define CEREAL_MAKE_HAS_NON_MEMBER_SAVE_MINIMAL_TEST(test_name, versioned) \
  473. namespace detail \
  474. { \
  475. template <class T, class A> \
  476. struct has_non_member_##test_name##_impl \
  477. { \
  478. template <class TT, class AA> \
  479. static auto test(int) -> decltype( CEREAL_SAVE_MINIMAL_FUNCTION_NAME( \
  480. std::declval<AA const &>(), \
  481. std::declval<TT const &>() versioned ), yes()); \
  482. template <class, class> static no test(...); \
  483. static const bool value = std::is_same<decltype(test<T, A>(0)), yes>::value; \
  484. \
  485. template <class TT, class AA> \
  486. static auto test2(int) -> decltype( CEREAL_SAVE_MINIMAL_FUNCTION_NAME( \
  487. std::declval<AA const &>(), \
  488. std::declval<typename std::remove_const<TT>::type&>() versioned ), yes()); \
  489. template <class, class> static no test2(...); \
  490. static const bool not_const_type = std::is_same<decltype(test2<T, A>(0)), yes>::value; \
  491. \
  492. static const bool valid = value || !not_const_type; \
  493. }; \
  494. \
  495. template <class T, class A, bool Valid> \
  496. struct get_non_member_##test_name##_type { using type = void; }; \
  497. \
  498. template <class T, class A> \
  499. struct get_non_member_##test_name##_type <T, A, true> \
  500. { \
  501. using type = decltype( CEREAL_SAVE_MINIMAL_FUNCTION_NAME( std::declval<A const &>(), \
  502. std::declval<T const &>() versioned ) ); \
  503. }; \
  504. } /* end namespace detail */ \
  505. \
  506. template <class T, class A> \
  507. struct has_non_member_##test_name : std::integral_constant<bool, detail::has_non_member_##test_name##_impl<T, A>::value> \
  508. { \
  509. using check = typename detail::has_non_member_##test_name##_impl<T, A>; \
  510. static_assert( check::valid, \
  511. "cereal detected a non-const type parameter in non-member " #test_name ". \n " \
  512. #test_name " non-member functions must always pass their types as const" ); \
  513. \
  514. using type = typename detail::get_non_member_##test_name##_type<T, A, check::value>::type; \
  515. static_assert( (check::value && is_minimal_type<type>::value) || !check::value, \
  516. "cereal detected a non-member " #test_name " with an invalid return type. \n " \
  517. "return type must be arithmetic or string" ); \
  518. };
  519. // ######################################################################
  520. // Non-Member Save Minimal
  521. CEREAL_MAKE_HAS_NON_MEMBER_SAVE_MINIMAL_TEST(save_minimal, )
  522. // ######################################################################
  523. // Non-Member Save Minimal (versioned)
  524. CEREAL_MAKE_HAS_NON_MEMBER_SAVE_MINIMAL_TEST(versioned_save_minimal, CEREAL_MAKE_VERSIONED_TEST)
  525. // ######################################################################
  526. #undef CEREAL_MAKE_HAS_NON_MEMBER_SAVE_MINIMAL_TEST
  527. // ######################################################################
  528. // Load Minimal Utilities
  529. namespace detail
  530. {
  531. //! Used to help strip away conversion wrappers
  532. /*! If someone writes a non-member load/save minimal function that accepts its
  533. parameter as some generic template type and needs to perform trait checks
  534. on that type, our NoConvert wrappers will interfere with this. Using
  535. the struct strip_minmal, users can strip away our wrappers to get to
  536. the underlying type, allowing traits to work properly */
  537. struct NoConvertBase {};
  538. //! A struct that prevents implicit conversion
  539. /*! Any type instantiated with this struct will be unable to implicitly convert
  540. to another type. Is designed to only allow conversion to Source const &.
  541. @tparam Source the type of the original source */
  542. template <class Source>
  543. struct NoConvertConstRef : NoConvertBase
  544. {
  545. using type = Source; //!< Used to get underlying type easily
  546. template <class Dest, class = typename std::enable_if<std::is_same<Source, Dest>::value>::type>
  547. operator Dest () = delete;
  548. //! only allow conversion if the types are the same and we are converting into a const reference
  549. template <class Dest, class = typename std::enable_if<std::is_same<Source, Dest>::value>::type>
  550. operator Dest const & ();
  551. };
  552. //! A struct that prevents implicit conversion
  553. /*! Any type instantiated with this struct will be unable to implicitly convert
  554. to another type. Is designed to only allow conversion to Source &.
  555. @tparam Source the type of the original source */
  556. template <class Source>
  557. struct NoConvertRef : NoConvertBase
  558. {
  559. using type = Source; //!< Used to get underlying type easily
  560. template <class Dest, class = typename std::enable_if<std::is_same<Source, Dest>::value>::type>
  561. operator Dest () = delete;
  562. #ifdef __clang__
  563. template <class Dest, class = typename std::enable_if<std::is_same<Source, Dest>::value>::type>
  564. operator Dest const & () = delete;
  565. #endif // __clang__
  566. //! only allow conversion if the types are the same and we are converting into a const reference
  567. template <class Dest, class = typename std::enable_if<std::is_same<Source, Dest>::value>::type>
  568. operator Dest & ();
  569. };
  570. //! A type that can implicitly convert to anything else
  571. struct AnyConvert
  572. {
  573. template <class Dest>
  574. operator Dest & ();
  575. template <class Dest>
  576. operator Dest const & () const;
  577. };
  578. } // namespace detail
  579. // ######################################################################
  580. //! Creates a test for whether a member load_minimal function exists
  581. /*! This creates a class derived from std::integral_constant that will be true if
  582. the type has the proper member function for the given archive.
  583. Our strategy here is to first check if a function matching the signature more or less exists
  584. (allow anything like load_minimal(xxx) using AnyConvert, and then secondly enforce
  585. that it has the correct signature using NoConvertConstRef
  586. @param test_name The name to give the test (e.g. load_minimal or versioned_load_minimal)
  587. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  588. #ifdef CEREAL_OLDER_GCC
  589. #define CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_IMPL(test_name, versioned) \
  590. namespace detail \
  591. { \
  592. template <class T, class A, class SFINAE = void> struct has_member_##test_name##_impl : no {}; \
  593. template <class T, class A> \
  594. struct has_member_##test_name##_impl<T, A, typename detail::Void< decltype( \
  595. cereal::access::member_load_minimal( std::declval<A const &>(), \
  596. std::declval<T &>(), AnyConvert() versioned ) ) >::type> : yes {}; \
  597. \
  598. template <class T, class A, class U, class SFINAE = void> struct has_member_##test_name##_type_impl : no {}; \
  599. template <class T, class A, class U> \
  600. struct has_member_##test_name##_type_impl<T, A, U, typename detail::Void< decltype( \
  601. cereal::access::member_load_minimal( std::declval<A const &>(), \
  602. std::declval<T &>(), NoConvertConstRef<U>() versioned ) ) >::type> : yes {}; \
  603. } /* end namespace detail */
  604. #else /* NOT CEREAL_OLDER_GCC =================================== */
  605. #define CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_IMPL(test_name, versioned) \
  606. namespace detail \
  607. { \
  608. template <class T, class A> \
  609. struct has_member_##test_name##_impl \
  610. { \
  611. template <class TT, class AA> \
  612. static auto test(int) -> decltype( cereal::access::member_load_minimal( \
  613. std::declval<AA const &>(), \
  614. std::declval<TT &>(), AnyConvert() versioned ), yes()); \
  615. template <class, class> static no test(...); \
  616. static const bool value = std::is_same<decltype(test<T, A>(0)), yes>::value; \
  617. }; \
  618. template <class T, class A, class U> \
  619. struct has_member_##test_name##_type_impl \
  620. { \
  621. template <class TT, class AA, class UU> \
  622. static auto test(int) -> decltype( cereal::access::member_load_minimal( \
  623. std::declval<AA const &>(), \
  624. std::declval<TT &>(), NoConvertConstRef<UU>() versioned ), yes()); \
  625. template <class, class, class> static no test(...); \
  626. static const bool value = std::is_same<decltype(test<T, A, U>(0)), yes>::value; \
  627. \
  628. }; \
  629. } /* end namespace detail */
  630. #endif // NOT CEREAL_OLDER_GCC
  631. // ######################################################################
  632. //! Creates helpers for minimal load functions
  633. /*! The has_member_*_wrapper structs ensure that the load and save types for the
  634. requested function type match appropriately.
  635. @param load_test_name The name to give the test (e.g. load_minimal or versioned_load_minimal)
  636. @param save_test_name The name to give the test (e.g. save_minimal or versioned_save_minimal,
  637. should match the load name.
  638. @param save_test_prefix The name to give the test (e.g. save_minimal or versioned_save_minimal,
  639. should match the load name, without the trailing "_minimal" (e.g.
  640. save or versioned_save). Needed because the preprocessor is an abomination.
  641. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  642. #define CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_HELPERS_IMPL(load_test_name, save_test_name, save_test_prefix, versioned) \
  643. namespace detail \
  644. { \
  645. template <class T, class A, bool Valid> \
  646. struct has_member_##load_test_name##_wrapper : std::false_type {}; \
  647. \
  648. template <class T, class A> \
  649. struct has_member_##load_test_name##_wrapper<T, A, true> \
  650. { \
  651. using AOut = typename detail::get_output_from_input<A>::type; \
  652. \
  653. static_assert( has_member_##save_test_prefix##_minimal<T, AOut>::value, \
  654. "cereal detected member " #load_test_name " but no valid member " #save_test_name ". \n " \
  655. "cannot evaluate correctness of " #load_test_name " without valid " #save_test_name "." ); \
  656. \
  657. using SaveType = typename detail::get_member_##save_test_prefix##_minimal_type<T, AOut, true>::type; \
  658. const static bool value = has_member_##load_test_name##_impl<T, A>::value; \
  659. const static bool valid = has_member_##load_test_name##_type_impl<T, A, SaveType>::value; \
  660. \
  661. static_assert( valid || !value, "cereal detected different or invalid types in corresponding member " \
  662. #load_test_name " and " #save_test_name " functions. \n " \
  663. "the paramater to " #load_test_name " must be a constant reference to the type that " \
  664. #save_test_name " returns." ); \
  665. }; \
  666. } /* end namespace detail */
  667. // ######################################################################
  668. //! Creates a test for whether a member load_minimal function exists
  669. /*! This creates a class derived from std::integral_constant that will be true if
  670. the type has the proper member function for the given archive.
  671. @param load_test_name The name to give the test (e.g. load_minimal or versioned_load_minimal)
  672. @param load_test_prefix The above parameter minus the trailing "_minimal" */
  673. #define CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_TEST(load_test_name, load_test_prefix) \
  674. template <class T, class A> \
  675. struct has_member_##load_test_prefix##_minimal : std::integral_constant<bool, \
  676. detail::has_member_##load_test_name##_wrapper<T, A, detail::has_member_##load_test_name##_impl<T, A>::value>::value> {};
  677. // ######################################################################
  678. // Member Load Minimal
  679. CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_IMPL(load_minimal, )
  680. CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_HELPERS_IMPL(load_minimal, save_minimal, save, )
  681. CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_TEST(load_minimal, load)
  682. // ######################################################################
  683. // Member Load Minimal (versioned)
  684. CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_IMPL(versioned_load_minimal, CEREAL_MAKE_VERSIONED_TEST)
  685. CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_HELPERS_IMPL(versioned_load_minimal, versioned_save_minimal, versioned_save, CEREAL_MAKE_VERSIONED_TEST)
  686. CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_TEST(versioned_load_minimal, versioned_load)
  687. // ######################################################################
  688. #undef CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_IMPL
  689. #undef CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_HELPERS_IMPL
  690. #undef CEREAL_MAKE_HAS_MEMBER_LOAD_MINIMAL_TEST
  691. // ######################################################################
  692. // Non-Member Load Minimal
  693. namespace detail
  694. {
  695. #ifdef CEREAL_OLDER_GCC
  696. void CEREAL_LOAD_MINIMAL_FUNCTION_NAME(); // prevents nonsense complaining about not finding this
  697. void CEREAL_SAVE_MINIMAL_FUNCTION_NAME();
  698. #endif // CEREAL_OLDER_GCC
  699. } // namespace detail
  700. // ######################################################################
  701. //! Creates a test for whether a non-member load_minimal function exists
  702. /*! This creates a class derived from std::integral_constant that will be true if
  703. the type has the proper member function for the given archive.
  704. See notes from member load_minimal implementation.
  705. @param test_name The name to give the test (e.g. load_minimal or versioned_load_minimal)
  706. @param save_name The corresponding name the save test would have (e.g. save_minimal or versioned_save_minimal)
  707. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  708. #define CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST(test_name, save_name, versioned) \
  709. namespace detail \
  710. { \
  711. template <class T, class A, class U = void> \
  712. struct has_non_member_##test_name##_impl \
  713. { \
  714. template <class TT, class AA> \
  715. static auto test(int) -> decltype( CEREAL_LOAD_MINIMAL_FUNCTION_NAME( \
  716. std::declval<AA const &>(), std::declval<TT&>(), AnyConvert() versioned ), yes() ); \
  717. template <class, class> static no test( ... ); \
  718. static const bool exists = std::is_same<decltype( test<T, A>( 0 ) ), yes>::value; \
  719. \
  720. template <class TT, class AA, class UU> \
  721. static auto test2(int) -> decltype( CEREAL_LOAD_MINIMAL_FUNCTION_NAME( \
  722. std::declval<AA const &>(), std::declval<TT&>(), NoConvertConstRef<UU>() versioned ), yes() ); \
  723. template <class, class, class> static no test2( ... ); \
  724. static const bool valid = std::is_same<decltype( test2<T, A, U>( 0 ) ), yes>::value; \
  725. \
  726. template <class TT, class AA> \
  727. static auto test3(int) -> decltype( CEREAL_LOAD_MINIMAL_FUNCTION_NAME( \
  728. std::declval<AA const &>(), NoConvertRef<TT>(), AnyConvert() versioned ), yes() ); \
  729. template <class, class> static no test3( ... ); \
  730. static const bool const_valid = std::is_same<decltype( test3<T, A>( 0 ) ), yes>::value; \
  731. }; \
  732. \
  733. template <class T, class A, bool Valid> \
  734. struct has_non_member_##test_name##_wrapper : std::false_type {}; \
  735. \
  736. template <class T, class A> \
  737. struct has_non_member_##test_name##_wrapper<T, A, true> \
  738. { \
  739. using AOut = typename detail::get_output_from_input<A>::type; \
  740. \
  741. static_assert( detail::has_non_member_##save_name##_impl<T, AOut>::valid, \
  742. "cereal detected non-member " #test_name " but no valid non-member " #save_name ". \n " \
  743. "cannot evaluate correctness of " #test_name " without valid " #save_name "." ); \
  744. \
  745. using SaveType = typename detail::get_non_member_##save_name##_type<T, AOut, true>::type; \
  746. using check = has_non_member_##test_name##_impl<T, A, SaveType>; \
  747. static const bool value = check::exists; \
  748. \
  749. static_assert( check::valid || !check::exists, "cereal detected different types in corresponding non-member " \
  750. #test_name " and " #save_name " functions. \n " \
  751. "the paramater to " #test_name " must be a constant reference to the type that " #save_name " returns." ); \
  752. static_assert( check::const_valid || !check::exists, \
  753. "cereal detected an invalid serialization type parameter in non-member " #test_name ". " \
  754. #test_name " non-member functions must accept their serialization type by non-const reference" ); \
  755. }; \
  756. } /* namespace detail */ \
  757. \
  758. template <class T, class A> \
  759. struct has_non_member_##test_name : std::integral_constant<bool, \
  760. detail::has_non_member_##test_name##_wrapper<T, A, detail::has_non_member_##test_name##_impl<T, A>::exists>::value> {};
  761. // ######################################################################
  762. // Non-Member Load Minimal
  763. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST(load_minimal, save_minimal, )
  764. // ######################################################################
  765. // Non-Member Load Minimal (versioned)
  766. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST(versioned_load_minimal, versioned_save_minimal, CEREAL_MAKE_VERSIONED_TEST)
  767. // ######################################################################
  768. #undef CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST
  769. // ######################################################################
  770. //! Member load and construct check
  771. template<typename T, typename A>
  772. struct has_member_load_and_construct : std::integral_constant<bool,
  773. std::is_same<decltype( access::load_and_construct<T>( std::declval<A&>(), std::declval< ::cereal::construct<T>&>() ) ), void>::value>
  774. { };
  775. // ######################################################################
  776. //! Member load and construct check (versioned)
  777. template<typename T, typename A>
  778. struct has_member_versioned_load_and_construct : std::integral_constant<bool,
  779. std::is_same<decltype( access::load_and_construct<T>( std::declval<A&>(), std::declval< ::cereal::construct<T>&>(), 0 ) ), void>::value>
  780. { };
  781. // ######################################################################
  782. //! Creates a test for whether a non-member load_and_construct specialization exists
  783. /*! This creates a class derived from std::integral_constant that will be true if
  784. the type has the proper non-member function for the given archive. */
  785. #define CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST(test_name, versioned) \
  786. namespace detail \
  787. { \
  788. template <class T, class A> \
  789. struct has_non_member_##test_name##_impl \
  790. { \
  791. template <class TT, class AA> \
  792. static auto test(int) -> decltype( LoadAndConstruct<TT>::load_and_construct( \
  793. std::declval<AA&>(), std::declval< ::cereal::construct<TT>&>() versioned ), yes()); \
  794. template <class, class> \
  795. static no test( ... ); \
  796. static const bool value = std::is_same<decltype( test<T, A>( 0 ) ), yes>::value; \
  797. }; \
  798. } /* end namespace detail */ \
  799. template <class T, class A> \
  800. struct has_non_member_##test_name : std::integral_constant<bool, detail::has_non_member_##test_name##_impl<T, A>::value> {};
  801. // ######################################################################
  802. //! Non member load and construct check
  803. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST(load_and_construct, )
  804. // ######################################################################
  805. //! Non member load and construct check (versioned)
  806. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST(versioned_load_and_construct, CEREAL_MAKE_VERSIONED_TEST)
  807. // ######################################################################
  808. //! Has either a member or non member load and construct
  809. template<typename T, typename A>
  810. struct has_load_and_construct : std::integral_constant<bool,
  811. has_member_load_and_construct<T, A>::value || has_non_member_load_and_construct<T, A>::value ||
  812. has_member_versioned_load_and_construct<T, A>::value || has_non_member_versioned_load_and_construct<T, A>::value>
  813. { };
  814. // ######################################################################
  815. #undef CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST
  816. // ######################################################################
  817. // End of serialization existence tests
  818. #undef CEREAL_MAKE_VERSIONED_TEST
  819. // ######################################################################
  820. template <class T, class InputArchive, class OutputArchive>
  821. struct has_member_split : std::integral_constant<bool,
  822. (has_member_load<T, InputArchive>::value && has_member_save<T, OutputArchive>::value) ||
  823. (has_member_versioned_load<T, InputArchive>::value && has_member_versioned_save<T, OutputArchive>::value)> {};
  824. // ######################################################################
  825. template <class T, class InputArchive, class OutputArchive>
  826. struct has_non_member_split : std::integral_constant<bool,
  827. (has_non_member_load<T, InputArchive>::value && has_non_member_save<T, OutputArchive>::value) ||
  828. (has_non_member_versioned_load<T, InputArchive>::value && has_non_member_versioned_save<T, OutputArchive>::value)> {};
  829. // ######################################################################
  830. template <class T, class OutputArchive>
  831. struct has_invalid_output_versioning : std::integral_constant<bool,
  832. (has_member_versioned_save<T, OutputArchive>::value && has_member_save<T, OutputArchive>::value) ||
  833. (has_non_member_versioned_save<T, OutputArchive>::value && has_non_member_save<T, OutputArchive>::value) ||
  834. (has_member_versioned_serialize<T, OutputArchive>::value && has_member_serialize<T, OutputArchive>::value) ||
  835. (has_non_member_versioned_serialize<T, OutputArchive>::value && has_non_member_serialize<T, OutputArchive>::value) ||
  836. (has_member_versioned_save_minimal<T, OutputArchive>::value && has_member_save_minimal<T, OutputArchive>::value) ||
  837. (has_non_member_versioned_save_minimal<T, OutputArchive>::value && has_non_member_save_minimal<T, OutputArchive>::value)> {};
  838. // ######################################################################
  839. template <class T, class InputArchive>
  840. struct has_invalid_input_versioning : std::integral_constant<bool,
  841. (has_member_versioned_load<T, InputArchive>::value && has_member_load<T, InputArchive>::value) ||
  842. (has_non_member_versioned_load<T, InputArchive>::value && has_non_member_load<T, InputArchive>::value) ||
  843. (has_member_versioned_serialize<T, InputArchive>::value && has_member_serialize<T, InputArchive>::value) ||
  844. (has_non_member_versioned_serialize<T, InputArchive>::value && has_non_member_serialize<T, InputArchive>::value) ||
  845. (has_member_versioned_load_minimal<T, InputArchive>::value && has_member_load_minimal<T, InputArchive>::value) ||
  846. (has_non_member_versioned_load_minimal<T, InputArchive>::value && has_non_member_load_minimal<T, InputArchive>::value)> {};
  847. // ######################################################################
  848. namespace detail
  849. {
  850. //! Create a test for a cereal::specialization entry
  851. #define CEREAL_MAKE_IS_SPECIALIZED_IMPL(name) \
  852. template <class T, class A> \
  853. struct is_specialized_##name : std::integral_constant<bool, \
  854. !std::is_base_of<std::false_type, specialize<A, T, specialization::name>>::value> {}
  855. CEREAL_MAKE_IS_SPECIALIZED_IMPL(member_serialize);
  856. CEREAL_MAKE_IS_SPECIALIZED_IMPL(member_load_save);
  857. CEREAL_MAKE_IS_SPECIALIZED_IMPL(member_load_save_minimal);
  858. CEREAL_MAKE_IS_SPECIALIZED_IMPL(non_member_serialize);
  859. CEREAL_MAKE_IS_SPECIALIZED_IMPL(non_member_load_save);
  860. CEREAL_MAKE_IS_SPECIALIZED_IMPL(non_member_load_save_minimal);
  861. #undef CEREAL_MAKE_IS_SPECIALIZED_IMPL
  862. //! Number of specializations detected
  863. template <class T, class A>
  864. struct count_specializations : std::integral_constant<int,
  865. is_specialized_member_serialize<T, A>::value +
  866. is_specialized_member_load_save<T, A>::value +
  867. is_specialized_member_load_save_minimal<T, A>::value +
  868. is_specialized_non_member_serialize<T, A>::value +
  869. is_specialized_non_member_load_save<T, A>::value +
  870. is_specialized_non_member_load_save_minimal<T, A>::value> {};
  871. } // namespace detail
  872. //! Check if any specialization exists for a type
  873. template <class T, class A>
  874. struct is_specialized : std::integral_constant<bool,
  875. detail::is_specialized_member_serialize<T, A>::value ||
  876. detail::is_specialized_member_load_save<T, A>::value ||
  877. detail::is_specialized_member_load_save_minimal<T, A>::value ||
  878. detail::is_specialized_non_member_serialize<T, A>::value ||
  879. detail::is_specialized_non_member_load_save<T, A>::value ||
  880. detail::is_specialized_non_member_load_save_minimal<T, A>::value>
  881. {
  882. static_assert(detail::count_specializations<T, A>::value <= 1, "More than one explicit specialization detected for type.");
  883. };
  884. //! Create the static assertion for some specialization
  885. /*! This assertion will fail if the type is indeed specialized and does not have the appropriate
  886. type of serialization functions */
  887. #define CEREAL_MAKE_IS_SPECIALIZED_ASSERT(name, versioned_name, print_name, spec_name) \
  888. static_assert( (is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value && \
  889. (has_##name<T, A>::value || has_##versioned_name<T, A>::value)) \
  890. || !(is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value), \
  891. "cereal detected " #print_name " specialization but no " #print_name " serialize function" )
  892. //! Generates a test for specialization for versioned and unversioned functions
  893. /*! This creates checks that can be queried to see if a given type of serialization function
  894. has been specialized for this type */
  895. #define CEREAL_MAKE_IS_SPECIALIZED(name, versioned_name, spec_name) \
  896. template <class T, class A> \
  897. struct is_specialized_##name : std::integral_constant<bool, \
  898. is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value> \
  899. { CEREAL_MAKE_IS_SPECIALIZED_ASSERT(name, versioned_name, name, spec_name); }; \
  900. template <class T, class A> \
  901. struct is_specialized_##versioned_name : std::integral_constant<bool, \
  902. is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value> \
  903. { CEREAL_MAKE_IS_SPECIALIZED_ASSERT(name, versioned_name, versioned_name, spec_name); }
  904. CEREAL_MAKE_IS_SPECIALIZED(member_serialize, member_versioned_serialize, member_serialize);
  905. CEREAL_MAKE_IS_SPECIALIZED(non_member_serialize, non_member_versioned_serialize, non_member_serialize);
  906. CEREAL_MAKE_IS_SPECIALIZED(member_save, member_versioned_save, member_load_save);
  907. CEREAL_MAKE_IS_SPECIALIZED(non_member_save, non_member_versioned_save, non_member_load_save);
  908. CEREAL_MAKE_IS_SPECIALIZED(member_load, member_versioned_load, member_load_save);
  909. CEREAL_MAKE_IS_SPECIALIZED(non_member_load, non_member_versioned_load, non_member_load_save);
  910. CEREAL_MAKE_IS_SPECIALIZED(member_save_minimal, member_versioned_save_minimal, member_load_save_minimal);
  911. CEREAL_MAKE_IS_SPECIALIZED(non_member_save_minimal, non_member_versioned_save_minimal, non_member_load_save_minimal);
  912. CEREAL_MAKE_IS_SPECIALIZED(member_load_minimal, member_versioned_load_minimal, member_load_save_minimal);
  913. CEREAL_MAKE_IS_SPECIALIZED(non_member_load_minimal, non_member_versioned_load_minimal, non_member_load_save_minimal);
  914. #undef CEREAL_MAKE_IS_SPECIALIZED_ASSERT
  915. #undef CEREAL_MAKE_IS_SPECIALIZED
  916. // ######################################################################
  917. // detects if a type has any active minimal output serialization
  918. template <class T, class OutputArchive>
  919. struct has_minimal_output_serialization : std::integral_constant<bool,
  920. is_specialized_member_save_minimal<T, OutputArchive>::value ||
  921. ((has_member_save_minimal<T, OutputArchive>::value ||
  922. has_non_member_save_minimal<T, OutputArchive>::value ||
  923. has_member_versioned_save_minimal<T, OutputArchive>::value ||
  924. has_non_member_versioned_save_minimal<T, OutputArchive>::value) &&
  925. !(is_specialized_member_serialize<T, OutputArchive>::value ||
  926. is_specialized_member_save<T, OutputArchive>::value))> {};
  927. // ######################################################################
  928. // detects if a type has any active minimal input serialization
  929. template <class T, class InputArchive>
  930. struct has_minimal_input_serialization : std::integral_constant<bool,
  931. is_specialized_member_load_minimal<T, InputArchive>::value ||
  932. ((has_member_load_minimal<T, InputArchive>::value ||
  933. has_non_member_load_minimal<T, InputArchive>::value ||
  934. has_member_versioned_load_minimal<T, InputArchive>::value ||
  935. has_non_member_versioned_load_minimal<T, InputArchive>::value) &&
  936. !(is_specialized_member_serialize<T, InputArchive>::value ||
  937. is_specialized_member_load<T, InputArchive>::value))> {};
  938. // ######################################################################
  939. namespace detail
  940. {
  941. //! The number of output serialization functions available
  942. /*! If specialization is being used, we'll count only those; otherwise we'll count everything */
  943. template <class T, class OutputArchive>
  944. struct count_output_serializers : std::integral_constant<int,
  945. count_specializations<T, OutputArchive>::value ? count_specializations<T, OutputArchive>::value :
  946. has_member_save<T, OutputArchive>::value +
  947. has_non_member_save<T, OutputArchive>::value +
  948. has_member_serialize<T, OutputArchive>::value +
  949. has_non_member_serialize<T, OutputArchive>::value +
  950. has_member_save_minimal<T, OutputArchive>::value +
  951. has_non_member_save_minimal<T, OutputArchive>::value +
  952. /*-versioned---------------------------------------------------------*/
  953. has_member_versioned_save<T, OutputArchive>::value +
  954. has_non_member_versioned_save<T, OutputArchive>::value +
  955. has_member_versioned_serialize<T, OutputArchive>::value +
  956. has_non_member_versioned_serialize<T, OutputArchive>::value +
  957. has_member_versioned_save_minimal<T, OutputArchive>::value +
  958. has_non_member_versioned_save_minimal<T, OutputArchive>::value> {};
  959. }
  960. template <class T, class OutputArchive>
  961. struct is_output_serializable : std::integral_constant<bool,
  962. detail::count_output_serializers<T, OutputArchive>::value == 1> {};
  963. // ######################################################################
  964. namespace detail
  965. {
  966. //! The number of input serialization functions available
  967. /*! If specialization is being used, we'll count only those; otherwise we'll count everything */
  968. template <class T, class InputArchive>
  969. struct count_input_serializers : std::integral_constant<int,
  970. count_specializations<T, InputArchive>::value ? count_specializations<T, InputArchive>::value :
  971. has_member_load<T, InputArchive>::value +
  972. has_non_member_load<T, InputArchive>::value +
  973. has_member_serialize<T, InputArchive>::value +
  974. has_non_member_serialize<T, InputArchive>::value +
  975. has_member_load_minimal<T, InputArchive>::value +
  976. has_non_member_load_minimal<T, InputArchive>::value +
  977. /*-versioned---------------------------------------------------------*/
  978. has_member_versioned_load<T, InputArchive>::value +
  979. has_non_member_versioned_load<T, InputArchive>::value +
  980. has_member_versioned_serialize<T, InputArchive>::value +
  981. has_non_member_versioned_serialize<T, InputArchive>::value +
  982. has_member_versioned_load_minimal<T, InputArchive>::value +
  983. has_non_member_versioned_load_minimal<T, InputArchive>::value> {};
  984. }
  985. template <class T, class InputArchive>
  986. struct is_input_serializable : std::integral_constant<bool,
  987. detail::count_input_serializers<T, InputArchive>::value == 1> {};
  988. // ######################################################################
  989. // Base Class Support
  990. namespace detail
  991. {
  992. struct base_class_id
  993. {
  994. template<class T>
  995. base_class_id(T const * const t) :
  996. type(typeid(T)),
  997. ptr(t),
  998. hash(std::hash<std::type_index>()(typeid(T)) ^ (std::hash<void const *>()(t) << 1))
  999. { }
  1000. bool operator==(base_class_id const & other) const
  1001. { return (type == other.type) && (ptr == other.ptr); }
  1002. std::type_index type;
  1003. void const * ptr;
  1004. size_t hash;
  1005. };
  1006. struct base_class_id_hash { size_t operator()(base_class_id const & id) const { return id.hash; } };
  1007. } // namespace detail
  1008. namespace detail
  1009. {
  1010. //! Common base type for base class casting
  1011. struct BaseCastBase {};
  1012. template <class>
  1013. struct get_base_class;
  1014. template <template<typename> class Cast, class Base>
  1015. struct get_base_class<Cast<Base>>
  1016. {
  1017. using type = Base;
  1018. };
  1019. //! Base class cast, behave as the test
  1020. template <class Cast, template<class, class> class Test, class Archive,
  1021. bool IsBaseCast = std::is_base_of<BaseCastBase, Cast>::value>
  1022. struct has_minimal_base_class_serialization_impl : Test<typename get_base_class<Cast>::type, Archive>
  1023. { };
  1024. //! Not a base class cast
  1025. template <class Cast, template<class, class> class Test, class Archive>
  1026. struct has_minimal_base_class_serialization_impl<Cast,Test, Archive, false> : std::false_type
  1027. { };
  1028. }
  1029. //! Checks to see if the base class used in a cast has a minimal serialization
  1030. /*! @tparam Cast Either base_class or virtual_base_class wrapped type
  1031. @tparam Test A has_minimal test (for either input or output)
  1032. @tparam Archive The archive to use with the test */
  1033. template <class Cast, template<class, class> class Test, class Archive>
  1034. struct has_minimal_base_class_serialization : detail::has_minimal_base_class_serialization_impl<Cast, Test, Archive>
  1035. { };
  1036. // ######################################################################
  1037. namespace detail
  1038. {
  1039. struct shared_from_this_wrapper
  1040. {
  1041. template <class U>
  1042. static auto (check)( U const & t ) -> decltype( ::cereal::access::shared_from_this(t), std::true_type() );
  1043. static auto (check)( ... ) -> decltype( std::false_type() );
  1044. template <class U>
  1045. static auto get( U const & t ) -> decltype( t.shared_from_this() );
  1046. };
  1047. }
  1048. //! Determine if T or any base class of T has inherited from std::enable_shared_from_this
  1049. template<class T>
  1050. struct has_shared_from_this : decltype((detail::shared_from_this_wrapper::check)(std::declval<T>()))
  1051. { };
  1052. //! Get the type of the base class of T which inherited from std::enable_shared_from_this
  1053. template <class T>
  1054. struct get_shared_from_this_base
  1055. {
  1056. private:
  1057. using PtrType = decltype(detail::shared_from_this_wrapper::get(std::declval<T>()));
  1058. public:
  1059. //! The type of the base of T that inherited from std::enable_shared_from_this
  1060. using type = typename std::decay<typename PtrType::element_type>::type;
  1061. };
  1062. // ######################################################################
  1063. //! Extracts the true type from something possibly wrapped in a cereal NoConvert
  1064. /*! Internally cereal uses some wrapper classes to test the validity of non-member
  1065. minimal load and save functions. This can interfere with user type traits on
  1066. templated load and save minimal functions. To get to the correct underlying type,
  1067. users should use strip_minimal when performing any enable_if type type trait checks.
  1068. See the enum serialization in types/common.hpp for an example of using this */
  1069. template <class T, bool IsCerealMinimalTrait = std::is_base_of<detail::NoConvertBase, T>::value>
  1070. struct strip_minimal
  1071. {
  1072. using type = T;
  1073. };
  1074. //! Specialization for types wrapped in a NoConvert
  1075. template <class T>
  1076. struct strip_minimal<T, true>
  1077. {
  1078. using type = typename T::type;
  1079. };
  1080. // ######################################################################
  1081. //! Determines whether the class T can be default constructed by cereal::access
  1082. template <class T>
  1083. struct is_default_constructible
  1084. {
  1085. #ifdef CEREAL_OLDER_GCC
  1086. template <class TT, class SFINAE = void>
  1087. struct test : no {};
  1088. template <class TT>
  1089. struct test<TT, typename detail::Void< decltype( cereal::access::construct<TT>() ) >::type> : yes {};
  1090. static const bool value = test<T>();
  1091. #else // NOT CEREAL_OLDER_GCC =========================================
  1092. template <class TT>
  1093. static auto test(int) -> decltype( cereal::access::construct<TT>(), yes());
  1094. template <class>
  1095. static no test(...);
  1096. static const bool value = std::is_same<decltype(test<T>(0)), yes>::value;
  1097. #endif // NOT CEREAL_OLDER_GCC
  1098. };
  1099. // ######################################################################
  1100. namespace detail
  1101. {
  1102. //! Removes all qualifiers and minimal wrappers from an archive
  1103. template <class A>
  1104. using decay_archive = typename std::decay<typename strip_minimal<A>::type>::type;
  1105. }
  1106. //! Checks if the provided archive type is equal to some cereal archive type
  1107. /*! This automatically does things such as std::decay and removing any other wrappers that may be
  1108. on the Archive template parameter.
  1109. Example use:
  1110. @code{cpp}
  1111. // example use to disable a serialization function
  1112. template <class Archive, EnableIf<cereal::traits::is_same_archive<Archive, cereal::BinaryOutputArchive>::value> = sfinae>
  1113. void save( Archive & ar, MyType const & mt );
  1114. @endcode */
  1115. template <class ArchiveT, class CerealArchiveT>
  1116. struct is_same_archive : std::integral_constant<bool,
  1117. std::is_same<detail::decay_archive<ArchiveT>, CerealArchiveT>::value>
  1118. { };
  1119. // ######################################################################
  1120. //! A macro to use to restrict which types of archives your function will work for.
  1121. /*! This requires you to have a template class parameter named Archive and replaces the void return
  1122. type for your function.
  1123. INTYPE refers to the input archive type you wish to restrict on.
  1124. OUTTYPE refers to the output archive type you wish to restrict on.
  1125. For example, if we want to limit a serialize to only work with binary serialization:
  1126. @code{.cpp}
  1127. template <class Archive>
  1128. CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
  1129. serialize( Archive & ar, MyCoolType & m )
  1130. {
  1131. ar & m;
  1132. }
  1133. @endcode
  1134. If you need to do more restrictions in your enable_if, you will need to do this by hand.
  1135. */
  1136. #define CEREAL_ARCHIVE_RESTRICT(INTYPE, OUTTYPE) \
  1137. typename std::enable_if<cereal::traits::is_same_archive<Archive, INTYPE>::value || cereal::traits::is_same_archive<Archive, OUTTYPE>::value, void>::type
  1138. //! Type traits only struct used to mark an archive as human readable (text based)
  1139. /*! Archives that wish to identify as text based/human readable should inherit from
  1140. this struct */
  1141. struct TextArchive {};
  1142. //! Checks if an archive is a text archive (human readable)
  1143. template <class A>
  1144. struct is_text_archive : std::integral_constant<bool,
  1145. std::is_base_of<TextArchive, detail::decay_archive<A>>::value>
  1146. { };
  1147. } // namespace traits
  1148. // ######################################################################
  1149. namespace detail
  1150. {
  1151. template <class T, class A,
  1152. bool Member = traits::has_member_load_and_construct<T, A>::value,
  1153. bool MemberVersioned = traits::has_member_versioned_load_and_construct<T, A>::value,
  1154. bool NonMember = traits::has_non_member_load_and_construct<T, A>::value,
  1155. bool NonMemberVersioned = traits::has_non_member_versioned_load_and_construct<T, A>::value>
  1156. struct Construct
  1157. {
  1158. static_assert( cereal::traits::detail::delay_static_assert<T>::value,
  1159. "cereal found more than one compatible load_and_construct function for the provided type and archive combination. \n\n "
  1160. "Types must either have a member load_and_construct function or a non-member specialization of LoadAndConstruct (you may not mix these). \n "
  1161. "In addition, you may not mix versioned with non-versioned load_and_construct functions. \n\n " );
  1162. static T * load_andor_construct( A & /*ar*/, construct<T> & /*construct*/ )
  1163. { return nullptr; }
  1164. };
  1165. // no load and construct case
  1166. template <class T, class A>
  1167. struct Construct<T, A, false, false, false, false>
  1168. {
  1169. static_assert( ::cereal::traits::is_default_constructible<T>::value,
  1170. "Trying to serialize a an object with no default constructor. \n\n "
  1171. "Types must either be default constructible or define either a member or non member Construct function. \n "
  1172. "Construct functions generally have the signature: \n\n "
  1173. "template <class Archive> \n "
  1174. "static void load_and_construct(Archive & ar, cereal::construct<T> & construct) \n "
  1175. "{ \n "
  1176. " var a; \n "
  1177. " ar( a ) \n "
  1178. " construct( a ); \n "
  1179. "} \n\n" );
  1180. static T * load_andor_construct()
  1181. { return ::cereal::access::construct<T>(); }
  1182. };
  1183. // member non-versioned
  1184. template <class T, class A>
  1185. struct Construct<T, A, true, false, false, false>
  1186. {
  1187. static void load_andor_construct( A & ar, construct<T> & construct )
  1188. {
  1189. access::load_and_construct<T>( ar, construct );
  1190. }
  1191. };
  1192. // member versioned
  1193. template <class T, class A>
  1194. struct Construct<T, A, false, true, false, false>
  1195. {
  1196. static void load_andor_construct( A & ar, construct<T> & construct )
  1197. {
  1198. const auto version = ar.template loadClassVersion<T>();
  1199. access::load_and_construct<T>( ar, construct, version );
  1200. }
  1201. };
  1202. // non-member non-versioned
  1203. template <class T, class A>
  1204. struct Construct<T, A, false, false, true, false>
  1205. {
  1206. static void load_andor_construct( A & ar, construct<T> & construct )
  1207. {
  1208. LoadAndConstruct<T>::load_and_construct( ar, construct );
  1209. }
  1210. };
  1211. // non-member versioned
  1212. template <class T, class A>
  1213. struct Construct<T, A, false, false, false, true>
  1214. {
  1215. static void load_andor_construct( A & ar, construct<T> & construct )
  1216. {
  1217. const auto version = ar.template loadClassVersion<T>();
  1218. LoadAndConstruct<T>::load_and_construct( ar, construct, version );
  1219. }
  1220. };
  1221. } // namespace detail
  1222. } // namespace cereal
  1223. #endif // CEREAL_DETAILS_TRAITS_HPP_