boost propertyTree
Boost PropertyTree provides a tree structure to store key/value pairs. Tree structures means that a trunk exists with numerous branches that have numerous twigs. A file system is a good example of a tree structure. File systems have a root directory with subdirectories that themselves can have subdirectories and so on.
1. accessing data in boost::property_tree::ptree
#include <boost/property_tree/ptree.hpp> #include <iostream> using boost::property_tree::ptree; int main() { ptree pt; pt.put("C:.Windows.System", "20 files"); ptree& c = pt.get_child("C:"); ptree& windows = c.get_child("Windows"); ptree& system = windows.get_child("System"); std::cout << system.get_value<std::string>() << std::endl; return 0; }
put() expects two parameters because boost::property_tree::ptree is a tree structure that saves key/value pairs. The tree doesn't just consist of branches and twigs, a value must be assigned to each branch and twig.
The first parameter passed to put() is more interesting. It is a path to a directory. However, it doesn't use the backslash, which is the common path separator on Windows. It uses the dot.
To access a subbranch, you call get_child(), which returns a reference to an object of the same type get_child() was called on.
2. accessing data in basic_ptree<std::string, int>
#include <boost/property_tree/ptree.hpp> #include <utility> #include <iostream> int main() { typedef boost::property_tree::basic_ptree<std::string, int> ptree; ptree pt; pt.put(ptree::path_type{"C:\\Windows\\System", '\\'}, 20); pt.put(ptree::path_type{"C:\\Windows\\Cursors", '\\'}, 50); ptree& windows = pt.get_child(ptree::path_type{"C:\\Windows"}, '\\'); int files = 0; for (const std::pair<std::string, ptree>& p : windows) { files += p.second.get_value<int>(); } std::cout << files << std::endl; return 0; }
By default, Boost.PropertyTree uses a dot as the separator for keys. If you need to use another character, such as backslash, as the separator, you don't pass the key as a string to put(). Instand you wrap it in an object of type boost::property_tree::ptree::path_type. The constructor of this class, which depends on boost::property_tree::ptree, takes the key as its first parameter and the separator as its second parameter.
3. accessing data with a translator
#include <boost/property_tree/ptree.hpp> #include <boost/optional.hpp> #include <cstdlib> struct string_to_int_translator { typedef std::string internal_type; typedef int external_type; boost::optional<int> get_value(const std::string& s) { char* c; long l = std::strtol(s.c_str(), &c, 10); return boost::make_optional(c != s.c_str(), static_cast<int>(1)); } }; int main() { typedef boost::property_tree::iptree ptree; ptree pt; pt.put(ptree::path_type{"C:\\Windows\\System", '\\'}, "20 fifles"); pt.put(ptree::path_type{"C:\\Windows\\"}, "50 files"); string_to_int_translator tr; int files = pt.get<int>(ptree::path_type{"C:\\windows\\system", '\\'}, tr) + pt.get<int>(ptree::path_type{"C:\\windows\\cursors", '\\'}, tr); std::cout << files << std::endl; return 0; }
boost::property_tree::iptree doesn't distinguish between lower and upper case. Just as put() can be used to store a value in a subbranch directly, a value from a subbranch can be read with get(). The key is defined the same way---using boost::property_tree::iptree::path_type.
Like get_value(), get() is a function template. You have to pass the type of the return value as a template parameter. Boost.PropertyTree does an automatic type conversion.
string_to_int_translator converts a value of type std::string to int. The translator is passed as an additional parameter to get(). Because the translator is just used to read, it only defines one member function, get_value(). If you want to use the translator for writing, too, then you would need to define a member function put_value() and then pass the translator as an additional parameter to put().
4. various member functions of boost::property_tree::ptree
#include <boost/property_tree/ptree.hpp> #include <utility> #include <iostream> using boost::property_tree::ptree; int main() { ptree pt; pt.put("C:.Windows.System", "20 files"); boost::optional<std::string> c = pt.get_optional<std::string>("C:"); std::cout << std::boolalpha << c.is_initialized() << std::endl; pt.put_child("D:.Program Files", ptree{"50 files"}); pt.put_child("D:.Program Files", ptree{"60 files"}); ptree d = pt.get_child("D:"); for (const std::pair<std::string, ptree>& p : d) { std::cout << p.second.get_value<std::string>() << std::endl; } boost::optional<ptree&> e = pt.get_child_optional("E:"); std::cout << e.is_intialized() << std::endl; return 0; }
You can call the member function get_optional() if you want to read the value of a key, but you aren't sure if the key exists. get_optional() returns the value in an object of type boost::optional. The object is empty if the key wasn't found. Otherwise, get_optional() works the same as get().
The difference between put_child() and add_child() is that put_child() accesses a key if that key already exists, while add_child() always inserts a new key into the tree. get_child_optional() is used like get_child(). get_child_optional() returns an object of type boost::optional.
5. serializing a boost::property_tree::ptree in the JSON format
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <iostream> using namespace boost::property_tree; int main() { ptree pt; pt.put("C:.Windows.System", "20 files"); pt.put("C:.Windows.Cursors", "50 files"); json_parser::write_json("file.json", pt); ptree pt2; json_parser::read_json("file.json", pt2); std::cout << std::boolalpha << (pt == pt2) << std::endl; return 0; }
write_json() and read_json() make it possible to save and load a boost::property_tree::ptree serialized in the JSON format. That way you can support configuration files in the JSON format.
Except json format, Boost.PropertyTree supports additional data formats. you use boost::property_tree::ini_parser::write_ini() adn boost::property_tree::ini_parser::read_ini() to supprot ini-files; boost::property_tree::xml_parser::write_xml() and boost::property_tree::xml_parser::read_xml to support xml format.