我想使用boost属性树来处理我的c ++应用程序的设置,因为它似乎在这种情况下被广泛使用。
我的问题:在更改属性树中的值时(通过xml解析或手动),有没有办法提前指定键的允许值列表? 例如。如果我想做一个简单的"是/否"设置,我是否必须使用if - 条件检查值,或者我可以以某种方式教我的树只接受这两个值"是"和"不"提前输入特定的密钥,以便在出错时抛出异常。
答案 0 :(得分:1)
您可以使用翻译器。我记得一篇很好的博客文章描述了这一点,以便在XML支持的属性树中获取自定义日期格式解析:
我们举个例子:
enum class YesNo { No, Yes };
在这种情况下,调用代码可能如下所示:
static YesNoTranslator trans;
int main() {
std::istringstream iss(R"(
<?xml version="1.0"?>
<demo>
<positive>Yes</positive>
<negative>No</negative>
<invalid>Bogus</invalid>
</demo>
)");
ptree pt;
read_xml(iss, pt);
for (auto&& field : { "demo.positive", "demo.negative", "demo.invalid" })
{
try {
std::cout << "With 'No' default: '" << field << "':\t" << pt.get(field, YesNo::No, trans) << "\n";
std::cout << "Without default: '" << field << "':\t" << pt.get<YesNo>(field, trans) << "\n";
} catch(std::exception const& e) {
std::cout << "Error parsing '" << field << "':\t" << e.what() << "\n";
}
}
}
<强> Live On Coliru 强>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>
#include <iostream>
using boost::property_tree::ptree;
enum class YesNo { No, Yes };
static inline std::ostream& operator<<(std::ostream& os, YesNo v) {
switch(v) {
case YesNo::Yes: return os << "Yes";
case YesNo::No: return os << "No";
}
return os << "??";
}
struct YesNoTranslator {
typedef std::string internal_type;
typedef YesNo external_type;
boost::optional<external_type> get_value(internal_type const& v) {
if (v == "Yes") return YesNo::Yes;
if (v == "No") return YesNo::No;
return boost::none;
}
boost::optional<internal_type> put_value(external_type const& v) {
switch(v) {
case YesNo::Yes: return std::string("Yes");
case YesNo::No: return std::string("No");
default: throw std::domain_error("YesNo");
}
}
};
static YesNoTranslator trans;
int main() {
std::istringstream iss(R"(
<?xml version="1.0"?>
<demo>
<positive>Yes</positive>
<negative>No</negative>
<invalid>Bogus</invalid>
</demo>
)");
ptree pt;
read_xml(iss, pt);
for (auto&& field : { "demo.positive", "demo.negative", "demo.invalid" })
{
try {
std::cout << "With 'No' default: '" << field << "':\t" << pt.get(field, YesNo::No, trans) << "\n";
std::cout << "Without default: '" << field << "':\t" << pt.get<YesNo>(field, trans) << "\n";
} catch(std::exception const& e) {
std::cout << "Error parsing '" << field << "':\t" << e.what() << "\n";
}
}
}
打印
With 'No' default: 'demo.positive': Yes
Without default: 'demo.positive': Yes
With 'No' default: 'demo.negative': No
Without default: 'demo.negative': No
With 'No' default: 'demo.invalid': No
Without default: 'demo.invalid': Error parsing 'demo.invalid': conversion of data to type "5YesNo" failed