暴露类的 boost::tuple 部分来提升 python

发布于 2024-11-17 09:03:38 字数 986 浏览 3 评论 0原文

我一直在试图弄清楚如何在我的类中公开一个 boost::tuple 属性。元组定义如下:

typedef boost::shared_ptr<Action> action_ptr;
typedef boost::tuple<BattleCharacter*, action_ptr > ActionTargetTuple;

它包含一个定义如下的类:

class Action : public Cloneable<Action>
{
     public:
          //Irrelevant Code Omitted
          std::vector<ActionTargetTuple> Targets;
}

当我搜索如何将 boost::tuple 转换为 python 元组时,我看过很多文章,但这不是我想要做的。我希望能够访问 Action 类中存在的元组。 (我知道如何做矢量部分)。

class_<Action, std::auto_ptr<ActionWrapper> >("Action")
        .def("Targets", &Action::Targets)
    ;

我就按照上面简单的暴露一下。我想我也许可以通过下面的一些变体来公开它:

 class_<ActionTargetTuple>("ActionTargetTuple")
        .def("get", &ActionTargetTuple::get<int>, return_value_policy<reference_existing_object>())
        ;

然后使用从 python 获取,但如果以这种方式可行,我不确定需要进行什么设置。有谁知道如何做到这一点/可以建议替代方案吗?

谢谢

I've been trying to figure out how to expose a property in my class that is a boost::tuple. The tuple is defined as follows:

typedef boost::shared_ptr<Action> action_ptr;
typedef boost::tuple<BattleCharacter*, action_ptr > ActionTargetTuple;

It's contained with a class defined as follows:

class Action : public Cloneable<Action>
{
     public:
          //Irrelevant Code Omitted
          std::vector<ActionTargetTuple> Targets;
}

I've seen numerous articles while I was searching about how to convert a boost::tuple into a python tuple, but that's not what I'm looking to do. I want to be able to access the tuple as it exists on the Action class. (I know how to do the vector part).

class_<Action, std::auto_ptr<ActionWrapper> >("Action")
        .def("Targets", &Action::Targets)
    ;

I expose it simply as above. I figured I might be able to expose it by some variation on the below:

 class_<ActionTargetTuple>("ActionTargetTuple")
        .def("get", &ActionTargetTuple::get<int>, return_value_policy<reference_existing_object>())
        ;

then use get from python, but if it is doable in this way, I'm not sure what the set up needs to be. Does anyone know how to do this/could suggest an alternative?

Thanks

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

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

发布评论

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

评论(2

鼻尖触碰 2024-11-24 09:03:38

您可以使用:

...
    .add_property("Targets", & ActionTargetTuple::get, &ActionTargetTuple::set)

在 C++ 中使用 getter/setter 方法创建读写属性

如果您想控制所有权:

namespace bp = boost::python;
...
    .add_property("Targets",
        bp::make_function(&ActionTargetTuple::get, bp::return_value_policy<...>()),
        bp::make_function(&ActionTargetTuple::set, bp::return_value_policy<...>())
    )

You can use:

...
    .add_property("Targets", & ActionTargetTuple::get, &ActionTargetTuple::set)

to make a read-write property using getter/setter methods in c++

If you want to control ownership:

namespace bp = boost::python;
...
    .add_property("Targets",
        bp::make_function(&ActionTargetTuple::get, bp::return_value_policy<...>()),
        bp::make_function(&ActionTargetTuple::set, bp::return_value_policy<...>())
    )
安穩 2024-11-24 09:03:38

除了使用上一个答案中解释的 add_property 和编写访问器函数之外,您还可以考虑为元组编写转换器(在 boost::tuple 和 boost:: 之间) python::tuple)并直接使用 def_readonlydef_readwrite 公开这些属性。如果您有许多这样的属性需要公开,那么这是值得的。

This 有一个可以用于 c++→python 转换的模板(使用 boost::tuple 而不是 std::pair),不过除非你使用 c++0x,否则你必须写出不同数量参数的模板。

如果您的属性是读写的,请另外定义 from-python 转换器,您可以在网上找到示例。 这里是我的代码用于定义通用的sequence-std::vector 转换器。在您的情况下,您必须检查 python 对象是否是一个序列,它是否具有正确数量的项目,您可以从每个项目中提取所需的类型;然后返回新的 boost::tuple 对象。

HTH,edx。

PS我发现ackward 已准备好转换器,也许您可​​以重复使用它。 此处的文档

Besides using add_property as explained in the previous answer, and writing accessor functions, you can consider writing converters for your tuple (between boost::tuple and boost::python::tuple) and exposing those attributes directly with def_readonly or def_readwrite. It is worth it if you have many such attributes to expose.

This has a template you adapt can for c++→python conversion (use boost::tuple instead of std::pair), though unless you go c++0x, you have to write out templates for different number of arguments.

If your property is read-write, additionaly define from-python converter, you find examples on the web. Here is my code I use to define generic sequence-std::vector converter. In your case, you have to check that the python object is a sequence, that it has the right number of items, that you can extract required types from each of them; and then return new boost::tuple object.

HTH, edx.

P.S. I found ackward has the converters ready, perhaps you could just reuse it. Doc here

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