24#ifndef __BITPIT_CONFIGURATION_TREE_TPP__
25#define __BITPIT_CONFIGURATION_TREE_TPP__
44template<
typename Source>
45void readConfiguration(Source &source, SourceFormat format,
const std::string &rootName,
46 bool checkVersion,
int version, Config *rootConfig)
49 throw std::runtime_error(
"config::tree::readConfiguration Null Config tree structure passed");
53 boost::property_tree::ptree propertyTree;
54 if (format == SOURCE_FORMAT_XML) {
55 boost::property_tree::read_xml(source, propertyTree);
56 }
else if (format == SOURCE_FORMAT_JSON) {
57 boost::property_tree::read_json(source, propertyTree);
66 auto rootNode = propertyTree.begin();
69 if (rootNode->first != rootName) {
70 throw std::runtime_error(
"The name of the root element is not \"" + rootName +
"\"");
76 int fileVersion = rootNode->second.get<
int>(
"<xmlattr>.version");
77 if (fileVersion != version) {
78 throw std::runtime_error(
"The version of the configuration file is not not \"" + std::to_string(version) +
"\"");
80 }
catch (boost::property_tree::ptree_bad_path &error) {
82 throw std::runtime_error(
"Unable to identify the version of the configuration");
88 const boost::property_tree::ptree *configTree;
90 configTree = &(propertyTree.begin()->second);
92 configTree = &propertyTree;
95 importTree(*configTree, rootConfig);
109template<
typename Destination>
110void writeConfiguration(Destination &destination, SourceFormat format,
const std::string &rootName,
111 int version,
const Config *rootConfig)
114 throw std::runtime_error(
"config::tree::writeConfiguration Null Config tree structure passed");
121 boost::property_tree::ptree propertyTree;
126 propertyTree.put<std::string>(rootName,
"");
129 auto rootNode = propertyTree.begin();
132 rootNode->second.put<
int>(
"<xmlattr>.version", version);
136 boost::property_tree::ptree *configTree;
138 configTree = &(propertyTree.begin()->second);
140 configTree = &propertyTree;
145 exportTree(*rootConfig, areArraysAllowed, configTree);
148 writeTree(destination, format, propertyTree);
158template<
typename Destination>
159void writeTree(Destination &destination, SourceFormat format, boost::property_tree::ptree &propertyTree)
161 if (format == SOURCE_FORMAT_XML) {
162 boost::property_tree::xml_writer_settings<std::string> settings(
' ', 4);
163 write_xml(destination, propertyTree, settings);
164 }
else if (format == SOURCE_FORMAT_JSON) {
165 write_json(destination, propertyTree,
true);
#define BITPIT_UNUSED(variable)
bool hasArraySupport(SourceFormat format)
bool hasRootSection(SourceFormat format)