Boost.PropertyTree 五分钟快速入门

时间:2022-09-09 07:40:02

本教程使用XML。注意,这个库不是专门绑定到XML的,而是可以使用任何其他支持的格式(如INI或JSON)。之所以选择XML,是因为作者认为很多人都熟悉XML。

假设我们正在为某个应用程序编写一个日志系统,并且需要在程序启动时从文件中读取日志配置。带有日志配置的文件如下所示:

  1. <debug>
  2. <filename>debug.log</filename>
  3. <modules>
  4. <module>Finance</module>
  5. <module>Admin</module>
  6. <module>HR</module>
  7. </modules>
  8. <level>2</level>
  9. </debug>
它包含日志文件名、启用日志记录的模块列表和调试级别值。
我们需要包含的头文件:
  1. #include<boost/property_tree/ptree.hpp>
  2. #include<boost/property_tree/xml_parser.hpp>
  3. #include<boost/foreach.hpp>
  4. #include<string>
  5. #include<set>
  6. #include<exception>
  7. #include<iostream>
  8. namespace pt = boost::property_tree;
为了存储日志的配置信息,我们创建一个结构来存储它
  1. struct debug_settings
  2. {
  3. std::string m_file;// log filename
  4. int m_level;// debug level
  5. std::set<std::string> m_modules;// modules where logging is enabled
  6. void load(const std::string &filename);
  7. void save(const std::string &filename);
  8. };
现在需要做的就是编写load()和save()成员函数的实现。让我们先来处理load()。它只包含7行代码,尽管它做了所有必要的事情,包括错误报告:
  1. void debug_settings::load(const std::string &filename)
  2. {
  3. // Create empty property tree object
  4. pt::ptree tree;
  5. // Parse the XML into the property tree.
  6. pt::read_xml(filename, tree);
  7. // Use the throwing version of get to find the debug filename.
  8. // If the path cannot be resolved, an exception is thrown.
  9. m_file = tree.get<std::string>("debug.filename");
  10. // Use the default-value version of get to find the debug level.
  11. // Note that the default value is used to deduce the target type.
  12. m_level = tree.get("debug.level",0);
  13. // Use get_child to find the node containing the modules, and iterate over
  14. // its children. If the path cannot be resolved, get_child throws.
  15. // A C++11 for-range loop would also work.
  16. BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("debug.modules")){
  17. // The data function is used to access the data stored in a node.
  18. m_modules.insert(v.second.data());
  19. }
  20. }
存储函数
  1. void debug_settings::save(const std::string &filename)
  2. {
  3. // Create an empty property tree object.
  4. pt::ptree tree;
  5. // Put the simple values into the tree. The integer is automatically
  6. // converted to a string. Note that the "debug" node is automatically
  7. // created if it doesn't exist.
  8. tree.put("debug.filename", m_file);
  9. tree.put("debug.level", m_level);
  10. // Add all the modules. Unlike put, which overwrites existing nodes, add
  11. // adds a new node at the lowest level, so the "modules" node will have
  12. // multiple "module" children.
  13. BOOST_FOREACH(const std::string &name, m_modules)
  14. tree.add("debug.modules.module", name);
  15. // Write property tree to XML file
  16. pt::write_xml(filename, tree);
  17. }
 
来个全代码的:
  1. //
  2. // Created by 瓜不甜 on 2018/1/20.
  3. //
  4. #include<boost/property_tree/ptree.hpp>
  5. #include<boost/property_tree/xml_parser.hpp>
  6. #include<boost/foreach.hpp>
  7. #include<string>
  8. #include<set>
  9. #include<exception>
  10. #include<iostream>
  11. namespace pt = boost::property_tree;
  12. struct debug_settings
  13. {
  14. std::string m_file;// log filename
  15. int m_level;// debug level
  16. std::set<std::string> m_modules;// modules where logging is enabled
  17. void load(const std::string &filename);
  18. void save(const std::string &filename);
  19. };
  20. void debug_settings::load(const std::string &filename)
  21. {
  22. // Create empty property tree object
  23. pt::ptree tree;
  24. // Parse the XML into the property tree.
  25. pt::read_xml(filename, tree);
  26. // Use the throwing version of get to find the debug filename.
  27. // If the path cannot be resolved, an exception is thrown.
  28. m_file = tree.get<std::string>("debug.filename");
  29. // Use the default-value version of get to find the debug level.
  30. // Note that the default value is used to deduce the target type.
  31. m_level = tree.get("debug.level",0);
  32. // Use get_child to find the node containing the modules, and iterate over
  33. // its children. If the path cannot be resolved, get_child throws.
  34. // A C++11 for-range loop would also work.
  35. BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("debug.modules")){
  36. // The data function is used to access the data stored in a node.
  37. m_modules.insert(v.second.data());
  38. }
  39. }
  40. void debug_settings::save(const std::string &filename)
  41. {
  42. // Create an empty property tree object.
  43. pt::ptree tree;
  44. // Put the simple values into the tree. The integer is automatically
  45. // converted to a string. Note that the "debug" node is automatically
  46. // created if it doesn't exist.
  47. tree.put("debug.filename", m_file);
  48. tree.put("debug.level", m_level);
  49. // Add all the modules. Unlike put, which overwrites existing nodes, add
  50. // adds a new node at the lowest level, so the "modules" node will have
  51. // multiple "module" children.
  52. BOOST_FOREACH(const std::string &name, m_modules)
  53. tree.add("debug.modules.module", name);
  54. // Write property tree to XML file
  55. pt::write_xml(filename, tree);
  56. }
  57. void test1(){
  58. debug_settings a;
  59. a.load("setting.xml");
  60. std::cout<<a.m_file<<std::endl;
  61. std::cout<<a.m_level<<std::endl;
  62. std::set<std::string>::iterator i;
  63. for( i = a.m_modules.begin(); i != a.m_modules.end();++i){
  64. std::cout<<*i<<std::endl;
  65. }
  66. }
Boost.PropertyTree 五分钟快速入门
很明显,这个是个非常好用的结构!
简单,高效!
这里有个foreach结构:
BOOST_FOREACH(const std::string &name, m_modules)
{
       tree.add("debug.modules.module", name);
}

这个是C++的一个类宏的高级应用了