
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?

On Tue, 12 May 2009 17:25:54 -0700, Chris Meyer <cmeyer1969+boost@gmail.com> wrote:
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?
Nothing in the existing API, although it is possible. However, I feel that this would be more suited for a generic iterator/range adaptor library instead of being embedded in the core interface. The new RangeEx library, I believe, should help you there. You can take the full range of children (actually, I plan to add iteration over the children with a given key), filter on the key, then add a transform iterator that extracts and converts the value. Sebastian
participants (2)
-
Chris Meyer
-
Sebastian Redl