在 Python 的 SWIG 中包装 boost::shared_ptr 的 std::vector

发布于 2024-10-30 16:24:10 字数 676 浏览 4 评论 0原文

编辑:解决了,我的错误;我的回答中解释了。

我有这个:

std::vector < boost::shared_ptr < Entity > > entities;

我尝试通过 SWIG 公开它,如下所示:

%include "boost_shared_ptr.i"
%include "std_vector.i"

%shared_ptr(Entity)
%include <Entity.h>

namespace std {
    %template(EntityVector) vector<boost::shared_ptr<Entity> >;
};

%include <TheFileWithEntities.h>

但是,在 Python 中实体最终成为一个元组:

import MyModule
print type(MyModule.cvar.entities)
# Output: (type 'tuple')

我已经在 Google 上搜索过这个,但找不到任何关于如何包装它的具体示例。一页给出了一个为 C# 包装它的小示例,但它对我的情况没有帮助。

非常感谢任何帮助。

EDIT: Solved, my mistake; explained in my answer.

I have this:

std::vector < boost::shared_ptr < Entity > > entities;

and I try to expose it through SWIG like this:

%include "boost_shared_ptr.i"
%include "std_vector.i"

%shared_ptr(Entity)
%include <Entity.h>

namespace std {
    %template(EntityVector) vector<boost::shared_ptr<Entity> >;
};

%include <TheFileWithEntities.h>

However, in Python entities ends up being a tuple:

import MyModule
print type(MyModule.cvar.entities)
# Output: (type 'tuple')

I've Googled for this, but could not find any concrete examples on how to wrap this. One page gave a small example for wrapping it for C#, but it didn't help in my case.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不忘初心 2024-11-06 16:24:10

我在将 Python 指针对象序列自动转换为指针对象的 std::vector 时遇到了一些困难。我目前(卡住)使用 Swig 1.3; YMMV,如果您使用的是 Swig 2。诀窍是在 Swig 接口文件(使用 %template)中实例化,不仅是向量,而且不仅仅是对象,还有指针对象

%include "std_vector.i"
%template(myObjectT) namespace::of::myObject<T>;
%template(myObjectPtrT) boost::shared_ptr<namespace::of::myObject<T> >;
%template(myObjectVectorT) std::vector<boost::shared_ptr<namespace::of::myObject<T> > >;

myObjectPtrT,Swig 似乎不太了解如何将指向 myObjectT 的 Python 指针序列转换为 myObjectVectorT

更新:出于某种原因,我还没有弄清楚,这导致无法从 myObjectPtrT 调用 myObjectT 上的方法,即使我已经还使用了 SWIG_SHARED_PTR(myObjectT, myObject)。

I had some difficulty getting a Python sequence of pointer objects to automatically convert into a std::vector of pointer objects. I am currently (stuck) using Swig 1.3; YMMV if you're using Swig 2. The trick was to instantiate in the Swig interface file (with %template) not just the vector, and not just the object, but the pointer objects also:

%include "std_vector.i"
%template(myObjectT) namespace::of::myObject<T>;
%template(myObjectPtrT) boost::shared_ptr<namespace::of::myObject<T> >;
%template(myObjectVectorT) std::vector<boost::shared_ptr<namespace::of::myObject<T> > >;

Without the myObjectPtrT, Swig doesn't seem to know enough to convert a Python sequence of pointers to myObjectT to a myObjectVectorT.

UPDATE: For some reason I haven't yet been able to figure out, this leads to not being able to call methods on myObjectT from a myObjectPtrT, even though I've also used SWIG_SHARED_PTR(myObjectT, myObject<T>).

梦醒灬来后我 2024-11-06 16:24:10

SWIG 似乎将 std::vector 类型的全局变量包装到元组中。解决方案是将实体移入类中,并通过该类的实例访问它。例子:

class Globals
{
public:
     std::vector < boost::shared_ptr < Entity > > entities;
};

extern Globals globals;

SWIG seems to wrap global variables of type std::vector into tuples. The solution is to move entities into a class, and access it through an instance of that class. Example:

class Globals
{
public:
     std::vector < boost::shared_ptr < Entity > > entities;
};

extern Globals globals;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文