【问题标题】:Wrapping Boost ptree to keep a map of loaded xml tag包装 Boost ptree 以保留加载的 xml 标记的映射
【发布时间】:2013-04-05 05:46:44
【问题描述】:

如标题所述:

struct AXmlConfigurator : private boost::noncopyable {
protected:
    std::string configuration_file_;
    std::map<std::string, std::string> parameters_;
    ptree ptree_;

public:

    AXmlConfigurator(const std::string &argConfigurationFile) :
        configuration_file_(argConfigurationFile) {
        read_xml(configuration_file_, ptree_);
    }

    ~AXmlConfigurator() {}

    template<typename Type> inline
    Type get(const std::string & argPath, Type argDefault) const {
        Type value = ptree_.get<Type>(argPath, argDefault);
        parameters_.insert(std::pair<std::string,std::string>(argPath, value));
        return value;
    }

    template<class Type , class Translator> inline
    typename boost::enable_if<detail::is_translator<Translator>, Type>::type
    get(const std::string &argPath, const Type &argDefault, Translator tr) const {
        Type value = ptree_.get(argPath, argDefault);
        parameters_.insert(std::pair<std::string,std::string>(argPath, value));
        return value;
    }
    void logConfiguration() {
        for(std::map<std::string, std::string>::const_iterator it = parameters_.begin() ; it != parameters_.end() ; ++it) {
            std::cout << (*it).first << " => " << (*it).second << std::endl;
        }
    }

};

我想包装 boost::property_tree::ptree 以将参数/值作为字符串存储到映射中,然后通过 logConfiguration 显示它们。

问题在于 map 的插入不接受值作为字符串,因为值类型未指定...

我没有想法,因此感谢任何帮助!

【问题讨论】:

  • 请显示您遇到的实际错误。如果我理解这个问题,那么您将无法理解错误告诉您的内容。 我们也是。 :)
  • 您好,感谢您的帮助:),我在插入此 std::map 时尝试的所有内容都出现了各种错误,我可以理解它们与什么相关...地图等待 std ::pair<:string std::string> 虽然我提供了一个未指定的类型......所以我的问题更像是:你将如何实现这个目标?
  • 不清楚为什么您希望get 处理未指定的类型而不是std::string。我们是否应该假设您要问如何将未指定的类型转换为 std::string?
  • 正如你在这里看到的boost.org/doc/libs/1_53_0/doc/html/boost_propertytree/…,get 是模板化的,我想通过失去模板方面来包装它。所以是的,我或多或少需要一种方法来让我的地图接受未指定的value...

标签: c++ templates boost


【解决方案1】:

由于您需要将数据设为std::string,因此更改:

    Type value = ptree_.get<Type>(argPath, argDefault);

到:

    std::string value = ptree_.get<std::string>(argPath, argDefault);
//  ^^^^^^^^^^^                    ^^^^^^^^^^^

【讨论】:

  • 我已经试过了帮助你
  • @Syffys 那么这个问题可能与 Boost ptree 没有直接关系。您已经编写了一个带有参数 argDefault 的函数,并且您需要该数据是 std::string。
  • 我明白,但我的意思是ptree从xml文件中检索数据显然是作为字符串,所以必须有办法从这种类型中获取字符串...
【解决方案2】:

这就是我最终所做的,它非常棒,但我并不追求这件事的表现,所以它会完成这项工作:

template<typename Type>
Type get(const std::string & argPath, Type argDefault) {
    std::string value = "a";
    try {
        value = ptree_.get<std::string>(argPath);
    } catch(ptree_bad_path &e) {
        return argDefault;
    }

    parameters_.insert(std::pair<std::string, std::string>(argPath,value));
    return ptree_.get<Type>(argPath, argDefault);
}

template<class Type , class Translator>
typename boost::enable_if<detail::is_translator<Translator>, Type>::type
get(const std::string &argPath, const Type &argDefault)  {
    std::string value = "";
    try {
        value = ptree_.get<std::string>(argPath);
    } catch(ptree_bad_path &e) {
        return argDefault;
    }

    parameters_.insert(std::pair<std::string,std::string>(argPath, value));
    return ptree_.get(argPath, argDefault);
}

我只是按照 Drew 的建议将参数作为字符串检索,只是在需要时捕获异常并返回默认值...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多