custom_data_type.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // This example shows what need to be done to customize data_type of ptree.
  11. //
  12. // It creates my_ptree type, which is a basic_ptree having boost::any as its data
  13. // container (instead of std::string that standard ptree has).
  14. #include <boost/property_tree/ptree.hpp>
  15. #include <boost/any.hpp>
  16. #include <list>
  17. #include <string>
  18. #include <iostream>
  19. // Custom translator that works with boost::any instead of std::string
  20. template <class Ext, class Int = boost::any>
  21. struct variant_translator
  22. {
  23. typedef Ext external_type;
  24. typedef Int internal_type;
  25. external_type
  26. get_value(const internal_type &value) const
  27. {
  28. return boost::any_cast<external_type>(value);
  29. }
  30. internal_type
  31. put_value(const external_type &value) const
  32. {
  33. return value;
  34. }
  35. };
  36. int main()
  37. {
  38. using namespace boost::property_tree;
  39. // Property_tree with boost::any as data type
  40. // Key type: std::string
  41. // Data type: boost::any
  42. // Key comparison: default (std::less<std::string>)
  43. typedef basic_ptree<std::string, boost::any> my_ptree;
  44. my_ptree pt;
  45. // Put/get int value
  46. typedef variant_translator<int> int_tran;
  47. pt.put("int value", 3, int_tran());
  48. int int_value = pt.get<int>("int value", int_tran());
  49. std::cout << "Int value: " << int_value << "\n";
  50. // Put/get string value
  51. typedef variant_translator<std::string> string_tran;
  52. pt.put<std::string>("string value", "foo bar", string_tran());
  53. std::string string_value = pt.get<std::string>(
  54. "string value"
  55. , string_tran()
  56. );
  57. std::cout << "String value: " << string_value << "\n";
  58. // Put/get list<int> value
  59. typedef std::list<int> intlist;
  60. typedef variant_translator<intlist> intlist_tran;
  61. int list_data[] = { 1, 2, 3, 4, 5 };
  62. pt.put<intlist>(
  63. "list value"
  64. , intlist(
  65. list_data
  66. , list_data + sizeof(list_data) / sizeof(*list_data)
  67. )
  68. , intlist_tran()
  69. );
  70. intlist list_value = pt.get<intlist>(
  71. "list value"
  72. , intlist_tran()
  73. );
  74. std::cout << "List value: ";
  75. for (intlist::iterator it = list_value.begin(); it != list_value.end(); ++it)
  76. std::cout << *it << ' ';
  77. std::cout << '\n';
  78. }