From: David F. <dav...@gm...> - 2011-04-09 09:16:51
|
> > You've marked A as a director and in doing so SWIG expects virtual methods > in A. As there aren't any, it doesn't treat A as a director. As the > allprotected mode is for director classes, your protected members are not > wrapped as A is not a director. You'll have to add in at least one virtual > method to A or a virtual destructor will also work. > After adding a virtual method, it works fine. If it can help someone, below is the test case, working with Swig 1.3.36. Thank again, Best, David ================================================================================ test_abc.py ================================================================================ #!/usr/bin/env python import ab class C(ab.A): def __init__(self): ab.A.__init__(self) def bar(self): self._b.foo() def baz(self): print "hello from C.baz()" c = C() c.setb(ab.B()) c.bar() c.baz() ================================================================================ test_abc.cpp ================================================================================ #include "c.hpp" int main(){ C c; c.setb(new B); c.bar(); c.baz(); } ================================================================================ a.cpp ================================================================================ #include "a.hpp" A::A(){} void A::setb(B* b){ _b = b; } ================================================================================ a.hpp ================================================================================ #ifndef __A_HPP_INCLUDED__ #define __A_HPP_INCLUDED__ #include "b.hpp" class A{ public: A(); void setb(B* b); protected: virtual void baz() = 0; B* _b; }; #endif ================================================================================ b.cpp ================================================================================ #include "b.hpp" B::B(){} void B::foo(){ cout << "hello from B::foo()" << endl; } ================================================================================ b.hpp ================================================================================ #ifndef __B__HPP_INCLUDED__ #define __B__HPP_INCLUDED__ #include <iostream> using namespace std; class B{ public: B(); void foo(); }; #endif ================================================================================ c.cpp ================================================================================ #include "c.hpp" C::C():A(){} void C::bar(){ _b->foo(); } void C::baz(){ cout << "hello from C::baz()" << endl; } ================================================================================ c.hpp ================================================================================ #ifndef __C_HPP_INCLUDED__ #define __C_HPP_INCLUDED__ #include "a.hpp" class C: public A{ public: C(); void bar(); void baz(); }; #endif ================================================================================ ab.i ================================================================================ %module(directors="1",allprotected="1") ab %{ #define SWIG_FILE_WITH_INIT #include "a.hpp" #include "b.hpp" %} %feature("director") A; %include "a.hpp" %include "b.hpp" ================================================================================ Makefile ================================================================================ CC=g++ default:: allpy clean: cleancpp cleanpy #=== cpp === allcpp: clean buildcpp testcpp buildcpp: test_abc.o a.o b.o c.o $(CC) -o test_abc.x $^ %.o: %.cpp $(CC) -c $< testcpp: buildcpp ./test_abc.x cleancpp: rm -f *.o *.x #=== py === allpy: clean buildpy testpy buildpy: python setup.py build testpy: python test_abc.py cleanpy: rm -rf build/ *.pyc *_wrap.* *.so ab.py ================================================================================ setup.py ================================================================================ #!/usr/bin/env python from distutils.core import setup, Extension ab_module = Extension( '_ab', sources = ['ab.i','a.cpp','b.cpp'], swig_opts = ['-c++',], ) setup (name = 'ab', ext_modules = [ab_module], py_modules = ['ab'], ) ================================================================================ setup.cfg ================================================================================ [build_ext] inplace=1 |