SWIG 支持静态成员函数的继承

发布于 2024-09-06 18:18:43 字数 1162 浏览 3 评论 0原文

SWIG 不包装派生类的继承静态函数。该如何解决呢?

这是问题的简单说明。

这是一个简单的 C++ 头文件:

// file test.hpp
#include <iostream>

class B
{
public:
  static void stat()
  { std::cerr << "=== calling static function B::stat" << std::endl; }

  void nonstat() const
  { std::cerr << "==== calling B::nonstat for some object of B" << std::endl; }
};

class D : public B {};

C++ 源文件仅包含头文件:

// file test.cpp
#include "test.hpp"

SWIG 接口文件仅包含 C++ 头文件:

// file test.swig
%module test
%{
#include "test.hpp"
%}

%include "test.hpp"

然后我通过以下方式生成 swig 包装器代码:

swig -c++ -tcl8 -namespace main.swig

然后我通过以下方式创建共享库:

g++ -fpic -Wall -pedantic -fno-strict-aliasing \
               test.cpp test_wrap.cxx -o libtest.so

所以当在 tcl 解释器中加载 libtest.so 并尝试使用包装的接口时,它具有以下行为:

% load libtest.so test
% test::B b
% test::D d
% b nonstat    # works fine
% d nonstat    # works fine
% test::B_stat # works fine
% test::D_stat # DOESN'T WORK !!

所以问题是如何使 SWIG 包装 D:​​:stat?

SWIG doesn't wrap inherited static functions of derived classes. How it can be resolved?

Here is a simple illustration of the problem.

This is a simple C++ header file:

// file test.hpp
#include <iostream>

class B
{
public:
  static void stat()
  { std::cerr << "=== calling static function B::stat" << std::endl; }

  void nonstat() const
  { std::cerr << "==== calling B::nonstat for some object of B" << std::endl; }
};

class D : public B {};

The C++ source file just includes the header file:

// file test.cpp
#include "test.hpp"

The SWIG interface file just includes the C++ header file:

// file test.swig
%module test
%{
#include "test.hpp"
%}

%include "test.hpp"

Then I generate the swig wrapper code by this:

swig -c++ -tcl8 -namespace main.swig

And then I create a shared library by this:

g++ -fpic -Wall -pedantic -fno-strict-aliasing \
               test.cpp test_wrap.cxx -o libtest.so

So when loading libtest.so in a tcl interpretor and trying to use the wrapped interface, it has the following behavior:

% load libtest.so test
% test::B b
% test::D d
% b nonstat    # works fine
% d nonstat    # works fine
% test::B_stat # works fine
% test::D_stat # DOESN'T WORK !!

So the question is how can i make SWIG to wrap D::stat?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2024-09-13 18:18:43

静态函数只在父类B中定义正确吗?如:

D::stat();

不可调用正确吗?这就是为什么 SWIG 不包装函数...

至于如何访问该函数,SWIG 允许您从任何您想要的类中添加/隐藏/包装函数,因此可以“修复” SWIG 类提供对 stat() 的访问。

相信语法是这样的:

%extend D {
   ...
}

自从我接触 SWIG 以来已经有一段时间了,所以我可能记错了一些东西。

The static function is only defined in the parent class B correct? as in:

D::stat();

Is not callable correct? Thats why SWIG doesn't wrap the function...

As to how you can get access to the function, SWIG allows you to add/hide/wrap functions from any class you want to, so it would be possible to "fix" the SWIG class to give access to stat().

Believe the syntax is something like:

%extend D {
   ...
}

Its been a while since I touched SWIG so I might be misremembering something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文