optional.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/archive/detail/basic_iarchive.hpp>
  14. #include <boost/optional.hpp>
  15. #include <boost/move/utility_core.hpp>
  16. #include <boost/serialization/item_version_type.hpp>
  17. #include <boost/serialization/split_free.hpp>
  18. #include <boost/serialization/level.hpp>
  19. #include <boost/serialization/nvp.hpp>
  20. #include <boost/serialization/version.hpp>
  21. #include <boost/type_traits/is_pointer.hpp>
  22. #include <boost/serialization/detail/stack_constructor.hpp>
  23. #include <boost/serialization/detail/is_default_constructible.hpp>
  24. #include <boost/serialization/force_include.hpp>
  25. // function specializations must be defined in the appropriate
  26. // namespace - boost::serialization
  27. namespace boost {
  28. namespace serialization {
  29. template<class Archive, class T>
  30. void save(
  31. Archive & ar,
  32. const boost::optional< T > & t,
  33. const unsigned int /*version*/
  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. #if ! defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
  41. BOOST_STATIC_ASSERT(
  42. boost::serialization::detail::is_default_constructible<T>::value
  43. || boost::is_pointer<T>::value
  44. );
  45. #endif
  46. const bool tflag = t.is_initialized();
  47. ar << boost::serialization::make_nvp("initialized", tflag);
  48. if (tflag){
  49. ar << boost::serialization::make_nvp("value", *t);
  50. }
  51. }
  52. template<class Archive, class T>
  53. void load(
  54. Archive & ar,
  55. boost::optional< T > & t,
  56. const unsigned int version
  57. ){
  58. bool tflag;
  59. ar >> boost::serialization::make_nvp("initialized", tflag);
  60. if(! tflag){
  61. t.reset();
  62. return;
  63. }
  64. if(0 == version){
  65. boost::serialization::item_version_type item_version(0);
  66. boost::archive::library_version_type library_version(
  67. ar.get_library_version()
  68. );
  69. if(boost::archive::library_version_type(3) < library_version){
  70. ar >> BOOST_SERIALIZATION_NVP(item_version);
  71. }
  72. }
  73. if(! t.is_initialized())
  74. t = T();
  75. ar >> boost::serialization::make_nvp("value", *t);
  76. }
  77. template<class Archive, class T>
  78. void serialize(
  79. Archive & ar,
  80. boost::optional< T > & t,
  81. const unsigned int version
  82. ){
  83. boost::serialization::split_free(ar, t, version);
  84. }
  85. template<class T>
  86. struct version<boost::optional<T> > {
  87. BOOST_STATIC_CONSTANT(int, value = 1);
  88. };
  89. } // serialization
  90. } // boost
  91. #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_