Brian Silverman | 836e90c | 2018-08-04 16:19:46 -0700 | [diff] [blame^] | 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 | |
| 11 | This tutorial uses XML. Note that the library is not specifically bound to XML, |
| 12 | and any other supported format (such as INI or JSON) could be used instead. |
| 13 | XML was chosen because the author thinks that a wide range of people is familiar |
| 14 | with it. |
| 15 | |
| 16 | Suppose we are writing a logging system for some application, and need to read |
| 17 | log configuration from a file when the program starts. The file with the log |
| 18 | configuration looks like this: |
| 19 | |
| 20 | [pre |
| 21 | <debug> |
| 22 | <filename>debug.log</filename> |
| 23 | <modules> |
| 24 | <module>Finance</module> |
| 25 | <module>Admin</module> |
| 26 | <module>HR</module> |
| 27 | </modules> |
| 28 | <level>2</level> |
| 29 | </debug> |
| 30 | ] |
| 31 | |
| 32 | It contains the log filename, a list of modules where logging is enabled, and |
| 33 | the debug level value. |
| 34 | |
| 35 | First we need some includes: |
| 36 | |
| 37 | [debug_settings_includes] |
| 38 | |
| 39 | To store the logging configuration in the program we create a debug_settings |
| 40 | structure: |
| 41 | |
| 42 | [debug_settings_data] |
| 43 | |
| 44 | All that needs to be done now is to write implementations of load() and save() |
| 45 | member functions. Let's first deal with load(). It contains just 7 lines of |
| 46 | code, although it does all the necessary things, including error reporting: |
| 47 | |
| 48 | [debug_settings_load] |
| 49 | |
| 50 | Now the save() function. It is also 7 lines of code: |
| 51 | |
| 52 | [debug_settings_save] |
| 53 | |
| 54 | The full program [@boost:/libs/property_tree/examples/debug_settings.cpp debug_settings.cpp] is |
| 55 | included in the examples directory. |
| 56 | [endsect] [/tutorial] |