ptree_serialization.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #ifndef BOOST_PROPERTY_TREE_PTREE_SERIALIZATION_HPP_INCLUDED
  11. #define BOOST_PROPERTY_TREE_PTREE_SERIALIZATION_HPP_INCLUDED
  12. #include <boost/property_tree/ptree.hpp>
  13. #include <boost/serialization/nvp.hpp>
  14. #include <boost/serialization/collections_save_imp.hpp>
  15. #include <boost/serialization/detail/stack_constructor.hpp>
  16. #include <boost/serialization/split_free.hpp>
  17. #include <boost/serialization/utility.hpp>
  18. namespace boost { namespace property_tree
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // boost::serialization support
  22. /**
  23. * Serialize the property tree to the given archive.
  24. * @note In addition to serializing to regular archives, this supports
  25. * serializing to archives requiring name-value pairs, e.g. XML
  26. * archives. However, the output format in the XML archive is not
  27. * guaranteed to be the same as that when using the Boost.PropertyTree
  28. * library's @c boost::property_tree::xml_parser::write_xml.
  29. * @param ar The archive to which to save the serialized property tree.
  30. * This archive should conform to the concept laid out by the
  31. * Boost.Serialization library.
  32. * @param t The property tree to serialize.
  33. * @param file_version file_version for the archive.
  34. * @post @c ar will contain the serialized form of @c t.
  35. */
  36. template<class Archive, class K, class D, class C>
  37. inline void save(Archive &ar,
  38. const basic_ptree<K, D, C> &t,
  39. const unsigned int file_version)
  40. {
  41. using namespace boost::serialization;
  42. stl::save_collection<Archive, basic_ptree<K, D, C> >(ar, t);
  43. ar << make_nvp("data", t.data());
  44. }
  45. namespace detail
  46. {
  47. template <class Archive, class K, class D, class C>
  48. inline void load_children(Archive &ar,
  49. basic_ptree<K, D, C> &t)
  50. {
  51. namespace bsl = boost::serialization;
  52. namespace bsa = boost::archive;
  53. typedef basic_ptree<K, D, C> tree;
  54. typedef typename tree::value_type value_type;
  55. bsl::collection_size_type count;
  56. ar >> BOOST_SERIALIZATION_NVP(count);
  57. bsl::item_version_type item_version(0);
  58. const bsa::library_version_type library_version(
  59. ar.get_library_version()
  60. );
  61. if(bsa::library_version_type(3) < library_version){
  62. ar >> BOOST_SERIALIZATION_NVP(item_version);
  63. }
  64. // Can't use the serialization helper, it expects resize() to exist
  65. // for default-constructible elements.
  66. // This is a copy/paste of the fallback version.
  67. t.clear();
  68. while(count-- > 0){
  69. bsl::detail::stack_construct<Archive, value_type>
  70. u(ar, item_version);
  71. ar >> bsl::make_nvp("item", u.reference());
  72. t.push_back(u.reference());
  73. ar.reset_object_address(& t.back() , & u.reference());
  74. }
  75. }
  76. }
  77. /**
  78. * De-serialize the property tree to the given archive.
  79. * @note In addition to de-serializing from regular archives, this supports
  80. * loading from archives requiring name-value pairs, e.g. XML
  81. * archives. The format should be that used by
  82. * boost::property_tree::save.
  83. * @param ar The archive from which to load the serialized property tree.
  84. * This archive should conform to the concept laid out by the
  85. * Boost.Serialization library.
  86. * @param t The property tree to de-serialize.
  87. * @param file_version file_version for the archive.
  88. * @post @c t will contain the de-serialized data from @c ar.
  89. */
  90. template<class Archive, class K, class D, class C>
  91. inline void load(Archive &ar,
  92. basic_ptree<K, D, C> &t,
  93. const unsigned int file_version)
  94. {
  95. namespace bsl = boost::serialization;
  96. detail::load_children(ar, t);
  97. ar >> bsl::make_nvp("data", t.data());
  98. }
  99. /**
  100. * Load or store the property tree using the given archive.
  101. * @param ar The archive from which to load or save the serialized property
  102. * tree. The type of this archive will determine whether saving or
  103. * loading is performed.
  104. * @param t The property tree to load or save.
  105. * @param file_version file_version for the archive.
  106. */
  107. template<class Archive, class K, class D, class C>
  108. inline void serialize(Archive &ar,
  109. basic_ptree<K, D, C> &t,
  110. const unsigned int file_version)
  111. {
  112. using namespace boost::serialization;
  113. split_free(ar, t, file_version);
  114. }
  115. } }
  116. #endif