ptree_serialization.hpp 5.2 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. #include <boost/serialization/library_version_type.hpp>
  19. namespace boost { namespace property_tree
  20. {
  21. ///////////////////////////////////////////////////////////////////////////
  22. // boost::serialization support
  23. /**
  24. * Serialize the property tree to the given archive.
  25. * @note In addition to serializing to regular archives, this supports
  26. * serializing to archives requiring name-value pairs, e.g. XML
  27. * archives. However, the output format in the XML archive is not
  28. * guaranteed to be the same as that when using the Boost.PropertyTree
  29. * library's @c boost::property_tree::xml_parser::write_xml.
  30. * @param ar The archive to which to save the serialized property tree.
  31. * This archive should conform to the concept laid out by the
  32. * Boost.Serialization library.
  33. * @param t The property tree to serialize.
  34. * @param file_version file_version for the archive.
  35. * @post @c ar will contain the serialized form of @c t.
  36. */
  37. template<class Archive, class K, class D, class C>
  38. inline void save(Archive &ar,
  39. const basic_ptree<K, D, C> &t,
  40. const unsigned int /*file_version*/)
  41. {
  42. using namespace boost::serialization;
  43. stl::save_collection<Archive, basic_ptree<K, D, C> >(ar, t);
  44. ar << make_nvp("data", t.data());
  45. }
  46. namespace detail
  47. {
  48. template <class Archive, class K, class D, class C>
  49. inline void load_children(Archive &ar,
  50. basic_ptree<K, D, C> &t)
  51. {
  52. namespace bsl = boost::serialization;
  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 bsl::library_version_type library_version(
  59. ar.get_library_version()
  60. );
  61. if(bsl::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