Boost 属性 树指定允许值

Boost property tree specify allowed values

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

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

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