开发者

Automatic type in Boost's Property Tree

Currently I have this code:

if(!variables["width"].defaulted())
{
    configTree.put(treeNames["width"], variables["width"].as<int>());
}

if(!variables["height"].defaulted())
{
    configTree.put(treeNames["height"], variables["height"].as<int>());
}

if(!variables["fullscreen"].defaulted())
{
    configTree.put(treeNames["fullscreen"], variables["height"].as<int>());
}

I'm trying to simply this. The only thing stopping me is that I will in the future also have std::string variables, which means simply looping through all the values and using as() isn't going to work.

I've considered trying to use boost::any for as<>(), but that do开发者_StackOverflow社区esn't work (loads of template errors). I've also considered having a tuple with a value specifying which type it will be, then switching through that and calling the appropriate as<>(), but that seems kind of overkill.

Is there a way to simply this?


template<class T> void xput(const char* name) {
    if(!variables[name].defaulted())
        configTree.put(treeNames[name], variables[name].as<T>());
}

xput<int>("width");
xput<int>("height");
xput<int>("fullscreen");
xput<std::string>("future-option");

In case you want to achieve runtime polymorphism, convert the above function to a functor and assign it to boost::function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜