optional.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include <boost/serialization/item_version_type.hpp>
  15. #include <boost/serialization/library_version_type.hpp>
  16. #include <boost/serialization/version.hpp>
  17. #include <boost/serialization/split_free.hpp>
  18. #include <boost/serialization/nvp.hpp>
  19. #include <boost/type_traits/is_pointer.hpp>
  20. #include <boost/serialization/detail/is_default_constructible.hpp>
  21. // function specializations must be defined in the appropriate
  22. // namespace - boost::serialization
  23. namespace boost {
  24. namespace serialization {
  25. template<class Archive, class T>
  26. void save(
  27. Archive & ar,
  28. const boost::optional< T > & t,
  29. const unsigned int /*version*/
  30. ){
  31. // It is an inherent limitation to the serialization of optional.hpp
  32. // that the underlying type must be either a pointer or must have a
  33. // default constructor. It's possible that this could change sometime
  34. // in the future, but for now, one will have to work around it. This can
  35. // be done by serialization the optional<T> as optional<T *>
  36. #if ! defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
  37. BOOST_STATIC_ASSERT(
  38. boost::serialization::detail::is_default_constructible<T>::value
  39. || boost::is_pointer<T>::value
  40. );
  41. #endif
  42. const bool tflag = t.is_initialized();
  43. ar << boost::serialization::make_nvp("initialized", tflag);
  44. if (tflag){
  45. ar << boost::serialization::make_nvp("value", *t);
  46. }
  47. }
  48. template<class Archive, class T>
  49. void load(
  50. Archive & ar,
  51. boost::optional< T > & t,
  52. const unsigned int version
  53. ){
  54. bool tflag;
  55. ar >> boost::serialization::make_nvp("initialized", tflag);
  56. if(! tflag){
  57. t.reset();
  58. return;
  59. }
  60. if(0 == version){
  61. boost::serialization::item_version_type item_version(0);
  62. boost::serialization::library_version_type library_version(
  63. ar.get_library_version()
  64. );
  65. if(boost::serialization::library_version_type(3) < library_version){
  66. ar >> BOOST_SERIALIZATION_NVP(item_version);
  67. }
  68. }
  69. if(! t.is_initialized())
  70. t = T();
  71. ar >> boost::serialization::make_nvp("value", *t);
  72. }
  73. template<class Archive, class T>
  74. void serialize(
  75. Archive & ar,
  76. boost::optional< T > & t,
  77. const unsigned int version
  78. ){
  79. boost::serialization::split_free(ar, t, version);
  80. }
  81. template<class T>
  82. struct version<boost::optional<T> > {
  83. BOOST_STATIC_CONSTANT(int, value = 1);
  84. };
  85. } // serialization
  86. } // boost
  87. #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_