How to initialize Boost.PropertyTree with Boost.Assign

1.4k Views Asked by At

There is a sample of filling boost::property_tree::ptree

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

How to extend Boost.Assign library that it fits for initializing boost::property_tree::ptree object? I'd like something like

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

There are 1 best solutions below

0
On

You can use something like

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

usage:

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