How to access c++ object methods from inside vector with SWIG Python -
i have 2 c++ classes: foo , bar. constructor foo looks this:
foo(std::vector<bar *> * bars);
the constructor bar , 1 of member functions following:
bar(int data) int getdata()
in interface file have:
%module myswig %{ #include "foo.h" #include "bar.h" #include <vector> %} %include "foo.h" %include "bar.h" %include "std_vector.i" namespace std { %template(vectorofbars) vector<bar *>; }
so in python following:
import myswig mybar = myswig.bar(5)
and need create std::vector<bar *> *
object pass foo constructor, try following:
vector = myswig.vectorofbars() vector.push_back(mybar)
to test if successful try:
print vector print vector[0] print "data: vector[0].getdata()
if result of third print out still "5" successful, instead i'm assuming pointer value instead
<myswig.vectorofbars; proxy of <swig object of type 'std::vector< bar *,std::allocator< bar * > > *' @ 0xb6a9c4d0> > <myswig.bar; proxy of <swig object of type 'std::vector< bar * >::value_type' @ 0xb6a9c3b0> > 3069958048
what doing wrong? how can create vector of bar pointer objects need pass in make foo object? why getting pointer value instead of actual value?
i figured out! problem type <myswig.bar; proxy of <swig object of type 'std::vector< bar * >::value_type' @ 0xb6a9c3b0> >
translates bar *
, not bar
object. swig handles pointers in case didn't when called method on bar *
object gave me bogus data.
in reality needed pass foo constructor. mistaken in thinking swig automatically dereference pointer bar access data
attribute. when changed template declaration
%template(vectorofbars) vector<bar *>;
to line
%template(vectorofbars) vector<bar>;
then able access data
incorrect passing foo's constructor. had right along.
Comments
Post a Comment