]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/property_tree/examples/empty_ptree_trick.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 #include <boost/property_tree/ptree.hpp>
12 #include <boost/property_tree/info_parser.hpp>
17 using namespace boost::property_tree
;
19 // Process settings using empty ptree trick. Note that it is considerably simpler
20 // than version which does not use the "trick"
21 void process_settings(const std::string
&filename
)
24 read_info(filename
, pt
);
25 const ptree
&settings
= pt
.get_child("settings", empty_ptree
<ptree
>());
26 std::cout
<< "\n Processing " << filename
<< std::endl
;
27 std::cout
<< " Setting 1 is " << settings
.get("setting1", 0) << std::endl
;
28 std::cout
<< " Setting 2 is " << settings
.get("setting2", 0.0) << std::endl
;
29 std::cout
<< " Setting 3 is " << settings
.get("setting3", "default") << std::endl
;
32 // Process settings not using empty ptree trick. This one must duplicate much of the code.
33 void process_settings_without_trick(const std::string
&filename
)
36 read_info(filename
, pt
);
37 if (boost::optional
<ptree
&> settings
= pt
.get_child_optional("settings"))
39 std::cout
<< "\n Processing " << filename
<< std::endl
;
40 std::cout
<< " Setting 1 is " << settings
.get().get("setting1", 0) << std::endl
;
41 std::cout
<< " Setting 2 is " << settings
.get().get("setting2", 0.0) << std::endl
;
42 std::cout
<< " Setting 3 is " << settings
.get().get("setting3", "default") << std::endl
;
46 std::cout
<< "\n Processing " << filename
<< std::endl
;
47 std::cout
<< " Setting 1 is " << 0 << std::endl
;
48 std::cout
<< " Setting 2 is " << 0.0 << std::endl
;
49 std::cout
<< " Setting 3 is " << "default" << std::endl
;
57 std::cout
<< "Processing settings with empty-ptree-trick:\n";
58 process_settings("settings_fully-existent.info");
59 process_settings("settings_partially-existent.info");
60 process_settings("settings_non-existent.info");
61 std::cout
<< "\nProcessing settings without empty-ptree-trick:\n";
62 process_settings_without_trick("settings_fully-existent.info");
63 process_settings_without_trick("settings_partially-existent.info");
64 process_settings_without_trick("settings_non-existent.info");
66 catch (std::exception
&e
)
68 std::cout
<< "Error: " << e
.what() << "\n";