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

标签 c++ python vector swig shared-ptr

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

我有这个:

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

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

非常感谢任何帮助。

最佳答案

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

%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 似乎不知道如何将 Python 指针序列转换为 myObjectTmyObjectVectorT .

更新:出于某种原因我还没有弄清楚,这导致无法调用 myObjectT 上的方法。来自myObjectPtrT ,即使我也使用过 SWIG_SHARED_PTR(myObjectT, myObject<T>) .

关于c++ - 在 Python 的 SWIG 中包装 boost::shared_ptr 的 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5535996/

相关文章:

C++ _inline 在单例中被忽略并出现在我的探查器中。怎么会?

python - 如何使用 ctypes 将数组从 Go [lang] 返回到 Python?

python - 递归函数 - Python

c++ - std::vector 迭代器和调整大小/保留的奇怪/有趣的行为

c++ - vector<T>::push_back() 是否调用其类型 T 的任何匹配构造函数?

c++ - 高频Insert到PostgreSQL,插入的数据几分钟后才能被选中?

c++ - x,y,z 点的 vtk 值

c++ - libtorque - 我如何包含 PBSD_status 函数?

java - Vector.remove(Object e) 在 Java 中如何工作?

python - 线程子进程并取得进展