unordered_collections_save_imp.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_SAVE_IMP_HPP
  2. #define BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_SAVE_IMP_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // hash_collections_save_imp.hpp: serialization for stl collections
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // (C) Copyright 2014 Jim Bell
  11. // Use, modification and distribution is subject to the Boost Software
  12. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. // See http://www.boost.org for updates, documentation, and revision history.
  15. // helper function templates for serialization of collections
  16. #include <boost/config.hpp>
  17. #include <boost/serialization/nvp.hpp>
  18. #include <boost/serialization/serialization.hpp>
  19. #include <boost/serialization/version.hpp>
  20. #include <boost/serialization/collection_size_type.hpp>
  21. #include <boost/serialization/item_version_type.hpp>
  22. namespace boost{
  23. namespace serialization {
  24. namespace stl {
  25. //////////////////////////////////////////////////////////////////////
  26. // implementation of serialization for STL containers
  27. //
  28. template<class Archive, class Container>
  29. inline void save_unordered_collection(Archive & ar, const Container &s)
  30. {
  31. collection_size_type count(s.size());
  32. const collection_size_type bucket_count(s.bucket_count());
  33. const item_version_type item_version(
  34. version<typename Container::value_type>::value
  35. );
  36. #if 0
  37. /* should only be necessary to create archives of previous versions
  38. * which is not currently supported. So for now comment this out
  39. */
  40. boost::archive::library_version_type library_version(
  41. ar.get_library_version()
  42. );
  43. // retrieve number of elements
  44. ar << BOOST_SERIALIZATION_NVP(count);
  45. ar << BOOST_SERIALIZATION_NVP(bucket_count);
  46. if(boost::archive::library_version_type(3) < library_version){
  47. // record number of elements
  48. // make sure the target type is registered so we can retrieve
  49. // the version when we load
  50. ar << BOOST_SERIALIZATION_NVP(item_version);
  51. }
  52. #else
  53. ar << BOOST_SERIALIZATION_NVP(count);
  54. ar << BOOST_SERIALIZATION_NVP(bucket_count);
  55. ar << BOOST_SERIALIZATION_NVP(item_version);
  56. #endif
  57. typename Container::const_iterator it = s.begin();
  58. while(count-- > 0){
  59. // note borland emits a no-op without the explicit namespace
  60. boost::serialization::save_construct_data_adl(
  61. ar,
  62. &(*it),
  63. boost::serialization::version<
  64. typename Container::value_type
  65. >::value
  66. );
  67. ar << boost::serialization::make_nvp("item", *it++);
  68. }
  69. }
  70. } // namespace stl
  71. } // namespace serialization
  72. } // namespace boost
  73. #endif //BOOST_SERIALIZATION_UNORDERED_COLLECTIONS_SAVE_IMP_HPP