optional.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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) && (_MSC_VER >= 1020)
  10. # pragma once
  11. #endif
  12. #include <boost/config.hpp>
  13. #include <boost/optional.hpp>
  14. #include <boost/serialization/split_free.hpp>
  15. #include <boost/serialization/level.hpp>
  16. #include <boost/serialization/nvp.hpp>
  17. #include <boost/serialization/version.hpp>
  18. #include <boost/serialization/detail/stack_constructor.hpp>
  19. // function specializations must be defined in the appropriate
  20. // namespace - boost::serialization
  21. namespace boost {
  22. namespace serialization {
  23. template<class Archive, class T>
  24. void save(
  25. Archive & ar,
  26. const boost::optional<T> & t,
  27. const unsigned int /*version*/
  28. ){
  29. const bool tflag = t.is_initialized();
  30. ar << boost::serialization::make_nvp("initialized", tflag);
  31. if (tflag){
  32. if(3 < ar.get_library_version()){
  33. const int v = version<T>::value;
  34. ar << boost::serialization::make_nvp("item_version", v);
  35. }
  36. ar << boost::serialization::make_nvp("value", *t);
  37. }
  38. }
  39. template<class Archive, class T>
  40. void load(
  41. Archive & ar,
  42. boost::optional<T> & t,
  43. const unsigned int /*version*/
  44. ){
  45. bool tflag;
  46. ar >> boost::serialization::make_nvp("initialized", tflag);
  47. if (tflag){
  48. unsigned int v = 0;
  49. if(3 < ar.get_library_version()){
  50. ar >> boost::serialization::make_nvp("item_version", v);
  51. }
  52. detail::stack_construct<Archive, T> aux(ar, v);
  53. ar >> boost::serialization::make_nvp("value", aux.reference());
  54. t.reset(aux.reference());
  55. }
  56. else {
  57. t.reset();
  58. }
  59. }
  60. template<class Archive, class T>
  61. void serialize(
  62. Archive & ar,
  63. boost::optional<T> & t,
  64. const unsigned int version
  65. ){
  66. boost::serialization::split_free(ar, t, version);
  67. }
  68. // the following would be slightly more efficient. But it
  69. // would mean that archives created with programs that support
  70. // TPS wouldn't be readable by programs that don't support TPS.
  71. // Hence we decline to support this otherwise convenient optimization.
  72. //#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  73. #if 0
  74. template <class T>
  75. struct implementation_level<optional<T> >
  76. {
  77. typedef mpl::integral_c_tag tag;
  78. typedef mpl::int_<boost::serialization::object_serializable> type;
  79. BOOST_STATIC_CONSTANT(
  80. int ,
  81. value = boost::serialization::implementation_level::type::value
  82. );
  83. };
  84. template<class T>
  85. struct tracking_level<optional<T> >
  86. {
  87. typedef mpl::integral_c_tag tag;
  88. typedef mpl::int_<boost::serialization::track_never> type;
  89. BOOST_STATIC_CONSTANT(
  90. int ,
  91. value = boost::serialization::tracking_level::type::value
  92. );
  93. };
  94. #endif
  95. } // serialization
  96. } // namespace boost
  97. #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_