From: William S F. <ws...@fu...> - 2006-03-30 22:35:43
|
Steven Sharp wrote: > While searching the lists for info on using std::auto_ptr and > reference counted pointers, I came across wsf's typemap for auto_ptr. > I extended for Java directors thus: > > //%define SWIG_AUTO_PTR_TYPEMAPS(PROXYCLASS, TYPE...) > %define SWIG_AUTO_PTR_TYPEMAPS(PROXYCLASS, TYPE, PROXYPKG...) > . > . > . > %typemap(directorin,descriptor="PROXYPKG/PROXYCLASS;") std::auto_ptr< TYPE > %{ > jlong lpp = 0; > *(TYPE**)(void *) &lpp = $1.release(); > $result = lpp; > %} > %typemap(javadirectorin) std::auto_ptr< TYPE > { > long cPtr = $jniinput; > return (cPtr == 0) ? null : new PROXYCLASS(cPtr, true); > } > %typemap(javadirectorout) std::auto_ptr< TYPE > > "PROXYCLASS.getCAutoPtr($javacall)" > > This *seems* to work as I haven't had a segfault yet. Is there any > way to concatenate things in the descriptor of the directorin? I was > hoping to use "LPROXYPKG" but obviously that won't work. The > work-around is that I'm passing "Lfoo" for the package. What is the > significance of "..." after TYPE? I'm using them on PROXYPKG but > without a good knowledge of how I may be borking the code. > > Also, the code I'm working with is rife with reference counted "smart" > pointers. I was having problems with the reference going out of scope > and deleting the referenced object. The smart pointer is passed into > a director method as a reference, which is then turned into a pointer. > I hand modified this code to copy construct a new smart pointer and > pass that as the CPtr instead. It seems to be working but I haven't > done a memory leak check yet. It looks like when the Java object is > GCed that the smart pointer's reference count is decremented as well. > > Is there an easier way to do this? A way to tell swig/directors that > it should copy an parameter object instead of creating a pointer to > it? Some flag I've missed? > I don't have time to look at this in detail, but I can't see anything obviously wrong with it. The %define is just like a C #define with main difference that it is a multiline define. The macro supports varargs, thus the "...". This is useful for when the C++ type contains commas such as in templates. So you can use the macro, eg: SWIG_AUTO_PTR_TYPEMAPS(VectorPairDouble, vector<pair<double, double> >) so you need to change your definition by swapping TYPE and PROXYPKG. With regard to concatenating strings in the descriptor, use normal macro macro concatenation like you would in a C macro. BTW the extra (void *) cast has been removed from all SWIG typemaps as it suppresses a useful aliasing warning in some versions of gcc, see CHANGES files. William |