gpt4 book ai didi

c++ - Boost 属性树指定允许值

转载 作者:行者123 更新时间:2023-11-30 05:41:09 25 4
gpt4 key购买 nike

我想使用 boosts 属性树来处理我的 C++ 应用程序的设置,因为它似乎在这种情况下被广泛使用。

我的问题:当更改属性树中的值时(通过 xml 解析或手动),有没有办法预先指定一个键的允许值列表?例如。如果我想做一个简单的“是/否”设置,我是否必须使用 if - 条件检查值,或者我可以以某种方式教我的树只接受特定键的两个值"is"和“否”提前,以便它在出错时抛出异常。

最佳答案

您可以为此使用翻译器。我记得一篇不错的博客文章描述了这个以在 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

关于c++ - Boost 属性树指定允许值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com