So, I am playing with Python, C ++ 0x and SWIG 2.0. I have a headline that looks like this:
#include <string>
#include <iostream>
#include <memory>
using namespace std;
struct Base {
virtual string name();
int foo;
shared_ptr<Base> mine;
Base(int);
virtual ~Base() {}
virtual void doit(shared_ptr<Base> b) {
cout << name() << " doing it to " << b->name() << endl;
mine = b;
}
virtual shared_ptr<Base> getit() {
return mine;
}
};
struct Derived : Base {
virtual string name();
int bar;
Derived(int, int);
};
Meanwhile, the interface file is as follows:
%module(directors="1") foo
%feature("director");
%include <std_string.i>
%include <std_shared_ptr.i>
%shared_ptr(Base)
%shared_ptr(Derived)
%{
#define SWIG_FILE_WITH_INIT
#include "foo.hpp"
%}
%include "foo.hpp"
My Python session is as follows:
>>> import foo
>>> b = foo.Base(42)
>>> d = foo.Derived(23,64)
>>> b.doit(d)
Base doing it to Derived
>>> g = b.getit()
>>> g
<foo.Base; proxy of <Swig Object of type 'std::shared_ptr< Base > *' at 0x7f7bd1391930> >
>>> d
<foo.Derived; proxy of <Swig Object of type 'std::shared_ptr< Derived > *' at 0x7f7bd137ce10> >
>>> d == g
False
>>> d is g
False
>>> d.foo == g.foo
True
>>> d.bar
64
>>> g.bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Base' object has no attribute 'bar'
I cannot figure out how to get the “original” proxy object here. Should I create a function for each base class to execute dynamic_pointer_cast? And if so, what of the Director subclasses are implemented in Python?
I feel that there is a switch or function that I can enable here to make SWIG execute the necessary lookup tables and produce the object that I want, but I have not found it yet.
(: , raw- , , SWIG dynamic_cast)
Update
( , ++, ) SWIG, SIP - Python?
# 2
SIP4 , , , . , . - SWIG, , :
shared_ptr, raw-? , enable_shared_from_this .
SIP4 ( Makefile distutils), Makefile?