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