如何使用boost::python将模板化的类作为一个类公开给python
How to expose a templated class as one class to python with boost::python
在很多关于boost::python的例子中,你会看到:
using namespace boost::python;
typedef class_<std::vector<float>> VectorFloat;
当然,如果你需要一个double
向量你会有第二个类,叫做DoubleVector
。在我看来,这不是很"蟒"。如果c++中的一个(模板化的)类实际上是python中的一个类,它会更直观(我认为),它接受像, ..., type='float', ... ,
这样的参数。这样,类也只在pydocs中出现一次,并且只需要在boost::python module
中添加一次。
假设我们有一个简单的c++模板类:
template <typename T>
MyClass
{
T val;
public:
MyClass(const T& tVal) : val(tVal) {}
T getVal() { return val; }
void setVal(const T& tVal) { val = tVal; }
};
现在我们要为Python编写一个接口。我的想法是:
使用boost::变异
typedef boost::variant<MyClass<float>, MyClass<double>> VariantClass;
class MyPythonClass
{
VariantClass vClass;
public:
MyPythonClass(
const PyObject& tVal,
const boost::python::str& type)
{
using namespace boost::python;
std::string type_string = extract<std::string>(type);
if( type_string == "float" )
{
float f = extract<float>(tVal);
vClass = MyClass(f);
}
else if( type_string == "double" )
{
double d = extract<double>(tVal);
vClass = MyClass(d);
}
}
boost::python::PyObject* getVal()
{
// What to put here?
}
void setVal(const boost::python::PyObject& tVal)
{
//What to put here?
}
};
BOOST_PYTHON_MODULE(my_module)
{
class_<MyPythonClass>("MyClass", init<boost::python::PyObject, int, boost::python::str>).def("getVal", &MyClass::getVal());
}
这个解决方案的一个明显缺点是我猜boost::variant可以处理完全不同的类型,而我的类除了它们存储的数据类型之外几乎是相同的。因此,可能有更多的信息被抽象掉了。
所以我想这个问题可以归结为例子中的两个空函数。然而,更简洁、更短或更少的"if"链式答案当然也会被接受。正如标题所说,它是关于模板化类和boost::python,而不一定是boost::variant。
这里有几个令人困惑的地方。首先,在Python中,object
基本上已经是一个变体——一切都是object
。在c++中,类模板不是类型——它是创建类型的方法。所以如果你想把MyClass<T>
从c++暴露给Python,你应该暴露所有的MyClass<T>s
我会写一个函数模板来做绑定:
template <class T>
void bind_MyClass(const char* name) {
class_<MyClass<T>>(name, init<T const&>())
.add_property("val", &MyClass<T>::getVal, &MyClass<T>::setVal);
;
}
然后命名为:
bind_MyClass<float>("MyClassFloat");
bind_MyClass<double>("MyClassDouble");
相关文章:
- 如何在C++中使用pybind11加载一个pickle python列表
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 我想通过带有C++和Python的插件创建一个可扩展的应用程序
- 有没有一个 c++ gmp 库函数与 python gmpy2 库 divm(..) 函数相同?
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 如何构建一个 setup.py 来编译C++使用 Python、pybind11 和 Mingw-w64 的扩展?
- 在 c++ 中创建一个类似 python 的列表
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 如何制作一个满足SWIG中接口的python类
- 在使用PyInstaller冻结Python脚本时包含一个C++可执行文件
- 加快一个长python代码的速度,因为只有一个块,所以速度很慢
- 如何创建一个接口,允许我访问C++中的按钮(和其他ui)函数,该函数是使用python中的MFC实现的
- OpenCV - 打开一个从C++到Python的XML文件
- 与 Python 中子进程中的另一个应用程序的交互式会话
- 如何使用c ++代码来调用和运行一个现有的python文件?
- SWIG:传递一个 std::vector< std::vector <double> >指向 python 的指针
- 将多个C 类包装成一个Python类
- 调试从 Linux 中的另一个C++应用程序启动的 python 脚本
- C++中是否有一个功能可以创建一个像Python的"numpy.bmat"一样简单的块矩阵?
- 使用一个 C 程序运行 python 脚本(不同的 python 版本)