optional.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // (C) Copyright 2002-4 Pavel Vozenilek .
  3. // Use, modification and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Provides non-intrusive serialization for boost::optional.
  7. #ifndef BOOST_SERIALIZATION_OPTIONAL_HPP
  8. #define BOOST_SERIALIZATION_OPTIONAL_HPP
  9. #if defined(_MSC_VER)
  10. # pragma once
  11. #endif
  12. #include <boost/config.hpp>
  13. #include <boost/optional.hpp>
  14. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  15. #include <optional>
  16. #endif
  17. #include <boost/serialization/item_version_type.hpp>
  18. #include <boost/serialization/library_version_type.hpp>
  19. #include <boost/serialization/version.hpp>
  20. #include <boost/serialization/split_free.hpp>
  21. #include <boost/serialization/nvp.hpp>
  22. #include <boost/type_traits/is_pointer.hpp>
  23. #include <boost/serialization/detail/is_default_constructible.hpp>
  24. // function specializations must be defined in the appropriate
  25. // namespace - boost::serialization
  26. namespace boost {
  27. namespace serialization {
  28. namespace detail {
  29. // OT is of the form optional<T>
  30. template<class Archive, class OT>
  31. void save_impl(
  32. Archive & ar,
  33. const OT & ot
  34. ){
  35. // It is an inherent limitation to the serialization of optional.hpp
  36. // that the underlying type must be either a pointer or must have a
  37. // default constructor. It's possible that this could change sometime
  38. // in the future, but for now, one will have to work around it. This can
  39. // be done by serialization the optional<T> as optional<T *>
  40. #ifndef BOOST_NO_CXX11_HDR_TYPE_TRAITS
  41. BOOST_STATIC_ASSERT(
  42. boost::serialization::detail::is_default_constructible<typename OT::value_type>::value
  43. || boost::is_pointer<typename OT::value_type>::value
  44. );
  45. #endif
  46. const bool tflag(ot);
  47. ar << boost::serialization::make_nvp("initialized", tflag);
  48. if (tflag){
  49. ar << boost::serialization::make_nvp("value", *ot);
  50. }
  51. }
  52. // OT is of the form optional<T>
  53. template<class Archive, class OT>
  54. void load_impl(
  55. Archive & ar,
  56. OT & ot,
  57. const unsigned int version
  58. ){
  59. bool tflag;
  60. ar >> boost::serialization::make_nvp("initialized", tflag);
  61. if(! tflag){
  62. ot.reset();
  63. return;
  64. }
  65. if(0 == version){
  66. boost::serialization::item_version_type item_version(0);
  67. boost::serialization::library_version_type library_version(
  68. ar.get_library_version()
  69. );
  70. if(boost::serialization::library_version_type(3) < library_version){
  71. ar >> BOOST_SERIALIZATION_NVP(item_version);
  72. }
  73. }
  74. typename OT::value_type t;
  75. ar >> boost::serialization::make_nvp("value",t);
  76. ot = t;
  77. }
  78. } // detail
  79. template<class Archive, class T>
  80. void save(
  81. Archive & ar,
  82. const boost::optional< T > & ot,
  83. const unsigned int /*version*/
  84. ){
  85. detail::save_impl(ar, ot);
  86. }
  87. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  88. template<class Archive, class T>
  89. void save(
  90. Archive & ar,
  91. const std::optional< T > & ot,
  92. const unsigned int /*version*/
  93. ){
  94. detail::save_impl(ar, ot);
  95. }
  96. #endif
  97. template<class Archive, class T>
  98. void load(
  99. Archive & ar,
  100. boost::optional< T > & ot,
  101. const unsigned int version
  102. ){
  103. detail::load_impl(ar, ot, version);
  104. }
  105. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  106. template<class Archive, class T>
  107. void load(
  108. Archive & ar,
  109. std::optional< T > & ot,
  110. const unsigned int version
  111. ){
  112. detail::load_impl(ar, ot, version);
  113. }
  114. #endif
  115. template<class Archive, class T>
  116. void serialize(
  117. Archive & ar,
  118. boost::optional< T > & ot,
  119. const unsigned int version
  120. ){
  121. boost::serialization::split_free(ar, ot, version);
  122. }
  123. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  124. template<class Archive, class T>
  125. void serialize(
  126. Archive & ar,
  127. std::optional< T > & ot,
  128. const unsigned int version
  129. ){
  130. boost::serialization::split_free(ar, ot, version);
  131. }
  132. #endif
  133. template<class T>
  134. struct version<boost::optional<T> >{
  135. BOOST_STATIC_CONSTANT(int, value = 1);
  136. };
  137. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  138. template<class T>
  139. struct version<std::optional<T> >{
  140. BOOST_STATIC_CONSTANT(int, value = 1);
  141. };
  142. #endif
  143. } // serialization
  144. } // boost
  145. #endif // BOOST_SERIALIZATION_OPTIONAL_HPP