开发者

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

开发者 https://www.devze.com 2023-02-22 09:09 出处:网络
EDIT: Solved, my mistake; explained in my answer. I have this: std::vector < boost::shared_ptr < Entity > > entities;

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开发者_StackOverflow中文版 <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.


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


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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消