使用 PythonQt 的智能指针
我的应用程序在整个 C++ API 中使用 QSharedPointers,它通常不返回一个对象,而是返回一个指向该对象的智能指针,并且为了方便起见,每个类都有一个附带的 typedef。
class SomeClass
{
SomeClassP getInstance() { return SomeClassP(new SomeClass()); }
}
typedef QSharedPointer<SomeClass> SomeClassP;
这很有效,但我想知道如何/是否需要更改我的设计来处理 PythonQt 集成。例如,在 PythonQtWrapper 中,我应该从指针返回什么?如果我在 python 中处理指针,我如何调用其他采用智能指针而不是普通指针的函数?我需要将智能指针公开给 PythonQt 吗?看起来在 boost::python 中很多智能指针的东西都是自动处理的。我的情况需要做什么?我是否应该在 C++ 中添加接受非智能指针的附加函数,这些函数只需将指针包装在智能指针中并将其发送到智能指针接受函数?看起来 python API 对于指针所有权有一些相当复杂的规则。
class PythonQtWrapper_SomeClass : public QObject
{
Q_OBJECT
public slots:
SomeClass* new_SomeClass() { return new SomeClass(); }
void delete_Mesh(SomeClass* obj) { delete obj; }
SomeClass* static_SomeClass_build(int foo) {
SomeClassP ack = SomeFactory::build(foo);
return ?
}
};
My app uses QSharedPointers throughout the C++ API, where instead of returning an object, it usually returns a smart pointer to it and every class has an accompanying typedef for convenience.
class SomeClass
{
SomeClassP getInstance() { return SomeClassP(new SomeClass()); }
}
typedef QSharedPointer<SomeClass> SomeClassP;
This has worked well, but I'm wondering how/if my design needs to be altered to handle PythonQt integration. For example, in a PythonQtWrapper what should I return from pointers? If I'm dealing with pointers in python, how can I call other functions that take a smart pointer and not a normal pointer? Do I need to expose the smart pointer to PythonQt? It seems that in boost::python a lot of the smart pointer stuff was taken care of automatically. What needs to be done in my case? Should I add additional functions in C++ that accept non-smart-pointers that simply wrap the pointer in a smart pointer and send it on to the smart-pointer-accepting function? It seems the python API has some fairly complex rules regarding pointer ownership.
class PythonQtWrapper_SomeClass : public QObject
{
Q_OBJECT
public slots:
SomeClass* new_SomeClass() { return new SomeClass(); }
void delete_Mesh(SomeClass* obj) { delete obj; }
SomeClass* static_SomeClass_build(int foo) {
SomeClassP ack = SomeFactory::build(foo);
return ?
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们也遇到了同样的问题,但还没有很好的解决方案。
早些时候,我们采用了添加返回原始指针的函数变体的方法。然后我们摆脱了这些,现在在 PythonQt 中装饰智能指针,以便 py 代码可以在它们上调用 .get() 来获取原始指针。这就是我第一次实验性切换到该风格的方法:(来自 https://github.com/realXtend/naali/commit/e72cb827c7fcbaee27b8258e616ee7bd2835fbd7 )
我不确定这是否安全,但似乎有效。如果有更好的解决方案我非常感兴趣。
We are having this exact same problem, and don't have a great solution yet.
Earlier resorted to adding variants of funcs that returned raw pointers instead. Then we got rid of those, and am now decorating smart pointers in PythonQt so that py code can call .get() on them to get the raw pointer. This is how I just made a first experimental switch to that style: (from https://github.com/realXtend/naali/commit/e72cb827c7fcbaee27b8258e616ee7bd2835fbd7 )
Am not sure if that is safe even but seems to work. Am very interested if there are better solutions.