]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/property_tree/examples/empty_ptree_trick.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / property_tree / examples / empty_ptree_trick.cpp
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 #include <boost/property_tree/ptree.hpp>
12 #include <boost/property_tree/info_parser.hpp>
13 #include <iostream>
14 #include <iomanip>
15 #include <string>
16
17 using namespace boost::property_tree;
18
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)
22 {
23 ptree pt;
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;
30 }
31
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)
34 {
35 ptree pt;
36 read_info(filename, pt);
37 if (boost::optional<ptree &> settings = pt.get_child_optional("settings"))
38 {
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;
43 }
44 else
45 {
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;
50 }
51 }
52
53 int main()
54 {
55 try
56 {
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");
65 }
66 catch (std::exception &e)
67 {
68 std::cout << "Error: " << e.what() << "\n";
69 }
70 return 0;
71 }