Boost属性树指定允许的值 [英] Boost property tree specify allowed values
问题描述
我想使用boosts属性树来处理我的c ++应用程序的设置,因为它似乎在这种情况下被广泛使用。
I wanted to use boosts property tree as handling the settings of my c++ app since it seems to be widely used in this scenario.
我的问题:更改值时在属性树中(通过xml解析或手动),是否可以预先指定键的允许值列表?
例如如果我想做一个简单的是/否设置,是否必须使用if-条件检查这些值,或者我可以以某种方式教我的树只接受特定键的是和否两个值?提前,以便在错误时引发异常。
My question: when changing values in the property tree (through xml parsing or manually), is there a way to specify a list of allowed values of a key in advance? E.g. if I wanted to do a simple "Yes/No" setting, do I have to check the values with an if - condition or can I somehow teach my tree to only accept the two values "Yes" and "No" for the specific key in advance, so that it throws an exception on error.
推荐答案
您可以为此使用翻译器。我记得有一篇不错的博客文章,描述了如何在XML支持的属性树中解析自定义日期格式:
You can use translators for this. A nice blog post I remember that describes this to get custom date format parsing in an XML-backed property tree was here:
- Andzrej's blog
让我们举个例子:
enum class YesNo { No, Yes };
在这种情况下,调用代码可能如下:
In this case the calling code could look like:
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";
}
}
}
完整演示
#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
这篇关于Boost属性树指定允许的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!