tutorial.qbk 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. [/
  2. / Copyright (c) 2008 Marcin Kalicinski (kalita <at> poczta dot onet dot pl)
  3. / Copyright (c) 2009, 2013 Sebastian Redl (sebastian dot redl <at> getdesigned dot at)
  4. /
  5. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. /]
  8. [section:tutorial Five Minute Tutorial]
  9. [import ../examples/debug_settings.cpp]
  10. This tutorial uses XML. Note that the library is not specifically bound to XML,
  11. and any other supported format (such as INI or JSON) could be used instead.
  12. XML was chosen because the author thinks that a wide range of people is familiar
  13. with it.
  14. Suppose we are writing a logging system for some application, and need to read
  15. log configuration from a file when the program starts. The file with the log
  16. configuration looks like this:
  17. [pre
  18. <debug>
  19. <filename>debug.log</filename>
  20. <modules>
  21. <module>Finance</module>
  22. <module>Admin</module>
  23. <module>HR</module>
  24. </modules>
  25. <level>2</level>
  26. </debug>
  27. ]
  28. It contains the log filename, a list of modules where logging is enabled, and
  29. the debug level value.
  30. First we need some includes:
  31. [debug_settings_includes]
  32. To store the logging configuration in the program we create a debug_settings
  33. structure:
  34. [debug_settings_data]
  35. All that needs to be done now is to write implementations of load() and save()
  36. member functions. Let's first deal with load(). It contains just 7 lines of
  37. code, although it does all the necessary things, including error reporting:
  38. [debug_settings_load]
  39. Now the save() function. It is also 7 lines of code:
  40. [debug_settings_save]
  41. The full program [@boost:/libs/property_tree/examples/debug_settings.cpp debug_settings.cpp] is
  42. included in the examples directory.
  43. [endsect] [/tutorial]