serialization.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef BOOST_CORE_SERIALIZATION_HPP_INCLUDED
  2. #define BOOST_CORE_SERIALIZATION_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // Copyright 2023 Peter Dimov
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // https://www.boost.org/LICENSE_1_0.txt
  10. //
  11. // Utilities needed to implement serialization support
  12. // without including a Boost.Serialization header
  13. #include <boost/core/nvp.hpp>
  14. #include <cstddef>
  15. namespace boost
  16. {
  17. namespace serialization
  18. {
  19. // Forward declarations (needed for specializations)
  20. template<class T> struct version;
  21. class access;
  22. // Our own version_type replacement. This has to be in
  23. // the `serialization` namespace, because its only purpose
  24. // is to add `serialization` as an associated namespace.
  25. struct core_version_type
  26. {
  27. unsigned int version_;
  28. core_version_type( unsigned int version ): version_( version ) {}
  29. operator unsigned int () const { return version_; }
  30. };
  31. } // namespace serialization
  32. namespace core
  33. {
  34. // nvp
  35. using serialization::nvp;
  36. using serialization::make_nvp;
  37. // split_free
  38. namespace detail
  39. {
  40. template<bool IsSaving> struct load_or_save_f;
  41. template<> struct load_or_save_f<true>
  42. {
  43. template<class A, class T> void operator()( A& a, T& t, unsigned int v ) const
  44. {
  45. save( a, t, serialization::core_version_type( v ) );
  46. }
  47. };
  48. template<> struct load_or_save_f<false>
  49. {
  50. template<class A, class T> void operator()( A& a, T& t, unsigned int v ) const
  51. {
  52. load( a, t, serialization::core_version_type( v ) );
  53. }
  54. };
  55. } // namespace detail
  56. template<class A, class T> inline void split_free( A& a, T& t, unsigned int v )
  57. {
  58. detail::load_or_save_f< A::is_saving::value >()( a, t, v );
  59. }
  60. // split_member
  61. namespace detail
  62. {
  63. template<bool IsSaving, class Access = serialization::access> struct load_or_save_m;
  64. template<class Access> struct load_or_save_m<true, Access>
  65. {
  66. template<class A, class T> void operator()( A& a, T const& t, unsigned int v ) const
  67. {
  68. Access::member_save( a, t, v );
  69. }
  70. };
  71. template<class Access> struct load_or_save_m<false, Access>
  72. {
  73. template<class A, class T> void operator()( A& a, T& t, unsigned int v ) const
  74. {
  75. Access::member_load( a, t, v );
  76. }
  77. };
  78. } // namespace detail
  79. template<class A, class T> inline void split_member( A& a, T& t, unsigned int v )
  80. {
  81. detail::load_or_save_m< A::is_saving::value >()( a, t, v );
  82. }
  83. // load_construct_data_adl
  84. template<class Ar, class T> void load_construct_data_adl( Ar& ar, T* t, unsigned int v )
  85. {
  86. load_construct_data( ar, t, serialization::core_version_type( v ) );
  87. }
  88. // save_construct_data_adl
  89. template<class Ar, class T> void save_construct_data_adl( Ar& ar, T const* t, unsigned int v )
  90. {
  91. save_construct_data( ar, t, serialization::core_version_type( v ) );
  92. }
  93. } // namespace core
  94. } // namespace boost
  95. #endif // #ifndef BOOST_CORE_SERIALIZATION_HPP_INCLUDED