diff options
Diffstat (limited to 'libs/property_tree/examples/debug_settings.cpp')
-rw-r--r-- | libs/property_tree/examples/debug_settings.cpp | 92 |
1 files changed, 39 insertions, 53 deletions
diff --git a/libs/property_tree/examples/debug_settings.cpp b/libs/property_tree/examples/debug_settings.cpp index 639155608..2684f4359 100644 --- a/libs/property_tree/examples/debug_settings.cpp +++ b/libs/property_tree/examples/debug_settings.cpp @@ -8,6 +8,7 @@ // For more information, see www.boost.org // ---------------------------------------------------------------------------- +//[debug_settings_includes #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <boost/foreach.hpp> @@ -15,7 +16,9 @@ #include <set> #include <exception> #include <iostream> - +namespace pt = boost::property_tree; +//] +//[debug_settings_data struct debug_settings { std::string m_file; // log filename @@ -24,80 +27,63 @@ struct debug_settings void load(const std::string &filename); void save(const std::string &filename); }; - +//] +//[debug_settings_load void debug_settings::load(const std::string &filename) { - // Create empty property tree object - using boost::property_tree::ptree; - ptree pt; + pt::ptree tree; - // Load XML file and put its contents in property tree. - // No namespace qualification is needed, because of Koenig - // lookup on the second argument. If reading fails, exception - // is thrown. - read_xml(filename, pt); + // Parse the XML into the property tree. + pt::read_xml(filename, tree); - // Get filename and store it in m_file variable. Note that - // we specify a path to the value using notation where keys - // are separated with dots (different separator may be used - // if keys themselves contain dots). If debug.filename key is - // not found, exception is thrown. - m_file = pt.get<std::string>("debug.filename"); - - // Get debug level and store it in m_level variable. This is - // another version of get method: if debug.level key is not - // found, it will return default value (specified by second - // parameter) instead of throwing. Type of the value extracted - // is determined by type of second parameter, so we can simply - // write get(...) instead of get<int>(...). - m_level = pt.get("debug.level", 0); + // Use the throwing version of get to find the debug filename. + // If the path cannot be resolved, an exception is thrown. + m_file = tree.get<std::string>("debug.filename"); - // Iterate over debug.modules section and store all found - // modules in m_modules set. get_child() function returns a - // reference to child at specified path; if there is no such - // child, it throws. Property tree iterator can be used in - // the same way as standard container iterator. Category - // is bidirectional_iterator. - BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.modules")) + // Use the default-value version of get to find the debug level. + // Note that the default value is used to deduce the target type. + m_level = tree.get("debug.level", 0); + + // Use get_child to find the node containing the modules, and iterate over + // its children. If the path cannot be resolved, get_child throws. + // A C++11 for-range loop would also work. + BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("debug.modules")) { + // The data function is used to access the data stored in a node. m_modules.insert(v.second.data()); + } } - +//] +//[debug_settings_save void debug_settings::save(const std::string &filename) { + // Create an empty property tree object. + pt::ptree tree; - // Create empty property tree object - using boost::property_tree::ptree; - ptree pt; - - // Put log filename in property tree - pt.put("debug.filename", m_file); - - // Put debug level in property tree - pt.put("debug.level", m_level); + // Put the simple values into the tree. The integer is automatically + // converted to a string. Note that the "debug" node is automatically + // created if it doesn't exist. + tree.put("debug.filename", m_file); + tree.put("debug.level", m_level); - // Iterate over modules in set and put them in property - // tree. Note that the add function places new key at the - // end of list of keys. This is fine in most of the - // situations. If you want to place item at some other - // place (i.e. at front or somewhere in the middle), - // this can be achieved using a combination of the insert - // and put_value functions + // Add all the modules. Unlike put, which overwrites existing nodes, add + // adds a new node at the lowest level, so the "modules" node will have + // multiple "module" children. BOOST_FOREACH(const std::string &name, m_modules) - pt.add("debug.modules.module", name); - - // Write property tree to XML file - write_xml(filename, pt); + tree.add("debug.modules.module", name); + // Write property tree to XML file + pt::write_xml(filename, tree); } +//] int main() { try { debug_settings ds; - ds.load("debug_settings.xml"); + ds.load("debug_settings_xml"); ds.save("debug_settings_out.xml"); std::cout << "Success\n"; } |