2011-10-14 39 views
0

目前自動型我有這樣的代碼:在Boost的屬性樹

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>()); 
} 

我想簡單地這一點。唯一阻止我的是我將來也會有std :: string變量,這意味着只需遍歷所有的值並使用as()不會起作用。

我考慮嘗試使用boost :: any作爲<>(),但這不起作用(加載模板錯誤)。我也考慮過使用一個具有指定它的類型的值的元組,然後切換並調用適當的<>(),但這似乎有點矯枉過正。

有沒有簡單的方法呢?

+1

你想實現什麼? (如果你不想實現任何東西,很容易簡化,只需全部刪除即可) – Florian

+0

我想通過每個變量值並將其插入到配置樹,字符串或int中。 – Jookia

回答

1
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"); 

如果你想實現運行時多態性,上述功能轉換爲一個仿函數並將其分配給的boost ::功能。