如何修复RapidXML字符串所有权问题? [英] How to fix RapidXML String ownership concerns?

查看:171
本文介绍了如何修复RapidXML字符串所有权问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RapidXML 是一个快速,轻量级的C ++ XML DOM解析器,但它有一些怪癖。

RapidXML is a fast, lightweight C++ XML DOM Parser, but it has some quirks.

这些最糟糕的情况是:


3.2字符串的所有权

RapidXml生成的节点和属性不是
拥有其名称和值字符串。他们
只保存指针。这个
意味着你必须小心,当
手动设置这些值,
使用 xml_base :: name(const Ch *)
xml_base :: value(const Ch *)函数。

Nodes and attributes produced by RapidXml do not own their name and value strings. They merely hold the pointers to them. This means you have to be careful when setting these values manually, by using xml_base::name(const Ch *) or xml_base::value(const Ch *) functions.


传递的字符串的生命周期为
,只要
节点/属性的生命周期。
的最简单的方法是从
文档拥有的memory_pool中分配字符串
。使用
memory_pool :: allocate_string()
函数用于此目的。

Care must be taken to ensure that lifetime of the string passed is at least as long as lifetime of the node/attribute. The easiest way to achieve it is to allocate the string from memory_pool owned by the document. Use memory_pool::allocate_string() function for this purpose.


b $ b

现在,我明白这是为了速度这样做,但这感觉像一个汽车碰撞等待发生。以下代码看起来无害,但是name和value在foo返回时超出范围,因此文档未定义。

Now, I understand it's done this way for speed, but this feels like an car crash waiting to happen. The following code looks innocuous but 'name' and 'value' are out of scope when foo returns, so the doc is undefined.

void foo()
{
  char name[]="Name";
  char value[]="Value";

  doc.append_node(doc.allocate_node(node_element, name, value));
}

建议使用 allocate_string c $ c>根据手工作品,但它是如此容易忘记。

The suggestion of using allocate_string() as per manual works, but it's so easy to forget.

有没有人改进了RapidXML以避免此问题?

Has anyone 'enhanced' RapidXML to avoid this issue?

推荐答案

我不使用RapidXML,但也许我的方法可以解决你的问题。

I don't use RapidXML, but maybe my approach can solve your problem.

我开始使用Xerces,但我发现它很重,除了其他小麻烦,移至CPPDOM。当我移动时,我决定创建一组包装类,所以我的代码不会依赖于特定的XML'引擎',我可以移植到另一个如果需要。

I started using Xerces, but I found it heavy, besides other minor annoyances, so I moved to CPPDOM. When I made the move I decided to create a set of wrapper classes, so that my code wouldn't be dependent from the specific XML 'engine' and I could port to another if needed.

我创建了自己的类来表示基本的DOM实体(节点,文档等)。这些类在内部使用pimpl成语来使用CPPDOM对象。
因为我的节点对象包含'real'节点对象(来自CPPDOM),我可以根据需要管理任何东西,所以正确的分配和释放字符串不会是一个问题。

I created my own classes to represent the basic DOM entities (node, document, etc). Those classes use internally the pimpl idiom to use CPPDOM objects. Since my node object contains the 'real' node object (from CPPDOM) I can manage anything as needed, so proper allocation and deallocation of strings wouldn't be a problem there.

由于我的代码是为CPPDOM,我不认为这将是非常有用的,但我可以发布,如果你想要的。

Since my code is for CPPDOM, I don't think it would be much useful for you, but I can post it if you want.

BTW,如果你已经有太多的代码已经使用RapidXML,你可以在你的包装类中重现它的接口。我没有这样做,因为使用Xerces的代码不是那么长,我将不得不重写它。

BTW, if you already have too much code that already uses RapidXML you can reproduce its interfaces in your wrapper classes. I didn't do it because the code that used Xerces was not that long and I'd have to rewrite it anyway.

这篇关于如何修复RapidXML字符串所有权问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆