在boost :: python中实例化shared_ptr [英] Instantiating shared_ptr's in boost::python

查看:504
本文介绍了在boost :: python中实例化shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于boost python的问题。我一直在努力将一个项目的一些功能导出到boost python,我没有找到一种方法来解决以下问题:
我有一组StatusEffect对象,我存储和使用整个游戏。在游戏启动时,我想要能够调用一个python脚本,将填充/添加到一组状态效果对象。我没有问题将StatusEffect类和它的派生类暴露给python并调用脚本。

I had a question about boost python. I've been working on exporting some functionality of a project into boost python, and I haven't found a way to solve the following problem: I have a set of StatusEffect objects that i store and use throughout the game. At the game startup, I want to be able to call a python script that will populate/add to the set of status effect objects. I'm having no problems exposing the StatusEffect class and it's derived class to python and calling the script.

问题是我将StatusEffect对象存储在 std :: vector< boost :: shared_ptr< StatusEffect> >效果;
我不知道如何创建 boost :: shared_ptr< StatusEffect> 的新实例,除了添加如此处所述的静态创建方法 http://wiki.python.org/moin/boost.python / PointersAndSmartPointers由于有大量的构造函数和各种各样的派生类我有,这似乎是一个非最佳的解决方案。我想能够直接使用StatusEffect对象的构造函数创建boost :: shared_ptr的实例,并且能够将它们添加到向量。这是否可能?

The problem is that I'm storing that StatusEffect objects in an std::vector<boost::shared_ptr<StatusEffect> > Effects; I have no idea how to create new instances of boost::shared_ptr<StatusEffect> aside from the method of adding a static create method as described here http://wiki.python.org/moin/boost.python/PointersAndSmartPointers Given the large number of constructors and the wide variety of derived classes I have, this seems an unoptimal solution at best. I'd like to be able to create instances of boost::shared_ptr directly using the constructors of the StatusEffect objects, and be able to add those to the vector. Is this possible?

一个答案或一些有用的建议是有帮助的。

An answer or some helpful suggestions would be helpful. I asked a simialr question yesterday but unfortunately it wasn't of much help.

提前感谢

推荐答案

我希望我能理解你的问题。如果你用 shared_ptr 声明你的python类,如 http://wiki.python.org/moin/boost.python/PointersAndSmartPointers ,则 boost :: python 将自动转换 StatusEffect 在python中创建的对象到 shared_ptr< StatusEffect> 如果有必要,你可以尝试这个例如。 def - 一个函数 const shared_ptr< StatusEffect>& shared_ptr< StatusEffect> 作为参数,并使用 StatusEffect 在python中创建的实例调用它。

I hope I am understanding your question. If you declare your python class with the shared_ptr as shown at http://wiki.python.org/moin/boost.python/PointersAndSmartPointers, then boost::python will automatically convert StatusEffect objects you create in python to shared_ptr<StatusEffect> if necessary (you can try this e.g. by .def-ing a function which takes const shared_ptr<StatusEffect>& or shared_ptr<StatusEffect> as argument, and call it with StatusEffect instance created in python.

如果要分配属性向量< shared_ptr< StatusEffect>> 类型,必须为它创建转换器(从python序列, a href =http://bazaar.launchpad.net/~yade-dev/yade/trunk/view/head:/py/wrapper/customConverters.cpp#L120 =nofollow> c ++ to python 转换器模板(行120),python到c ++模板(行127),然后将其用于序列(行212)中包含的各种类型(包括shared_ptr)。

If you want to assign an attribute of type vector<shared_ptr<StatusEffect> >, you must create converters for it (from python sequences, and back), that's described in documentation. For an example, see c++ to python converter template (line 120), python to c++ template (line 127), and then using it for various types (including shared_ptr's) contained in the sequences (line 212).

然后你可以写出 yourObject.listOfStatusEffects = [StatusEffect(),StatusEffect(),StatusEffect()]

这篇关于在boost :: python中实例化shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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