empty_ptree_trick.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <boost/property_tree/ptree.hpp>
  11. #include <boost/property_tree/info_parser.hpp>
  12. #include <iostream>
  13. #include <iomanip>
  14. #include <string>
  15. using namespace boost::property_tree;
  16. // Process settings using empty ptree trick. Note that it is considerably simpler
  17. // than version which does not use the "trick"
  18. void process_settings(const std::string &filename)
  19. {
  20. ptree pt;
  21. read_info(filename, pt);
  22. const ptree &settings = pt.get_child("settings", empty_ptree<ptree>());
  23. std::cout << "\n Processing " << filename << std::endl;
  24. std::cout << " Setting 1 is " << settings.get("setting1", 0) << std::endl;
  25. std::cout << " Setting 2 is " << settings.get("setting2", 0.0) << std::endl;
  26. std::cout << " Setting 3 is " << settings.get("setting3", "default") << std::endl;
  27. }
  28. // Process settings not using empty ptree trick. This one must duplicate much of the code.
  29. void process_settings_without_trick(const std::string &filename)
  30. {
  31. ptree pt;
  32. read_info(filename, pt);
  33. if (boost::optional<ptree &> settings = pt.get_child_optional("settings"))
  34. {
  35. std::cout << "\n Processing " << filename << std::endl;
  36. std::cout << " Setting 1 is " << settings.get().get("setting1", 0) << std::endl;
  37. std::cout << " Setting 2 is " << settings.get().get("setting2", 0.0) << std::endl;
  38. std::cout << " Setting 3 is " << settings.get().get("setting3", "default") << std::endl;
  39. }
  40. else
  41. {
  42. std::cout << "\n Processing " << filename << std::endl;
  43. std::cout << " Setting 1 is " << 0 << std::endl;
  44. std::cout << " Setting 2 is " << 0.0 << std::endl;
  45. std::cout << " Setting 3 is " << "default" << std::endl;
  46. }
  47. }
  48. int main()
  49. {
  50. try
  51. {
  52. std::cout << "Processing settings with empty-ptree-trick:\n";
  53. process_settings("settings_fully-existent.info");
  54. process_settings("settings_partially-existent.info");
  55. process_settings("settings_non-existent.info");
  56. std::cout << "\nProcessing settings without empty-ptree-trick:\n";
  57. process_settings_without_trick("settings_fully-existent.info");
  58. process_settings_without_trick("settings_partially-existent.info");
  59. process_settings_without_trick("settings_non-existent.info");
  60. }
  61. catch (std::exception &e)
  62. {
  63. std::cout << "Error: " << e.what() << "\n";
  64. }
  65. return 0;
  66. }