在 boost::python 中实例化shared_ptr

发布于 2024-11-06 01:44:35 字数 769 浏览 3 评论 0 原文

我有一个关于 boost python 的问题。我一直致力于将项目的一些功能导出到boost python中,但我还没有找到解决以下问题的方法: 我有一组 StatusEffect 对象,我在整个游戏中存储和使用它们。在游戏启动时,我希望能够调用一个 python 脚本来填充/添加到状态效果对象集。我在向 python 公开 StatusEffect 类及其派生类并调用脚本时没有任何问题。

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

答案或一些有用的建议将会有所帮助。我昨天问了一个类似的问题,但不幸的是它没有多大帮助。

提前致谢

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.

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.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷离° 2024-11-13 01:44:35

我希望我能理解你的问题。如果您使用 shared_ptr 声明 Python 类,如 http 所示: //wiki.python.org/moin/boost.python/PointersAndSmartPointers,然后 boost::python 会自动将你在 python 中创建的 StatusEffect 对象转换为 < code>shared_ptr 如有必要(您可以通过 .def 尝试此操作,例如使用 const shared_ptr& 或 < code>shared_ptr 作为参数,并使用在 python 中创建的 StatusEffect 实例来调用它

。 ,您必须为其创建转换器(从 python 序列开始,然后返回),如文档中所述。有关示例,请参阅 c++ 到 python 转换器模板(第 120 行),python 到 c++ 模板(第 127 行),然后将其用于序列中包含的各种类型(包括shared_ptr's)(第212行)。

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

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.

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

Then you can write something like yourObject.listOfStatusEffects=[StatusEffect(),StatusEffect(),StatusEffect()]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文