在SWIG for Python中包装boost :: shared_ptr的std :: vector [英] Wrapping std::vector of boost::shared_ptr in SWIG for Python

查看:648
本文介绍了在SWIG for Python中包装boost :: shared_ptr的std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:解决了,我的错误;

  std: :vector& boost :: shared_ptr<实体> >实体; 

,我尝试通过SWIG公开它:

 %includeboost_shared_ptr.i
%includestd_vector.i

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

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

%include< TheFileWithEntities.h>但是,在Python实体最终是一个元组:


$ b


  import MyModule 
打印类型(MyModule.cvar.entities)
#输出:(类型'tuple')

我已经为此搜索了,但没有找到任何具体的例子如何包装。一个页面给了一个小例子,为C#封装它,但它在我的情况下没有帮助。



任何帮助是非常感激。

解决方案

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

  class Globals 
{
public:
std :: vector< boost :: shared_ptr<实体> >实体;
};

extern全局变量


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.

解决方案

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;

这篇关于在SWIG for Python中包装boost :: shared_ptr的std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆