如何使用Boost.Assign初始化Boost.PropertyTree

时间:2013-12-20 10:04:42

标签: c++ boost assign boost-propertytree

有一个填充boost :: property_tree :: ptree

的示例
boost::property_tree::ptree pt;
pt.put("one", "value1");
pt.put("one.two", "value2");
pt.put("one.three", "value3");

如何扩展适合初始化boost::property_tree::ptree对象的 Boost.Assign 库? 我喜欢像

这样的东西
// XXX is some function / functor
boost::property_tree::ptree pt =
   XXX("one", "value1")("one.two", "value2")("one.three", "value3");

1 个答案:

答案 0 :(得分:3)

您可以使用类似

的内容
template<class C>
class call_put
{
   C& c_;
public:
   call_put(C& c) : c_(c) {}
   template<typename T, typename V>
   void operator () (const T& key, const V& value)
   {
      c_.put(key, value);
   }
};

template<class C>
inline boost::assign::list_inserter<call_put<C> >
put(C& c)
{
   return boost::assign::make_list_inserter(call_put<C>(c));
}

用法:

boost::property_tree::ptree pt;
put(pt)("one", "value1")("one.two", "value2")("one.three", "value3");