Boost::Python class with function templates: 如何从外部添加实例?

Boost::Python class with function templates: How to add instances from the outside?

总结

有没有办法(在 C++ 中,而不是在 Python 中)从外部为 Boost::Python 中的 class 添加函数模板的额外实例化(通过注入,重新打开定义、注册所需的实例等)?

背景

给定一个 class(不是 class 模板)包含函数模板成员,我想使用 Boost::Python.[=20= 生成 Python 绑定]

但是,由于我正在编写一个库,所以我事先并不知道成员函数将使用哪些模板参数进行调用。这意味着,我无法在 Boost::Python class 定义中列出它们。

例子

假设我们有一个 class TheClass 有函数模板(有重载),和两个测试 classes SomeClassOtherClass这个:

Class 定义

#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:

提升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 class 定义中创建的(参见源代码中的注释)。这意味着,库的用户在不修改这段代码的情况下无法添加新实例。

因此,我正在寻找的是一种方法

最后,图书馆的用户应该能够做这样的事情:

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_ 定义 returns(当然)一个 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 的实际绑定是在运行时创建的。在您的 boost::python 绑定代码中,您实际上只是定义了一些 classes 和函数——它们在 main() 之前调用以启动 C++ 代码与 Python 解释器之间的实际连接。

我想,这在 boost::python 帮助中没有很好的记录。我花了一段时间才弄明白这一点。一开始,我认为 boost::python 以某种方式(通过一些模板魔术)在编译期间生成了绑定。如果这是真的,上面的解决方案将不起作用。事后看来,这对我来说已经很清楚了。

此外,这种见解(绑定是在运行时完成的,使用您提供的定义)也让我想到了编写一个静态寄存器 class 来收集要导出到 classes =36=]。这将进一步帮助我的库方法,因为我不必为每个新的 class 扩展主 BOOST_PYTHON_MODULE 定义,而是简单地将 class 注册到静态寄存器(可能使用又是一些宏……)。但这是未来的工作......现在问题已解决!