hash_collections_save_imp.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef BOOST_SERIALIZATION_HASH_COLLECTIONS_SAVE_IMP_HPP
  2. #define BOOST_SERIALIZATION_HASH_COLLECTIONS_SAVE_IMP_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. // hash_collections_save_imp.hpp: serialization for stl collections
  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. // helper function templates for serialization of collections
  15. #include <boost/config.hpp>
  16. #include <boost/serialization/nvp.hpp>
  17. #include <boost/serialization/serialization.hpp>
  18. #include <boost/serialization/version.hpp>
  19. #include <boost/serialization/collection_size_type.hpp>
  20. #include <boost/serialization/item_version_type.hpp>
  21. namespace boost{
  22. namespace serialization {
  23. namespace stl {
  24. //////////////////////////////////////////////////////////////////////
  25. // implementation of serialization for STL containers
  26. //
  27. template<class Archive, class Container>
  28. inline void save_hash_collection(Archive & ar, const Container &s)
  29. {
  30. collection_size_type count(s.size());
  31. const collection_size_type bucket_count(s.bucket_count());
  32. const item_version_type item_version(
  33. version<typename Container::value_type>::value
  34. );
  35. #if 0
  36. /* should only be necessary to create archives of previous versions
  37. * which is not currently supported. So for now comment this out
  38. */
  39. boost::archive::library_version_type library_version(
  40. ar.get_library_version()
  41. );
  42. // retrieve number of elements
  43. if(boost::archive::library_version_type(6) != library_version){
  44. ar << BOOST_SERIALIZATION_NVP(count);
  45. ar << BOOST_SERIALIZATION_NVP(bucket_count);
  46. }
  47. else{
  48. // note: fixup for error in version 6. collection size was
  49. // changed to size_t BUT for hashed collections it was implemented
  50. // as an unsigned int. This should be a problem only on win64 machines
  51. // but I'll leave it for everyone just in case.
  52. const unsigned int c = count;
  53. const unsigned int bc = bucket_count;
  54. ar << BOOST_SERIALIZATION_NVP(c);
  55. ar << BOOST_SERIALIZATION_NVP(bc);
  56. }
  57. if(boost::archive::library_version_type(3) < library_version){
  58. // record number of elements
  59. // make sure the target type is registered so we can retrieve
  60. // the version when we load
  61. ar << BOOST_SERIALIZATION_NVP(item_version);
  62. }
  63. #else
  64. ar << BOOST_SERIALIZATION_NVP(count);
  65. ar << BOOST_SERIALIZATION_NVP(bucket_count);
  66. ar << BOOST_SERIALIZATION_NVP(item_version);
  67. #endif
  68. typename Container::const_iterator it = s.begin();
  69. while(count-- > 0){
  70. // note borland emits a no-op without the explicit namespace
  71. boost::serialization::save_construct_data_adl(
  72. ar,
  73. &(*it),
  74. boost::serialization::version<
  75. typename Container::value_type
  76. >::value
  77. );
  78. ar << boost::serialization::make_nvp("item", *it++);
  79. }
  80. }
  81. } // namespace stl
  82. } // namespace serialization
  83. } // namespace boost
  84. #endif //BOOST_SERIALIZATION_HASH_COLLECTIONS_SAVE_IMP_HPP