如何将C ++对象转换为boost :: python :: object? [英] How to convert C++ objects to boost::python::object?

查看:109
本文介绍了如何将C ++对象转换为boost :: python :: object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bp :: extract将bp :: object转换为特定类型. 问题是如何做反义词?

bp::extract converts bp::object to specific type. The question is how to do vice-verse?

假设我有一个PointContainer和Point类. 我需要具有这种签名的功能

Let's presume I have a PointContainer and Point classes. I need to have a function with such signature

bp::object get_point(const PointContainer &, const bp::object & input);

它应该检查输入参数是否为整数.在这种情况下,它将从PointContainer返回对Point实例的引用,并带有相应的索引. 如果不是整数,则函数检查输入是否为切片对象(例如mylist [1:10:2]).在这种情况下,它将返回PointContainer的副本.

It should check if input argument is an integer. In that case it returns a reference to Point instance from PointContainer with corresponding index. If it is not an integer then function checks if the input is a slice object (ex. mylist[1:10:2]). In that case it returns a copy of PointContainer.

问题是如何将Point,PointContainer实例转换为bp :: objects?

The question is how to convert Point, PointContainer instances to bp::objects?

有关上述课程的一些详细信息

Some details about mentioned classes

class_<Point<int>>("Point")
    .def("__getitem__", get_point_item)
    .def("__setitem__", set_point_item)
    .def("__len__", get_point_size)
    .def("__str__", print_point)
    .def("__eq__", &Point<int>::operator ==)
    .def("__ne__", &Point<int>::operator !=)
    .def("set_x", &Point<int>::set_x)
    .def("get_x", &Point<int>::get_x)
    .def("set_y", &Point<int>::set_y)
    .def("get_y", &Point<int>::get_y)
;

typedef std::vector<Point<int>> PointContainer;
typedef boost::shared_ptr<PointContainer> PointContainerPtr;

class_<PointContainer, PointContainerPtr>("PointContainer")
    .def("__iter__", iterator<PointContainer>())
    .def("__getitem__", get_point)
    .def("__setitem__", set_point)
    .def("__len__", &PointContainer::size)
    .def("append", push_point)
    .def("reserve", &PointContainer::reserve)
    .def("clear", &PointContainer::clear)
;

推荐答案

对于类型已通过boost::python::class_公开的C ++对象,可以使用以下构造函数:

For C++ objects whose type has been exposed via boost::python::class_, one can construct a Python object with an instance of a C++ object using the following constructor:

template <class T>
explicit object(T const& x);

效果:将x转换为python并管理对其的引用.

Effects: converts x to python and manages a reference to it.

抛出: error_already_set,并在无法进行此类转换的情况下设置Python TypeError异常.

Throws: error_already_set and sets a Python TypeError exception if no such conversion is possible.


通过 ,Boost.Python将为C ++类型注册to-python和from-python转换器.使用object()的模板化构造函数时,它将检查内部注册表中的to-python转换器,并在找到时使用它.生成的Python对象将拥有并拥有自己的C ++对象实例.


When a type is exposed via boost::python::class_, Boost.Python will register to-python and from-python converters for the C++ type. When the templated constructor for object() is used, it will check the internal registry for a to-python converter and use it if found. The resulting Python object will have and own its own instance of the C++ object.

这是一个完整的最小示例演示从C ++对象构造boost::python::object:

Here is a complete minimal example demonstrating constructing boost::python::objects from C++ objects:

#include <boost/python.hpp>

// Mockup types.
class spam {};
class egg  {};

// Factory function that returns boost::python::objects.
boost::python::object make_object(std::string name)
{
  namespace python = boost::python;
  if (name == "spam")     return python::object(spam{});
  else if (name == "egg") return python::object(egg{});
  else return python::object();
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;

  // Expose models.
  python::class_<spam>("Spam", python::init<>());
  python::class_<egg>("Egg", python::init<>());

  // Expose factory function.
  python::def("make_object", &make_object);
}

互动用法:

>>> import example
>>> assert(type(example.make_object("spam")) is example.Spam)
>>> assert(type(example.make_object("egg")) is example.Egg)
>>> assert(example.make_object("bogus") is None)


如果需要不同的返回值语义,例如boost::python::object应该引用现有C ++对象而不是副本,则需要提供


If different return value semantics are needed, such as the boost::python::object should have a reference to an existing C++ object rather than a copy, then one needs to provide call policies when wrapping C++ functions.

这篇关于如何将C ++对象转换为boost :: python :: object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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