collections_save_imp.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef BOOST_SERIALIZATION_COLLECTIONS_SAVE_IMP_HPP
  2. #define BOOST_SERIALIZATION_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. // 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. namespace boost{
  21. namespace serialization {
  22. namespace stl {
  23. //////////////////////////////////////////////////////////////////////
  24. // implementation of serialization for STL containers
  25. //
  26. template<class Archive, class Container>
  27. inline void save_collection(Archive & ar, const Container &s)
  28. {
  29. // record number of elements
  30. collection_size_type const count(s.size());
  31. ar << BOOST_SERIALIZATION_NVP(count);
  32. // make sure the target type is registered so we can retrieve
  33. // the version when we load
  34. if(3 < ar.get_library_version()){
  35. const unsigned int item_version = version<
  36. BOOST_DEDUCED_TYPENAME Container::value_type
  37. >::value;
  38. ar << BOOST_SERIALIZATION_NVP(item_version);
  39. }
  40. BOOST_DEDUCED_TYPENAME Container::const_iterator it = s.begin();
  41. std::size_t c=count;
  42. while(c-- > 0){
  43. // note borland emits a no-op without the explicit namespace
  44. boost::serialization::save_construct_data_adl(
  45. ar,
  46. &(*it),
  47. boost::serialization::version<
  48. BOOST_DEDUCED_TYPENAME Container::value_type
  49. >::value
  50. );
  51. ar << boost::serialization::make_nvp("item", *it++);
  52. }
  53. }
  54. } // namespace stl
  55. } // namespace serialization
  56. } // namespace boost
  57. #endif //BOOST_SERIALIZATION_COLLECTIONS_SAVE_IMP_HPP