Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG support for inheritance of static member functions

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?

like image 631
Vahagn Avatar asked Nov 05 '22 11:11

Vahagn


1 Answers

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.

like image 195
Petriborg Avatar answered Nov 12 '22 19:11

Petriborg