1

Is it possible to store pointers to objects in boost property trees, and then use an iterator to retrieve the data? I'm trying to do something like:

property_tree::ptree pt;
pt.put<CGUICrateElement*>("1.2.3.4", new MyObject() );
//... more tree construction here...

and then recursively itererate through all the tree nodes with something like:

property_tree::ptree::iterator iter = treeNode.begin();
property_tree::ptree::iterator iter_end = treeNode.end();

for ( ; iter != iter_end; ++iter )
{
MyObject *obj = lexical_cast<MyObject*>(iter->second.data());
    //... etc 

The problem is I get the error lexical_cast.hpp:1112: error: no match for 'operator>>' in 'stream >> output' on the lexical cast line.

and adding the following to MyObject doesn't help

friend std::istream& operator>>(std::istream& in, MyObject& obj){ return in; }

I've also tried c casts and dynamic casts to no avail.

Is using pointers even possible inside a ptree? I'm about to just create my own tree structure as a workaround by I figured I'd ask here first.

Cheers.

Eran
  • 21,632
  • 6
  • 56
  • 89
gmcc051
  • 390
  • 3
  • 17

2 Answers2

0

Adding an operator>> for a reference to MyObject won't help when you're actually trying to lexical_cast to a pointer to MyObject. You could conceivably create an operator>>(std::istream&, MyObject*&). However, remember that property_tree is designed for reading configuration from text files, so you'll have the joy of converting your object to and from text.

Don't use property_tree as a generic data structure. Internally it will be expecting to deal with text.

RobH
  • 3,199
  • 1
  • 22
  • 27
  • Thanks for the info. I was starting to get the feeling that the property tree was not the tool to be using here. I ended up creating my own tree data structure using the basic structure of the ptree. – gmcc051 Oct 16 '11 at 06:22
0

It is looking a lot like you wanted a serialization solution. I covered some ground (including storing through pointers) in this post:

copying and repopulating a struct instance with pointers

This example also shows serialization to XML, collections containing (potentially) duplicated pointers. On deserialization, the pointers will be reconstructed faithfully (including the duplication).

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633