属性 树:完整路径和名称
Property tree: complete path and name
我想创建一个可以从 属性 树构建的 class,如本例所示:
<?xml version="1.0" encoding="utf-8"?>
<config>
<name>testing</name>
<!-- Test property tree -->
<lambda min="200000" max="200">100</lambda>
...
使用 属性 树很容易,但我需要访问子树的两个属性,如 class:
parameter::parameter(boost::property_tree::ptree t)
{
// Set the value
value = t.get_value<double>();
// ?????
auto nodename = t.something();
// ?????
std::string nodepath = t.somethingelse();
// Get the attributes (or empty)
auto p = t.get_child("<xmlattr>", boost::property_tree::ptree());
// If we have attributes, read them
if (p != boost::property_tree::ptree())
{
min = t.get<double>("<xmlattr>.min");
max = t.get<double>("<xmlattr>.max");
if (min > max)
throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath);
}
else
{
min = +1.0;
max = -1.0;
}
}
// ... Someplace else
lambda = parameter(config.get_child("config.lambda"));
在 XML 中,lambda
的 mim/max 属性无效,我需要抛出一个可以读作
的异常
Min and max values invalid for the parameter lambda, path: config.lambda
当然我可以只传递字符串,但这会破坏目的。我试过弄乱 t
的迭代器和 data
,但一无所获。
我可以从 ptree
中获取这些值吗?
谢谢!
我会略微调整一下界面,这样您就不会过早地删除所需的信息:
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
struct parameter {
parameter(ptree const& tree, ptree::path_type const& nodepath)
{
ptree const& t = tree.get_child(nodepath);
// Set the value
value = t.get_value<double>();
auto nodename = [nodepath] {
auto copy = nodepath;
while (!copy.single()) copy.reduce();
return copy.reduce();
}();
// Get the attributes (or empty)
auto p = t.get_child("<xmlattr>", boost::property_tree::ptree());
// If we have attributes, read them
if (p != boost::property_tree::ptree())
{
auto min = t.get<double>("<xmlattr>.min");
auto max = t.get<double>("<xmlattr>.max");
if (min > max)
throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath.dump());
}
else
{
min = +1.0;
max = -1.0;
}
}
private:
double min, max;
double value;
};
int main() {
ptree config;
std::ifstream xml("input.txt");
read_xml(xml, config);
auto lambda = parameter(config, "config.lambda");
}
版画
terminate called after throwing an instance of 'std::runtime_error'
what(): Min and max values invalid for the parameter lambda, path: config.lambda
我想创建一个可以从 属性 树构建的 class,如本例所示:
<?xml version="1.0" encoding="utf-8"?>
<config>
<name>testing</name>
<!-- Test property tree -->
<lambda min="200000" max="200">100</lambda>
...
使用 属性 树很容易,但我需要访问子树的两个属性,如 class:
parameter::parameter(boost::property_tree::ptree t)
{
// Set the value
value = t.get_value<double>();
// ?????
auto nodename = t.something();
// ?????
std::string nodepath = t.somethingelse();
// Get the attributes (or empty)
auto p = t.get_child("<xmlattr>", boost::property_tree::ptree());
// If we have attributes, read them
if (p != boost::property_tree::ptree())
{
min = t.get<double>("<xmlattr>.min");
max = t.get<double>("<xmlattr>.max");
if (min > max)
throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath);
}
else
{
min = +1.0;
max = -1.0;
}
}
// ... Someplace else
lambda = parameter(config.get_child("config.lambda"));
在 XML 中,lambda
的 mim/max 属性无效,我需要抛出一个可以读作
Min and max values invalid for the parameter lambda, path: config.lambda
当然我可以只传递字符串,但这会破坏目的。我试过弄乱 t
的迭代器和 data
,但一无所获。
我可以从 ptree
中获取这些值吗?
谢谢!
我会略微调整一下界面,这样您就不会过早地删除所需的信息:
#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;
struct parameter {
parameter(ptree const& tree, ptree::path_type const& nodepath)
{
ptree const& t = tree.get_child(nodepath);
// Set the value
value = t.get_value<double>();
auto nodename = [nodepath] {
auto copy = nodepath;
while (!copy.single()) copy.reduce();
return copy.reduce();
}();
// Get the attributes (or empty)
auto p = t.get_child("<xmlattr>", boost::property_tree::ptree());
// If we have attributes, read them
if (p != boost::property_tree::ptree())
{
auto min = t.get<double>("<xmlattr>.min");
auto max = t.get<double>("<xmlattr>.max");
if (min > max)
throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath.dump());
}
else
{
min = +1.0;
max = -1.0;
}
}
private:
double min, max;
double value;
};
int main() {
ptree config;
std::ifstream xml("input.txt");
read_xml(xml, config);
auto lambda = parameter(config, "config.lambda");
}
版画
terminate called after throwing an instance of 'std::runtime_error'
what(): Min and max values invalid for the parameter lambda, path: config.lambda