Boost logo

Boost :

Subject: [boost] property_tree iterators
From: Chris Meyer (cmeyer1969+boost_at_[hidden])
Date: 2009-05-12 20:25:54


I've been using property_tree and it is very useful to me.

It's easy to add typed data to the tree using something like

MyType myvar[10];
for (i=0; i<10; ++i)
        pt.put(key, myvar[i], true);

I can also implement my own translator to convert MyType to a string
suitable for serializing to XML, JSON, or whatever.

However, if I go to retrieve this data, things are less refined.

Iterating over all elements with a given key requires something like this:

ptree::key_compare comp;
BOOST_FOREACH(ptree::value_type &v, pt)
{
    if (!comp(v.first, key) && !comp(key, v.first))
    {
        MyType myvar = convertStringToMyType(v.second.data());
        doSomething(myvar);
    }
}

Perhaps there should be a templatized member function which uses the
translator to automatically iterate all values of a particular key and
have it converted to the desired data type.

Then I could use something like:

BOOST_FOREACH(MyType myvar, pt.get_all<MyType>(key))
{
        doSomething(myvar);
}

Is this possible? Or is there something I'm missing from the existing
API that allows me to do this?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk