File: empty_ptree_trick.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (73 lines) | stat: -rw-r--r-- 2,879 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ----------------------------------------------------------------------------
// Copyright (C) 2002-2006 Marcin Kalicinski
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org
// ----------------------------------------------------------------------------

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <iostream>
#include <iomanip>
#include <string>

using namespace boost::property_tree;

  static ptree dflt;

    // Process settings using empty ptree trick. Note that it is considerably simpler
// than version which does not use the "trick"
void process_settings(const std::string &filename)
{
    ptree pt;
    read_info(filename, pt);
    const ptree &settings = pt.get_child("settings", dflt);
    std::cout << "\n    Processing " << filename << std::endl;
    std::cout << "        Setting 1 is " << settings.get("setting1", 0) << std::endl;
    std::cout << "        Setting 2 is " << settings.get("setting2", 0.0) << std::endl;
    std::cout << "        Setting 3 is " << settings.get("setting3", "default") << std::endl;
}

// Process settings not using empty ptree trick. This one must duplicate much of the code.
void process_settings_without_trick(const std::string &filename)
{
    ptree pt;
    read_info(filename, pt);    
    if (boost::optional<ptree &> settings = pt.get_child_optional("settings"))
    {
        std::cout << "\n    Processing " << filename << std::endl;
        std::cout << "        Setting 1 is " << settings.get().get("setting1", 0) << std::endl;
        std::cout << "        Setting 2 is " << settings.get().get("setting2", 0.0) << std::endl;
        std::cout << "        Setting 3 is " << settings.get().get("setting3", "default") << std::endl;
    }
    else
    {
        std::cout << "\n    Processing " << filename << std::endl;
        std::cout << "        Setting 1 is " << 0 << std::endl;
        std::cout << "        Setting 2 is " << 0.0 << std::endl;
        std::cout << "        Setting 3 is " << "default" << std::endl;
    }
}

int main()
{
    try
    {
        std::cout << "Processing settings with empty-ptree-trick:\n";
        process_settings("settings_fully-existent.info");
        process_settings("settings_partially-existent.info");
        process_settings("settings_non-existent.info");
        std::cout << "\nProcessing settings without empty-ptree-trick:\n";
        process_settings_without_trick("settings_fully-existent.info");
        process_settings_without_trick("settings_partially-existent.info");
        process_settings_without_trick("settings_non-existent.info");
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << "\n";
    }
    return 0;
}