|
|
От: |
Ops
|
|
| Дата: | 01.04.13 05:04 | ||
| Оценка: | +1 | ||
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_propertytree/accessing.htmlThree Ways of Getting Data
There are three versions of get: get, get (default-value version), and get_optional, which differ by failure handling strategy. All versions take path specifier, which determines in which key to search for a value. It can be a single key, or a path to key, where path elements are separated with a special character (a '.' if not specified differently). For example debug.logging.errorlevel might be a valid path with dot as a separator.
The throwing version (get):
ptree pt;
/* ... */
float v = pt.get<float>("a.path.to.float.value");
This call locates the proper node in the tree and tries to translate its data string to a float value. If that fails, exception is thrown. If path does not exist, it will be ptree_bad_path exception. If value could not be translated, it will be ptree_bad_data. Both of them derive from ptree_error to make common handling possible.
The default-value version (get):
ptree pt;
/* ... */
float v = pt.get("a.path.to.float.value", -1.f);
It will do the same as above, but if it fails, it will return the default value specified by second parameter (here -1.f) instead of throwing. This is very useful in common situations where one wants to allow omitting of some keys. Note that type specification needed in throwing version is normally not necessary here, because type is determined by the default value parameter.
The optional version (get_optional):
ptree pt;
/* ... */
boost::optional<float> v = pt.get_optional<float>("a.path.to.float.value");
This version uses boost::optional class to handle extraction failure. On successful extraction, it will return boost::optional initialized with extracted value. Otherwise, it will return uninitialized boost::optional.