开发者

Boost property_tree for storing pointers

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...

an开发者_如何转开发d 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.


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜