SWIG support for inheriting static member functions

SWIG does not carry inherited static functions of derived classes. How can this be solved?

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 includes only the header file:

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

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

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

Then I create the swig shell code as follows:

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

And then I create a shared library as follows:

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

Therefore, when loading libtest.so in the tcl interpreter and trying to use a 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 to get SWIG to wrap D :: stat?

+4
source share
1 answer

Static function defined only in parent class B correct? how in:

 D::stat(); 

Can't be right? This is why SWIG does not end the function ...

Regarding how you can access this function, SWIG allows you to add / hide / transfer functions from any class you want, so it would be possible to β€œfix” the SWIG class to provide access to stat() .

Believe that the syntax looks something like this:

 %extend D { ... } 

It has been some time since I touched SWIG so that I could forget something.

+1
source

Source: https://habr.com/ru/post/1313275/


All Articles