ptree_serialization.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/collections_load_imp.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. /**
  46. * De-serialize the property tree to the given archive.
  47. * @note In addition to de-serializing from regular archives, this supports
  48. * loading from archives requiring name-value pairs, e.g. XML
  49. * archives. The format should be that used by
  50. * boost::property_tree::save.
  51. * @param ar The archive from which to load the serialized property tree.
  52. * This archive should conform to the concept laid out by the
  53. * Boost.Serialization library.
  54. * @param t The property tree to de-serialize.
  55. * @param file_version file_version for the archive.
  56. * @post @c t will contain the de-serialized data from @c ar.
  57. */
  58. template<class Archive, class K, class D, class C>
  59. inline void load(Archive &ar,
  60. basic_ptree<K, D, C> &t,
  61. const unsigned int file_version)
  62. {
  63. using namespace boost::serialization;
  64. // Load children
  65. stl::load_collection<Archive,
  66. basic_ptree<K, D, C>,
  67. stl::archive_input_seq<Archive,
  68. basic_ptree<K, D, C> >,
  69. stl::no_reserve_imp<
  70. basic_ptree<K, D, C> >
  71. >(ar, t);
  72. // Load data (must be after load_collection, as it calls clear())
  73. ar >> make_nvp("data", t.data());
  74. }
  75. /**
  76. * Load or store the property tree using the given archive.
  77. * @param ar The archive from which to load or save the serialized property
  78. * tree. The type of this archive will determine whether saving or
  79. * loading is performed.
  80. * @param t The property tree to load or save.
  81. * @param file_version file_version for the archive.
  82. */
  83. template<class Archive, class K, class D, class C>
  84. inline void serialize(Archive &ar,
  85. basic_ptree<K, D, C> &t,
  86. const unsigned int file_version)
  87. {
  88. using namespace boost::serialization;
  89. split_free(ar, t, file_version);
  90. }
  91. } }
  92. #endif