traits.hpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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 the copyright holder 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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. Note that there should be an additional const check on load_minimal after the valid check,
  706. but this currently interferes with many valid uses of minimal serialization. It has been
  707. removed (see #565 on github) and previously was:
  708. @code
  709. static_assert( check::const_valid || !check::exists,
  710. "cereal detected an invalid serialization type parameter in non-member " #test_name ". "
  711. #test_name " non-member functions must accept their serialization type by non-const reference" );
  712. @endcode
  713. See #132, #436, #263, and #565 on https://github.com/USCiLab/cereal for more details.
  714. @param test_name The name to give the test (e.g. load_minimal or versioned_load_minimal)
  715. @param save_name The corresponding name the save test would have (e.g. save_minimal or versioned_save_minimal)
  716. @param versioned Either blank or the macro CEREAL_MAKE_VERSIONED_TEST */
  717. #define CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST(test_name, save_name, versioned) \
  718. namespace detail \
  719. { \
  720. template <class T, class A, class U = void> \
  721. struct has_non_member_##test_name##_impl \
  722. { \
  723. template <class TT, class AA> \
  724. static auto test(int) -> decltype( CEREAL_LOAD_MINIMAL_FUNCTION_NAME( \
  725. std::declval<AA const &>(), std::declval<TT&>(), AnyConvert() versioned ), yes() ); \
  726. template <class, class> static no test( ... ); \
  727. static const bool exists = std::is_same<decltype( test<T, A>( 0 ) ), yes>::value; \
  728. \
  729. template <class TT, class AA, class UU> \
  730. static auto test2(int) -> decltype( CEREAL_LOAD_MINIMAL_FUNCTION_NAME( \
  731. std::declval<AA const &>(), std::declval<TT&>(), NoConvertConstRef<UU>() versioned ), yes() ); \
  732. template <class, class, class> static no test2( ... ); \
  733. static const bool valid = std::is_same<decltype( test2<T, A, U>( 0 ) ), yes>::value; \
  734. \
  735. template <class TT, class AA> \
  736. static auto test3(int) -> decltype( CEREAL_LOAD_MINIMAL_FUNCTION_NAME( \
  737. std::declval<AA const &>(), NoConvertRef<TT>(), AnyConvert() versioned ), yes() ); \
  738. template <class, class> static no test3( ... ); \
  739. static const bool const_valid = std::is_same<decltype( test3<T, A>( 0 ) ), yes>::value; \
  740. }; \
  741. \
  742. template <class T, class A, bool Valid> \
  743. struct has_non_member_##test_name##_wrapper : std::false_type {}; \
  744. \
  745. template <class T, class A> \
  746. struct has_non_member_##test_name##_wrapper<T, A, true> \
  747. { \
  748. using AOut = typename detail::get_output_from_input<A>::type; \
  749. \
  750. static_assert( detail::has_non_member_##save_name##_impl<T, AOut>::valid, \
  751. "cereal detected non-member " #test_name " but no valid non-member " #save_name ". \n " \
  752. "cannot evaluate correctness of " #test_name " without valid " #save_name "." ); \
  753. \
  754. using SaveType = typename detail::get_non_member_##save_name##_type<T, AOut, true>::type; \
  755. using check = has_non_member_##test_name##_impl<T, A, SaveType>; \
  756. static const bool value = check::exists; \
  757. \
  758. static_assert( check::valid || !check::exists, "cereal detected different types in corresponding non-member " \
  759. #test_name " and " #save_name " functions. \n " \
  760. "the paramater to " #test_name " must be a constant reference to the type that " #save_name " returns." ); \
  761. }; \
  762. } /* namespace detail */ \
  763. \
  764. template <class T, class A> \
  765. struct has_non_member_##test_name : std::integral_constant<bool, \
  766. detail::has_non_member_##test_name##_wrapper<T, A, detail::has_non_member_##test_name##_impl<T, A>::exists>::value> {};
  767. // ######################################################################
  768. // Non-Member Load Minimal
  769. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST(load_minimal, save_minimal, )
  770. // ######################################################################
  771. // Non-Member Load Minimal (versioned)
  772. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST(versioned_load_minimal, versioned_save_minimal, CEREAL_MAKE_VERSIONED_TEST)
  773. // ######################################################################
  774. #undef CEREAL_MAKE_HAS_NON_MEMBER_LOAD_MINIMAL_TEST
  775. // ######################################################################
  776. namespace detail
  777. {
  778. // const stripped away before reaching here, prevents errors on conversion from
  779. // construct<const T> to construct<T>
  780. template<typename T, typename A>
  781. struct has_member_load_and_construct_impl : std::integral_constant<bool,
  782. std::is_same<decltype( access::load_and_construct<T>( std::declval<A&>(), std::declval< ::cereal::construct<T>&>() ) ), void>::value>
  783. { };
  784. template<typename T, typename A>
  785. struct has_member_versioned_load_and_construct_impl : std::integral_constant<bool,
  786. std::is_same<decltype( access::load_and_construct<T>( std::declval<A&>(), std::declval< ::cereal::construct<T>&>(), 0 ) ), void>::value>
  787. { };
  788. } // namespace detail
  789. //! Member load and construct check
  790. template<typename T, typename A>
  791. struct has_member_load_and_construct : detail::has_member_load_and_construct_impl<typename std::remove_const<T>::type, A>
  792. { };
  793. //! Member load and construct check (versioned)
  794. template<typename T, typename A>
  795. struct has_member_versioned_load_and_construct : detail::has_member_versioned_load_and_construct_impl<typename std::remove_const<T>::type, A>
  796. { };
  797. // ######################################################################
  798. //! Creates a test for whether a non-member load_and_construct specialization exists
  799. /*! This creates a class derived from std::integral_constant that will be true if
  800. the type has the proper non-member function for the given archive. */
  801. #define CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST(test_name, versioned) \
  802. namespace detail \
  803. { \
  804. template <class T, class A> \
  805. struct has_non_member_##test_name##_impl \
  806. { \
  807. template <class TT, class AA> \
  808. static auto test(int) -> decltype( LoadAndConstruct<TT>::load_and_construct( \
  809. std::declval<AA&>(), std::declval< ::cereal::construct<TT>&>() versioned ), yes()); \
  810. template <class, class> \
  811. static no test( ... ); \
  812. static const bool value = std::is_same<decltype( test<T, A>( 0 ) ), yes>::value; \
  813. }; \
  814. } /* end namespace detail */ \
  815. template <class T, class A> \
  816. struct has_non_member_##test_name : \
  817. std::integral_constant<bool, detail::has_non_member_##test_name##_impl<typename std::remove_const<T>::type, A>::value> {};
  818. // ######################################################################
  819. //! Non member load and construct check
  820. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST(load_and_construct, )
  821. // ######################################################################
  822. //! Non member load and construct check (versioned)
  823. CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST(versioned_load_and_construct, CEREAL_MAKE_VERSIONED_TEST)
  824. // ######################################################################
  825. //! Has either a member or non member load and construct
  826. template<typename T, typename A>
  827. struct has_load_and_construct : std::integral_constant<bool,
  828. has_member_load_and_construct<T, A>::value || has_non_member_load_and_construct<T, A>::value ||
  829. has_member_versioned_load_and_construct<T, A>::value || has_non_member_versioned_load_and_construct<T, A>::value>
  830. { };
  831. // ######################################################################
  832. #undef CEREAL_MAKE_HAS_NON_MEMBER_LOAD_AND_CONSTRUCT_TEST
  833. // ######################################################################
  834. // End of serialization existence tests
  835. #undef CEREAL_MAKE_VERSIONED_TEST
  836. // ######################################################################
  837. template <class T, class InputArchive, class OutputArchive>
  838. struct has_member_split : std::integral_constant<bool,
  839. (has_member_load<T, InputArchive>::value && has_member_save<T, OutputArchive>::value) ||
  840. (has_member_versioned_load<T, InputArchive>::value && has_member_versioned_save<T, OutputArchive>::value)> {};
  841. // ######################################################################
  842. template <class T, class InputArchive, class OutputArchive>
  843. struct has_non_member_split : std::integral_constant<bool,
  844. (has_non_member_load<T, InputArchive>::value && has_non_member_save<T, OutputArchive>::value) ||
  845. (has_non_member_versioned_load<T, InputArchive>::value && has_non_member_versioned_save<T, OutputArchive>::value)> {};
  846. // ######################################################################
  847. template <class T, class OutputArchive>
  848. struct has_invalid_output_versioning : std::integral_constant<bool,
  849. (has_member_versioned_save<T, OutputArchive>::value && has_member_save<T, OutputArchive>::value) ||
  850. (has_non_member_versioned_save<T, OutputArchive>::value && has_non_member_save<T, OutputArchive>::value) ||
  851. (has_member_versioned_serialize<T, OutputArchive>::value && has_member_serialize<T, OutputArchive>::value) ||
  852. (has_non_member_versioned_serialize<T, OutputArchive>::value && has_non_member_serialize<T, OutputArchive>::value) ||
  853. (has_member_versioned_save_minimal<T, OutputArchive>::value && has_member_save_minimal<T, OutputArchive>::value) ||
  854. (has_non_member_versioned_save_minimal<T, OutputArchive>::value && has_non_member_save_minimal<T, OutputArchive>::value)> {};
  855. // ######################################################################
  856. template <class T, class InputArchive>
  857. struct has_invalid_input_versioning : std::integral_constant<bool,
  858. (has_member_versioned_load<T, InputArchive>::value && has_member_load<T, InputArchive>::value) ||
  859. (has_non_member_versioned_load<T, InputArchive>::value && has_non_member_load<T, InputArchive>::value) ||
  860. (has_member_versioned_serialize<T, InputArchive>::value && has_member_serialize<T, InputArchive>::value) ||
  861. (has_non_member_versioned_serialize<T, InputArchive>::value && has_non_member_serialize<T, InputArchive>::value) ||
  862. (has_member_versioned_load_minimal<T, InputArchive>::value && has_member_load_minimal<T, InputArchive>::value) ||
  863. (has_non_member_versioned_load_minimal<T, InputArchive>::value && has_non_member_load_minimal<T, InputArchive>::value)> {};
  864. // ######################################################################
  865. namespace detail
  866. {
  867. //! Create a test for a cereal::specialization entry
  868. #define CEREAL_MAKE_IS_SPECIALIZED_IMPL(name) \
  869. template <class T, class A> \
  870. struct is_specialized_##name : std::integral_constant<bool, \
  871. !std::is_base_of<std::false_type, specialize<A, T, specialization::name>>::value> {}
  872. CEREAL_MAKE_IS_SPECIALIZED_IMPL(member_serialize);
  873. CEREAL_MAKE_IS_SPECIALIZED_IMPL(member_load_save);
  874. CEREAL_MAKE_IS_SPECIALIZED_IMPL(member_load_save_minimal);
  875. CEREAL_MAKE_IS_SPECIALIZED_IMPL(non_member_serialize);
  876. CEREAL_MAKE_IS_SPECIALIZED_IMPL(non_member_load_save);
  877. CEREAL_MAKE_IS_SPECIALIZED_IMPL(non_member_load_save_minimal);
  878. #undef CEREAL_MAKE_IS_SPECIALIZED_IMPL
  879. //! Number of specializations detected
  880. template <class T, class A>
  881. struct count_specializations : std::integral_constant<int,
  882. is_specialized_member_serialize<T, A>::value +
  883. is_specialized_member_load_save<T, A>::value +
  884. is_specialized_member_load_save_minimal<T, A>::value +
  885. is_specialized_non_member_serialize<T, A>::value +
  886. is_specialized_non_member_load_save<T, A>::value +
  887. is_specialized_non_member_load_save_minimal<T, A>::value> {};
  888. } // namespace detail
  889. //! Check if any specialization exists for a type
  890. template <class T, class A>
  891. struct is_specialized : std::integral_constant<bool,
  892. detail::is_specialized_member_serialize<T, A>::value ||
  893. detail::is_specialized_member_load_save<T, A>::value ||
  894. detail::is_specialized_member_load_save_minimal<T, A>::value ||
  895. detail::is_specialized_non_member_serialize<T, A>::value ||
  896. detail::is_specialized_non_member_load_save<T, A>::value ||
  897. detail::is_specialized_non_member_load_save_minimal<T, A>::value>
  898. {
  899. static_assert(detail::count_specializations<T, A>::value <= 1, "More than one explicit specialization detected for type.");
  900. };
  901. //! Create the static assertion for some specialization
  902. /*! This assertion will fail if the type is indeed specialized and does not have the appropriate
  903. type of serialization functions */
  904. #define CEREAL_MAKE_IS_SPECIALIZED_ASSERT(name, versioned_name, print_name, spec_name) \
  905. static_assert( (is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value && \
  906. (has_##name<T, A>::value || has_##versioned_name<T, A>::value)) \
  907. || !(is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value), \
  908. "cereal detected " #print_name " specialization but no " #print_name " serialize function" )
  909. //! Generates a test for specialization for versioned and unversioned functions
  910. /*! This creates checks that can be queried to see if a given type of serialization function
  911. has been specialized for this type */
  912. #define CEREAL_MAKE_IS_SPECIALIZED(name, versioned_name, spec_name) \
  913. template <class T, class A> \
  914. struct is_specialized_##name : std::integral_constant<bool, \
  915. is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value> \
  916. { CEREAL_MAKE_IS_SPECIALIZED_ASSERT(name, versioned_name, name, spec_name); }; \
  917. template <class T, class A> \
  918. struct is_specialized_##versioned_name : std::integral_constant<bool, \
  919. is_specialized<T, A>::value && detail::is_specialized_##spec_name<T, A>::value> \
  920. { CEREAL_MAKE_IS_SPECIALIZED_ASSERT(name, versioned_name, versioned_name, spec_name); }
  921. CEREAL_MAKE_IS_SPECIALIZED(member_serialize, member_versioned_serialize, member_serialize);
  922. CEREAL_MAKE_IS_SPECIALIZED(non_member_serialize, non_member_versioned_serialize, non_member_serialize);
  923. CEREAL_MAKE_IS_SPECIALIZED(member_save, member_versioned_save, member_load_save);
  924. CEREAL_MAKE_IS_SPECIALIZED(non_member_save, non_member_versioned_save, non_member_load_save);
  925. CEREAL_MAKE_IS_SPECIALIZED(member_load, member_versioned_load, member_load_save);
  926. CEREAL_MAKE_IS_SPECIALIZED(non_member_load, non_member_versioned_load, non_member_load_save);
  927. CEREAL_MAKE_IS_SPECIALIZED(member_save_minimal, member_versioned_save_minimal, member_load_save_minimal);
  928. CEREAL_MAKE_IS_SPECIALIZED(non_member_save_minimal, non_member_versioned_save_minimal, non_member_load_save_minimal);
  929. CEREAL_MAKE_IS_SPECIALIZED(member_load_minimal, member_versioned_load_minimal, member_load_save_minimal);
  930. CEREAL_MAKE_IS_SPECIALIZED(non_member_load_minimal, non_member_versioned_load_minimal, non_member_load_save_minimal);
  931. #undef CEREAL_MAKE_IS_SPECIALIZED_ASSERT
  932. #undef CEREAL_MAKE_IS_SPECIALIZED
  933. // ######################################################################
  934. // detects if a type has any active minimal output serialization
  935. template <class T, class OutputArchive>
  936. struct has_minimal_output_serialization : std::integral_constant<bool,
  937. is_specialized_member_save_minimal<T, OutputArchive>::value ||
  938. ((has_member_save_minimal<T, OutputArchive>::value ||
  939. has_non_member_save_minimal<T, OutputArchive>::value ||
  940. has_member_versioned_save_minimal<T, OutputArchive>::value ||
  941. has_non_member_versioned_save_minimal<T, OutputArchive>::value) &&
  942. !(is_specialized_member_serialize<T, OutputArchive>::value ||
  943. is_specialized_member_save<T, OutputArchive>::value))> {};
  944. // ######################################################################
  945. // detects if a type has any active minimal input serialization
  946. template <class T, class InputArchive>
  947. struct has_minimal_input_serialization : std::integral_constant<bool,
  948. is_specialized_member_load_minimal<T, InputArchive>::value ||
  949. ((has_member_load_minimal<T, InputArchive>::value ||
  950. has_non_member_load_minimal<T, InputArchive>::value ||
  951. has_member_versioned_load_minimal<T, InputArchive>::value ||
  952. has_non_member_versioned_load_minimal<T, InputArchive>::value) &&
  953. !(is_specialized_member_serialize<T, InputArchive>::value ||
  954. is_specialized_member_load<T, InputArchive>::value))> {};
  955. // ######################################################################
  956. namespace detail
  957. {
  958. //! The number of output serialization functions available
  959. /*! If specialization is being used, we'll count only those; otherwise we'll count everything */
  960. template <class T, class OutputArchive>
  961. struct count_output_serializers : std::integral_constant<int,
  962. count_specializations<T, OutputArchive>::value ? count_specializations<T, OutputArchive>::value :
  963. has_member_save<T, OutputArchive>::value +
  964. has_non_member_save<T, OutputArchive>::value +
  965. has_member_serialize<T, OutputArchive>::value +
  966. has_non_member_serialize<T, OutputArchive>::value +
  967. has_member_save_minimal<T, OutputArchive>::value +
  968. has_non_member_save_minimal<T, OutputArchive>::value +
  969. /*-versioned---------------------------------------------------------*/
  970. has_member_versioned_save<T, OutputArchive>::value +
  971. has_non_member_versioned_save<T, OutputArchive>::value +
  972. has_member_versioned_serialize<T, OutputArchive>::value +
  973. has_non_member_versioned_serialize<T, OutputArchive>::value +
  974. has_member_versioned_save_minimal<T, OutputArchive>::value +
  975. has_non_member_versioned_save_minimal<T, OutputArchive>::value> {};
  976. }
  977. template <class T, class OutputArchive>
  978. struct is_output_serializable : std::integral_constant<bool,
  979. detail::count_output_serializers<T, OutputArchive>::value == 1> {};
  980. // ######################################################################
  981. namespace detail
  982. {
  983. //! The number of input serialization functions available
  984. /*! If specialization is being used, we'll count only those; otherwise we'll count everything */
  985. template <class T, class InputArchive>
  986. struct count_input_serializers : std::integral_constant<int,
  987. count_specializations<T, InputArchive>::value ? count_specializations<T, InputArchive>::value :
  988. has_member_load<T, InputArchive>::value +
  989. has_non_member_load<T, InputArchive>::value +
  990. has_member_serialize<T, InputArchive>::value +
  991. has_non_member_serialize<T, InputArchive>::value +
  992. has_member_load_minimal<T, InputArchive>::value +
  993. has_non_member_load_minimal<T, InputArchive>::value +
  994. /*-versioned---------------------------------------------------------*/
  995. has_member_versioned_load<T, InputArchive>::value +
  996. has_non_member_versioned_load<T, InputArchive>::value +
  997. has_member_versioned_serialize<T, InputArchive>::value +
  998. has_non_member_versioned_serialize<T, InputArchive>::value +
  999. has_member_versioned_load_minimal<T, InputArchive>::value +
  1000. has_non_member_versioned_load_minimal<T, InputArchive>::value> {};
  1001. }
  1002. template <class T, class InputArchive>
  1003. struct is_input_serializable : std::integral_constant<bool,
  1004. detail::count_input_serializers<T, InputArchive>::value == 1> {};
  1005. // ######################################################################
  1006. // Base Class Support
  1007. namespace detail
  1008. {
  1009. struct base_class_id
  1010. {
  1011. template<class T>
  1012. base_class_id(T const * const t) :
  1013. type(typeid(T)),
  1014. ptr(t),
  1015. hash(std::hash<std::type_index>()(typeid(T)) ^ (std::hash<void const *>()(t) << 1))
  1016. { }
  1017. bool operator==(base_class_id const & other) const
  1018. { return (type == other.type) && (ptr == other.ptr); }
  1019. std::type_index type;
  1020. void const * ptr;
  1021. size_t hash;
  1022. };
  1023. struct base_class_id_hash { size_t operator()(base_class_id const & id) const { return id.hash; } };
  1024. } // namespace detail
  1025. namespace detail
  1026. {
  1027. //! Common base type for base class casting
  1028. struct BaseCastBase {};
  1029. template <class>
  1030. struct get_base_class;
  1031. template <template<typename> class Cast, class Base>
  1032. struct get_base_class<Cast<Base>>
  1033. {
  1034. using type = Base;
  1035. };
  1036. //! Base class cast, behave as the test
  1037. template <class Cast, template<class, class> class Test, class Archive,
  1038. bool IsBaseCast = std::is_base_of<BaseCastBase, Cast>::value>
  1039. struct has_minimal_base_class_serialization_impl : Test<typename get_base_class<Cast>::type, Archive>
  1040. { };
  1041. //! Not a base class cast
  1042. template <class Cast, template<class, class> class Test, class Archive>
  1043. struct has_minimal_base_class_serialization_impl<Cast,Test, Archive, false> : std::false_type
  1044. { };
  1045. }
  1046. //! Checks to see if the base class used in a cast has a minimal serialization
  1047. /*! @tparam Cast Either base_class or virtual_base_class wrapped type
  1048. @tparam Test A has_minimal test (for either input or output)
  1049. @tparam Archive The archive to use with the test */
  1050. template <class Cast, template<class, class> class Test, class Archive>
  1051. struct has_minimal_base_class_serialization : detail::has_minimal_base_class_serialization_impl<Cast, Test, Archive>
  1052. { };
  1053. // ######################################################################
  1054. namespace detail
  1055. {
  1056. struct shared_from_this_wrapper
  1057. {
  1058. template <class U>
  1059. static auto (check)( U const & t ) -> decltype( ::cereal::access::shared_from_this(t), std::true_type() );
  1060. static auto (check)( ... ) -> decltype( std::false_type() );
  1061. template <class U>
  1062. static auto get( U const & t ) -> decltype( t.shared_from_this() );
  1063. };
  1064. }
  1065. //! Determine if T or any base class of T has inherited from std::enable_shared_from_this
  1066. template<class T>
  1067. struct has_shared_from_this : decltype((detail::shared_from_this_wrapper::check)(std::declval<T>()))
  1068. { };
  1069. //! Get the type of the base class of T which inherited from std::enable_shared_from_this
  1070. template <class T>
  1071. struct get_shared_from_this_base
  1072. {
  1073. private:
  1074. using PtrType = decltype(detail::shared_from_this_wrapper::get(std::declval<T>()));
  1075. public:
  1076. //! The type of the base of T that inherited from std::enable_shared_from_this
  1077. using type = typename std::decay<typename PtrType::element_type>::type;
  1078. };
  1079. // ######################################################################
  1080. //! Extracts the true type from something possibly wrapped in a cereal NoConvert
  1081. /*! Internally cereal uses some wrapper classes to test the validity of non-member
  1082. minimal load and save functions. This can interfere with user type traits on
  1083. templated load and save minimal functions. To get to the correct underlying type,
  1084. users should use strip_minimal when performing any enable_if type type trait checks.
  1085. See the enum serialization in types/common.hpp for an example of using this */
  1086. template <class T, bool IsCerealMinimalTrait = std::is_base_of<detail::NoConvertBase, T>::value>
  1087. struct strip_minimal
  1088. {
  1089. using type = T;
  1090. };
  1091. //! Specialization for types wrapped in a NoConvert
  1092. template <class T>
  1093. struct strip_minimal<T, true>
  1094. {
  1095. using type = typename T::type;
  1096. };
  1097. // ######################################################################
  1098. //! Determines whether the class T can be default constructed by cereal::access
  1099. template <class T>
  1100. struct is_default_constructible
  1101. {
  1102. #ifdef CEREAL_OLDER_GCC
  1103. template <class TT, class SFINAE = void>
  1104. struct test : no {};
  1105. template <class TT>
  1106. struct test<TT, typename detail::Void< decltype( cereal::access::construct<TT>() ) >::type> : yes {};
  1107. static const bool value = test<T>();
  1108. #else // NOT CEREAL_OLDER_GCC =========================================
  1109. template <class TT>
  1110. static auto test(int) -> decltype( cereal::access::construct<TT>(), yes());
  1111. template <class>
  1112. static no test(...);
  1113. static const bool value = std::is_same<decltype(test<T>(0)), yes>::value;
  1114. #endif // NOT CEREAL_OLDER_GCC
  1115. };
  1116. // ######################################################################
  1117. namespace detail
  1118. {
  1119. //! Removes all qualifiers and minimal wrappers from an archive
  1120. template <class A>
  1121. using decay_archive = typename std::decay<typename strip_minimal<A>::type>::type;
  1122. }
  1123. //! Checks if the provided archive type is equal to some cereal archive type
  1124. /*! This automatically does things such as std::decay and removing any other wrappers that may be
  1125. on the Archive template parameter.
  1126. Example use:
  1127. @code{cpp}
  1128. // example use to disable a serialization function
  1129. template <class Archive, EnableIf<cereal::traits::is_same_archive<Archive, cereal::BinaryOutputArchive>::value> = sfinae>
  1130. void save( Archive & ar, MyType const & mt );
  1131. @endcode */
  1132. template <class ArchiveT, class CerealArchiveT>
  1133. struct is_same_archive : std::integral_constant<bool,
  1134. std::is_same<detail::decay_archive<ArchiveT>, CerealArchiveT>::value>
  1135. { };
  1136. // ######################################################################
  1137. //! A macro to use to restrict which types of archives your function will work for.
  1138. /*! This requires you to have a template class parameter named Archive and replaces the void return
  1139. type for your function.
  1140. INTYPE refers to the input archive type you wish to restrict on.
  1141. OUTTYPE refers to the output archive type you wish to restrict on.
  1142. For example, if we want to limit a serialize to only work with binary serialization:
  1143. @code{.cpp}
  1144. template <class Archive>
  1145. CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
  1146. serialize( Archive & ar, MyCoolType & m )
  1147. {
  1148. ar & m;
  1149. }
  1150. @endcode
  1151. If you need to do more restrictions in your enable_if, you will need to do this by hand.
  1152. */
  1153. #define CEREAL_ARCHIVE_RESTRICT(INTYPE, OUTTYPE) \
  1154. typename std::enable_if<cereal::traits::is_same_archive<Archive, INTYPE>::value || cereal::traits::is_same_archive<Archive, OUTTYPE>::value, void>::type
  1155. //! Type traits only struct used to mark an archive as human readable (text based)
  1156. /*! Archives that wish to identify as text based/human readable should inherit from
  1157. this struct */
  1158. struct TextArchive {};
  1159. //! Checks if an archive is a text archive (human readable)
  1160. template <class A>
  1161. struct is_text_archive : std::integral_constant<bool,
  1162. std::is_base_of<TextArchive, detail::decay_archive<A>>::value>
  1163. { };
  1164. } // namespace traits
  1165. // ######################################################################
  1166. namespace detail
  1167. {
  1168. template <class T, class A,
  1169. bool Member = traits::has_member_load_and_construct<T, A>::value,
  1170. bool MemberVersioned = traits::has_member_versioned_load_and_construct<T, A>::value,
  1171. bool NonMember = traits::has_non_member_load_and_construct<T, A>::value,
  1172. bool NonMemberVersioned = traits::has_non_member_versioned_load_and_construct<T, A>::value>
  1173. struct Construct
  1174. {
  1175. static_assert( cereal::traits::detail::delay_static_assert<T>::value,
  1176. "cereal found more than one compatible load_and_construct function for the provided type and archive combination. \n\n "
  1177. "Types must either have a member load_and_construct function or a non-member specialization of LoadAndConstruct (you may not mix these). \n "
  1178. "In addition, you may not mix versioned with non-versioned load_and_construct functions. \n\n " );
  1179. static T * load_andor_construct( A & /*ar*/, construct<T> & /*construct*/ )
  1180. { return nullptr; }
  1181. };
  1182. // no load and construct case
  1183. template <class T, class A>
  1184. struct Construct<T, A, false, false, false, false>
  1185. {
  1186. static_assert( ::cereal::traits::is_default_constructible<T>::value,
  1187. "Trying to serialize a an object with no default constructor. \n\n "
  1188. "Types must either be default constructible or define either a member or non member Construct function. \n "
  1189. "Construct functions generally have the signature: \n\n "
  1190. "template <class Archive> \n "
  1191. "static void load_and_construct(Archive & ar, cereal::construct<T> & construct) \n "
  1192. "{ \n "
  1193. " var a; \n "
  1194. " ar( a ) \n "
  1195. " construct( a ); \n "
  1196. "} \n\n" );
  1197. static T * load_andor_construct()
  1198. { return ::cereal::access::construct<T>(); }
  1199. };
  1200. // member non-versioned
  1201. template <class T, class A>
  1202. struct Construct<T, A, true, false, false, false>
  1203. {
  1204. static void load_andor_construct( A & ar, construct<T> & construct )
  1205. {
  1206. access::load_and_construct<T>( ar, construct );
  1207. }
  1208. };
  1209. // member versioned
  1210. template <class T, class A>
  1211. struct Construct<T, A, false, true, false, false>
  1212. {
  1213. static void load_andor_construct( A & ar, construct<T> & construct )
  1214. {
  1215. const auto version = ar.template loadClassVersion<T>();
  1216. access::load_and_construct<T>( ar, construct, version );
  1217. }
  1218. };
  1219. // non-member non-versioned
  1220. template <class T, class A>
  1221. struct Construct<T, A, false, false, true, false>
  1222. {
  1223. static void load_andor_construct( A & ar, construct<T> & construct )
  1224. {
  1225. LoadAndConstruct<T>::load_and_construct( ar, construct );
  1226. }
  1227. };
  1228. // non-member versioned
  1229. template <class T, class A>
  1230. struct Construct<T, A, false, false, false, true>
  1231. {
  1232. static void load_andor_construct( A & ar, construct<T> & construct )
  1233. {
  1234. const auto version = ar.template loadClassVersion<T>();
  1235. LoadAndConstruct<T>::load_and_construct( ar, construct, version );
  1236. }
  1237. };
  1238. } // namespace detail
  1239. } // namespace cereal
  1240. #endif // CEREAL_DETAILS_TRAITS_HPP_