forward_list.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef BOOST_SERIALIZATION_FORWARD_LIST_HPP
  2. #define BOOST_SERIALIZATION_FORWARD_LIST_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // forward_list.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <boost/config.hpp>
  15. #include <forward_list>
  16. #include <iterator> // distance
  17. #include <boost/serialization/collections_save_imp.hpp>
  18. #include <boost/serialization/collections_load_imp.hpp>
  19. #include <boost/archive/detail/basic_iarchive.hpp>
  20. #include <boost/serialization/nvp.hpp>
  21. #include <boost/serialization/collection_size_type.hpp>
  22. #include <boost/serialization/item_version_type.hpp>
  23. #include <boost/serialization/split_free.hpp>
  24. #include <boost/serialization/detail/stack_constructor.hpp>
  25. #include <boost/serialization/detail/is_default_constructible.hpp>
  26. #include <boost/move/utility_core.hpp>
  27. namespace boost {
  28. namespace serialization {
  29. template<class Archive, class U, class Allocator>
  30. inline void save(
  31. Archive & ar,
  32. const std::forward_list<U, Allocator> &t,
  33. const unsigned int /*file_version*/
  34. ){
  35. const collection_size_type count(std::distance(t.cbegin(), t.cend()));
  36. boost::serialization::stl::save_collection<
  37. Archive,
  38. std::forward_list<U, Allocator>
  39. >(ar, t, count);
  40. }
  41. namespace stl {
  42. template<
  43. class Archive,
  44. class T,
  45. class Allocator
  46. >
  47. typename boost::disable_if<
  48. typename detail::is_default_constructible<
  49. typename std::forward_list<T, Allocator>::value_type
  50. >,
  51. void
  52. >::type
  53. collection_load_impl(
  54. Archive & ar,
  55. std::forward_list<T, Allocator> &t,
  56. collection_size_type count,
  57. item_version_type item_version
  58. ){
  59. t.clear();
  60. boost::serialization::detail::stack_construct<Archive, T> u(ar, item_version);
  61. ar >> boost::serialization::make_nvp("item", u.reference());
  62. t.push_front(boost::move(u.reference()));
  63. typename std::forward_list<T, Allocator>::iterator last;
  64. last = t.begin();
  65. ar.reset_object_address(&(*t.begin()) , & u.reference());
  66. while(--count > 0){
  67. detail::stack_construct<Archive, T> u(ar, item_version);
  68. ar >> boost::serialization::make_nvp("item", u.reference());
  69. last = t.insert_after(last, boost::move(u.reference()));
  70. ar.reset_object_address(&(*last) , & u.reference());
  71. }
  72. }
  73. } // stl
  74. template<class Archive, class U, class Allocator>
  75. inline void load(
  76. Archive & ar,
  77. std::forward_list<U, Allocator> &t,
  78. const unsigned int /*file_version*/
  79. ){
  80. const boost::archive::library_version_type library_version(
  81. ar.get_library_version()
  82. );
  83. // retrieve number of elements
  84. item_version_type item_version(0);
  85. collection_size_type count;
  86. ar >> BOOST_SERIALIZATION_NVP(count);
  87. if(boost::archive::library_version_type(3) < library_version){
  88. ar >> BOOST_SERIALIZATION_NVP(item_version);
  89. }
  90. stl::collection_load_impl(ar, t, count, item_version);
  91. }
  92. // split non-intrusive serialization function member into separate
  93. // non intrusive save/load member functions
  94. template<class Archive, class U, class Allocator>
  95. inline void serialize(
  96. Archive & ar,
  97. std::forward_list<U, Allocator> &t,
  98. const unsigned int file_version
  99. ){
  100. boost::serialization::split_free(ar, t, file_version);
  101. }
  102. } // serialization
  103. } // namespace boost
  104. #include <boost/serialization/collection_traits.hpp>
  105. BOOST_SERIALIZATION_COLLECTION_TRAITS(std::forward_list)
  106. #endif // BOOST_SERIALIZATION_FORWARD_LIST_HPP