Wrapping std::vector of boost::shared_ptr in SWIG for Python

谁说胖子不能爱 提交于 2019-12-05 05:27:59

问题


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.


回答1:


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




回答2:


See How to expose std::vector<int> as a Python list using SWIG? for probably good info




回答3:


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;


来源:https://stackoverflow.com/questions/5535996/wrapping-stdvector-of-boostshared-ptr-in-swig-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!