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

Exposing constructor with an array of other class as argument

本文关键字:参数 构造函数 其他 数组      更新时间:2023-10-16

我有两个不同的类,我想使用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]>())
[...]
;
}

这会导致"未知的转换错误"。我试过其他几种可能性,但到目前为止运气不好。。。

知道我该怎么做吗?感谢

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

下面是我发现的一个将python可迭代项转换为c++向量的示例:https://stackoverflow.com/a/19092051/996197

一个解决方案是取消Boost.Python默认初始化器,并注册一个custon初始化器,该初始化器可以从包含INT96对象的可迭代Python对象构造Xi_CW<4>对象。

  • 通过boost::python::class_公开类时,可以通过提供boost::python::no_init作为init_spec来抑制默认初始值设定项
  • 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