简体   繁体   English

提升property_tree用于存储指针

[英]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? 是否可以将指向对象的指针存储在boost属性树中,然后使用迭代器来检索数据? 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. 问题是我得到了错误lexical_cast.hpp:1112:错误:在词法转换行的“流>>输出”中与“运算符>>”不匹配。

and adding the following to MyObject doesn't help 并将以下内容添加到MyObject并没有帮助

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

I've also tried c casts and dynamic casts to no avail. 我也尝试了c强制转换和动态强制转换均无济于事。

Is using pointers even possible inside a ptree? 在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. 当您实际上试图lexical_cast 指向 MyObject的指针时 ,添加引用MyObject的运算符>>不会有帮助。 You could conceivably create an operator>>(std::istream&, MyObject*&) . 可以想象您可以创建一个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. 但是,请记住,property_tree是专为从文本文件读取配置而设计的,因此您将很高兴将对象与文本相互转换。

Don't use property_tree as a generic data structure. 不要将property_tree用作通用数据结构。 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. 此示例还显示了对XML的序列化,即包含(可能)重复的指针的集合。 On deserialization, the pointers will be reconstructed faithfully (including the duplication). 反序列化时,指针将忠实地重建(包括复制)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM