使用其他类的数组作为参数公开构造函数

时间:2014-01-28 15:03:29

标签: c++ boost-python

我有两个不同的类,我想使用boost-python公开,但第二个的构造函数接受第一个的数组作为参数,我无法弄清楚如何去做。

这是类的定义:

class INT96{
public:
    uint64_t value[3];

    INT96(){};
    INT96(uint64_t x0, uint64_t x1, uint64_t x2);
    ...
};

template <unsigned k>
class Xi_CW{
    protected:
        INT96 A[k];

    public:
        Xi_CW(INT96 (&A)[k]);
        ...
};

我尝试使用boost-python公开它们:

using namespace boost::python;    
typedef Xi_CW<4> Xi_CW4;

BOOST_PYTHON_MODULE(xis)
{
    class_<INT96>("INT96", init<double,double,double>())
        [...]
    ;
    class_<Xi_CW4>("Xi_CW4", init<INT96[4]>())
        [...]
    ;
}

这会导致“未知转换错误”。我尝试了其他几种可能性,但到目前为止还没有运气......

知道我应该怎么做? 谢谢

2 个答案:

答案 0 :(得分:0)

我相信您可以使用boost :: python :: list为您的构造函数接收int96数组,然后根据需要将其转换为常规c ++数组。

以下是我发现将python iterables转换为c ++向量的示例:https://stackoverflow.com/a/19092051/996197

答案 1 :(得分:0)

一种解决方案是禁止Boost.Python默认初始化程序并注册一个可以从包含Xi_CW<4>个对象的可迭代Python对象构造INT96对象的custon初始化程序。

  • 通过boost::python::no_init公开课程时,init_specboost::python::class_,可以取消默认初始值设定项。
  • boost::python::make_constructor()包裹的辅助功能可以作为__init__方法在类中公开。

以下是基于原始问题中提供的类型的完整示例:

#include <iostream>
#include <boost/foreach.hpp>
#include <boost/python.hpp>

/// @brief Mockup spam class.
struct spam
{
  int value[3];
  spam() {};
  spam(int x, int y, int z)
  {
    value[0] = x;
    value[1] = y;
    value[2] = z;
  }

};

/// @brief Mockup egg class.
template <std::size_t N>
class egg
{
public:

  explicit egg(spam (&spams)[N]) : spams_(spams) {}

  /// @brief Return string reprenstation of the egg class.
  std::string to_string()
  {
    std::stringstream stream;
    stream << "[";
    BOOST_FOREACH(spam& s, spams_)
    {
      stream << "[" << s.value[0] << ", " 
                    << s.value[1] << ", "
                    << s.value[2]
             << "]";
    }
    stream << "]";
    return stream.str();
  }

private:
  spam spams_[N];
};

/// @brief Auxiliary function that will attempt to create an egg<N> type
///        from an iterable Python object that contains spam objects.
template <std::size_t N>
egg<N>* make_egg(boost::python::object object)
{
  spam spams[N];
  // Iterate over the python object, extracting and copying spam objects.
  // Boost.Python will handle throwing exceptions for the appropriate cases:
  // - object does not support indexing (__getitem__)
  // - object[N-1] is out of range
  // - object[i] does not contain or cannot be converted to a spam&
  for (long i = 0; i < N; ++i)
    spams[i] = boost::python::extract<spam&>(object[i]);

  // Construct egg from the local spam array.  Ownership of the object is
  // passed to Boost.Python
  return new egg<N>(spams);
}

/// @brief Expose an egg<N> type to Python.
///
/// @param name The name that will be exposed to Python.
template <std::size_t N>
boost::python::class_<egg<N> > expose_egg(const char* name)
{
  namespace python = boost::python;

  // Explicitly suppress the Boost.Python default constructor.
  typedef egg<N> egg_type;
  python::class_<egg_type> egg_class(name, python::no_init);

  // Register a custom factory function as the constructor method.
  egg_class
    .def("__init__", python::make_constructor(&make_egg<N>))
    .def("__str__", &egg_type::to_string)
    ;

  return egg_class;
}

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

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

  // Expose different egg types.
  expose_egg<2>("Egg2");
  expose_egg<4>("Egg4");
}

交互式使用:

>>> import example
>>> spams = [
...   example.Spam(0, 1, 2),
...   example.Spam(1, 2, 3),
...   example.Spam(2, 3, 4),
...   example.Spam(3, 4, 5)
... ]
>>> print example.Egg2(spams)
[[0, 1, 2][1, 2, 3]]
>>> print example.Egg4(tuple(spams)) # different type
[[0, 1, 2][1, 2, 3][2, 3, 4][3, 4, 5]]
>>> print example.Egg4(spams[:1]) # expect IndexError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> print example.Egg4(42) # expect TypeError (42[0] is invalid)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'
>>> print example.Egg4([42]) # expect TypeError (conversion)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No registered converter was able to extract a C++ reference to
           type spam from this Python object of type int