python - Swig 从 Base* 向下转型到 Derived*

标签 python c++ swig downcast

我有以下 C++ 类(简化的),我使用 SWIG 将它们暴露给 Python:

struct Component
{
    virtual void update();
}

struct DerivedComponent : public Component
{
    void update() { cout << "DerivedComponent::update()" << endl; }
    void speak() { cout << "DerivedComponent::speak()" << endl; }
}

class Entity
{
public:
    Component* component(const std::string& class_name)
    {
        return m_components[class_name];
    }

private:
    std::unordered_map<std::string, Component*> m_components;
}

现在,在 Python 中我可以成功调用 component("DerivedComponent").update()在实体实例上。但是,我不能调用 component("DerivedComponent").speak()因为 component("DerivedComponent") 返回的类型报告为 <class 'module.Component'> .

我显然需要向下转换 component() 的结果函数以调用 DerivedComponent 中定义的方法.我曾希望 Swig 会像我相信 Boost.Python 那样执行自动向下转型。

如果没有在 C++ 中定义一大堆类型转换函数并将它们暴露给 Python,是否有更好的解决方案来使用 Swig 或 Python 进行向下转换?我有哪些选择?

最佳答案

你可以在 Python 中做你想做的事,只需要一点点工作。它可以如您所愿地工作,因为在 Python 中向下转换有点毫无意义,因为函数的返回类型(或一般类型)不是强类型的,因此我们可以将您的 Entity::component 函数修改为 always返回最派生的类型,不管它是什么。

要使其与您的 C++/Python 绑定(bind)一起使用,您需要为 Entity::component 编写一个“输出”类型映射。我已经写了一个例子来说明它是如何工作的。在这种情况下,我们必须稍微避开它,因为知道将其向下转换为什么的唯一方法来自函数的参数。 (例如,如果您的基类有一个方法将其作为字符串/枚举返回,您可以进一步简化它,而不依赖于输入参数)。

%module test

%{
#include "test.hh"
%}

%include <std_string.i>

%typemap(out) Component * Entity::component {
    const std::string lookup_typename = *arg2 + " *";
    swig_type_info * const outtype = SWIG_TypeQuery(lookup_typename.c_str());
    $result = SWIG_NewPointerObj(SWIG_as_voidptr($1), outtype, $owner);
}

%include "test.hh"

这使用 SWIG_TypeQuery 函数要求 Python 运行时根据 arg2(对于您的示例是字符串)查找类型。

我必须对您的示例 header (在我的示例中名为 test.hh)进行一些更改以解决一些问题,然后才能将其变成一个完整的工作演示,它最终看起来像:

#include <iostream>
#include <map>
#include <string>

struct Component
{
    virtual void update() = 0;
    virtual ~Component() {}
};

struct DerivedComponent : public Component
{
    void update() { std::cout << "DerivedComponent::update()" << std::endl; }
    void speak() { std::cout << "DerivedComponent::speak()" << std::endl; }
};

class Entity
{
public:
    Entity() {
       m_components["DerivedComponent"] = new DerivedComponent;
    }

    Component* component(const std::string& class_name)
    {
        return m_components[class_name];
    }

private:
    std::map<std::string, Component*> m_components;
};

然后我构建了它:

swig -py3 -c++ -python -Wall test.i
g++ -Wall -Wextra test_wrap.cxx -I/usr/include/python3.4/ -lpython3.4m -shared -o _test.so

有了这个,我就可以运行以下 Python:

from test import *

e=Entity()
print(e)

c=e.component("DerivedComponent")
print(c)
print(type(c))

c.update()
c.speak()

这如您所愿:

<test.Entity; proxy of <Swig Object of type 'Entity *' at 0xb7230458> >
Name is: DerivedComponent *, type is: 0xb77661d8
<test.DerivedComponent; proxy of <Swig Object of type 'DerivedComponent *' at 0xb72575d8> >
<class 'test.DerivedComponent'>
DerivedComponent::update()
DerivedComponent::speak()

关于python - Swig 从 Base* 向下转型到 Derived*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392602/

相关文章:

c++ - 是否可以使用 curlpp 使用 HTTP 字节范围

c++ - 如何获取源代码(C++)深处的变量值? (例如 haar.cpp、OpenCV 中 stage_sum 的值)

php - 使用 SWIG 将 C++ std::map 类型转换为 PHP

c++ - 在 C 中跟踪 Lua 表

python - 从公司名称中获取后缀列表

python - 如何在python中将两个数据帧一个带值另一个带 bool 值变成一个数据帧?

c++ - 带有包含和源路径的 CMake - 基本设置

c++ - 如何处理我的 C++ __getitem__ 函数中的切片(由 SWIG 使用)

python - 在 post_save 上添加 M2M

python - Django、Python 模块和 Git 子模块