总结
有没有办法(在 C++ 中,而不是在 Python 中)从外部为 Boost::Python 中的类添加函数模板的额外实例化(通过注入(inject)、重新打开定义、注册所需的实例化等)?
背景
给定一个包含函数模板成员的类(不是类模板),我想使用 Boost::Python 生成 Python 绑定(bind)。
但是,由于我正在编写一个库,所以我事先并不知道成员函数将使用哪些模板参数进行调用。这意味着,我不能在 Boost::Python 类定义中列出它们。
例子
假设我们有一个类 TheClass
,它有函数模板(带重载),以及两个测试类 SomeClass
和 OtherClass
,如下所示:
类定义
#include <iostream>
#include <string>
class SomeClass
{
public:
std::string Name()
{
return "SomeClass";
}
};
class OtherClass
{
public:
std::string Name()
{
return "OtherClass";
}
};
class TheClass
{
public:
template <class T>
void Foo (T& arg)
{
std::cout << "Called Foo(" << arg.Name() << ")" << std::endl;
}
template <class T>
void Bar (T& arg, std::string param)
{
std::cout << "Called Bar(" << arg.Name() << ", " << param << ")" << std::endl;
}
template <class T>
void Bar (T& arg, int param)
{
std::cout << "Called Bar(" << arg.Name() << ", " << param << ")" << std::endl;
}
};
然后我使用这段代码将上述所有内容导出到 Python:
boost Python 导出
#include <boost/python.hpp>
#define GENERATE_THE_CLASS_METHODS(classname) \
.def( \
"Foo", \
( void ( TheClass::* )( classname& ))( &TheClass::Foo ), \
( boost::python::arg("arg") ) \
) \
.def( \
"Bar", \
( void ( TheClass::* )( classname&, std::string ))( &TheClass::Bar ), \
( boost::python::arg("arg"), boost::python::arg("param") ) \
) \
.def( \
"Bar", \
( void ( TheClass::* )( classname&, int ))( &TheClass::Bar ), \
( boost::python::arg("arg"), boost::python::arg("param") ) \
)
BOOST_PYTHON_MODULE(my_module)
{
boost::python::class_< TheClass > ( "TheClass" )
GENERATE_THE_CLASS_METHODS(SomeClass)
GENERATE_THE_CLASS_METHODS(OtherClass)
// This is the interesting part: all instantiations of the function
// templates have to be inserted here. How can this be avoided
// so that new classes can also be used?
;
boost::python::class_< SomeClass > ( "SomeClass" );
boost::python::class_< OtherClass > ( "OtherClass" );
}
(附带问题:我在这里使用宏是为了避免出于维护原因的重复代码。是否有更漂亮的 C++ 式实现方法?)
Python 测试脚本
上面的代码使用 Clang 和 C++11、Boost 1.57.0 和 Python 2.7.6 进行编译。它适用于此测试脚本:
#!/usr/bin/python
from my_module import *
s = SomeClass()
o = OtherClass()
t = TheClass()
t.Foo(s)
t.Foo(o)
t.Bar(s, 42)
t.Bar(o, 42)
t.Bar(s, "Hello World")
t.Bar(o, "Hello World")
产生这个结果:
Called Foo(SomeClass)
Called Foo(OtherClass)
Called Bar(SomeClass, 42)
Called Bar(OtherClass, 42)
Called Bar(SomeClass, Hello World)
Called Bar(OtherClass, Hello World)
问题
在示例中,Foo() 和 Bar() 函数模板的实例是在 Boost::Python 类定义中创建的(请参阅源代码中的注释)。这意味着,库的用户在不修改这段代码的情况下无法添加新实例。
因此,我正在寻找的是一种方法
- 从 Boost::Python 类定义的外部“注入(inject)”那些实例
- 以某种方式重新打开定义
- 在调用 Boost::Python 类定义之前在某处注册所需的实例
最后,图书馆的用户应该能够做这样的事情:
class AnotherClass
{
public:
std::string Name()
{
return "AnotherClass";
}
};
add_to_the_class(AnotherClass);
// or
add_to_class_definition<AnotherClass>("TheClass");
// or whatever works...
这有可能吗?还有其他方法可以实现类似的目标吗?
最佳答案
一段时间后,我找到了解决方案,并认为其他人也可能会感兴趣。
解决方案
其实很简单:boost::python::class_
定义返回(当然)类型为 boost::python::class_< TheClass >
的类实例.这可以被存储,所以我们可以稍后向它添加成员定义:
static auto the_class_ = boost::python::class_< TheClass > ( "TheClass" )
// add some .def(...) and so on here, if needed
;
template <class T>
void add_to_the_class()
{
the_class_
.def(
"Foo",
( void ( TheClass::* )( T& ))( &TheClass::Foo ),
( boost::python::arg("arg") )
)
.def(
"Bar",
( void ( TheClass::* )( T&, std::string ))( &TheClass::Bar ),
( boost::python::arg("arg"), boost::python::arg("param") )
)
.def(
"Bar",
( void ( TheClass::* )( T&, int ))( &TheClass::Bar ),
( boost::python::arg("arg"), boost::python::arg("param") )
)
;
}
现在我们可以根据需要从外部向这个定义添加任意多的额外重载:
add_to_the_class<AnotherClass>();
这也摆脱了丑陋的宏。
一些额外的见解
这一切都有效,因为与 Python 的实际绑定(bind)是在运行时创建的。在您的 boost::python 绑定(bind)代码中,您实际上只是定义了一些类和函数——它们在 main() 之前调用以启动您的 C++ 代码和 Python 解释器之间的实际连接。
我认为,这在 boost::python 帮助中没有很好的记录。我花了一段时间才弄明白这一点。一开始,我认为 boost::python 以某种方式(通过一些模板魔术)在编译期间生成了绑定(bind)。如果这是真的,上面的解决方案将不起作用。事后看来,这对我来说已经很清楚了。
此外,这种洞察力(绑定(bind)是在运行时完成的,使用您提供的定义)也让我想到了编写一个静态寄存器类来收集要导出到 Python 的类。这将有助于我的图书馆更进一步,因为我不必扩展主要的 BOOST_PYTHON_MODULE
每个新类的定义,而是简单地将类注册到静态寄存器(可能再次使用一些宏......)。但这是 future 的工作......现在问题已经解决了!
关于python - 带有函数模板的 Boost::Python 类:如何从外部添加实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30463563/