]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/property_tree/examples/custom_data_type.cpp
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
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)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
11 // This example shows what need to be done to customize data_type of ptree.
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).
16 #include <boost/property_tree/ptree.hpp>
17 #include <boost/any.hpp>
22 // Custom translator that works with boost::any instead of std::string
23 template <class Ext
, class Int
= boost::any
>
24 struct variant_translator
26 typedef Ext external_type
;
27 typedef Int internal_type
;
30 get_value(const internal_type
&value
) const
32 return boost::any_cast
<external_type
>(value
);
35 put_value(const external_type
&value
) const
44 using namespace boost::property_tree
;
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
;
54 typedef variant_translator
<int> int_tran
;
55 pt
.put("int value", 3, int_tran());
56 int int_value
= pt
.get
<int>("int value", int_tran());
57 std::cout
<< "Int value: " << int_value
<< "\n";
59 // Put/get string value
60 typedef variant_translator
<std::string
> string_tran
;
61 pt
.put
<std::string
>("string value", "foo bar", string_tran());
62 std::string string_value
= pt
.get
<std::string
>(
66 std::cout
<< "String value: " << string_value
<< "\n";
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 };
76 , list_data
+ sizeof(list_data
) / sizeof(*list_data
)
80 intlist list_value
= pt
.get
<intlist
>(
84 std::cout
<< "List value: ";
85 for (intlist::iterator it
= list_value
.begin(); it
!= list_value
.end(); ++it
)
86 std::cout
<< *it
<< ' ';