8 May 18:32
Making smart pointers a bit smarter
From: Kenneth Duda <ken.duda <at> gmail.com>
Subject: Making smart pointers a bit smarter
Newsgroups: gmane.comp.programming.swig
Date: 2005-05-08 16:35:13 GMT
Subject: Making smart pointers a bit smarter
Newsgroups: gmane.comp.programming.swig
Date: 2005-05-08 16:35:13 GMT
Hi folks,
SWIG (at least swig -python) has nice support for wrapping C++ smart
pointers. One thing that I feel could be improved is the handling of
NULL smart pointers. When converting a NULL C++ raw pointer to
python, SWIG (correctly, in my view) converts it to None. However,
SWIIG doesn't do anything similar for smart pointers --- it basically
wraps the NULL smart pointer and returns it. A NULL smart pointer
wrapped this way is a booby trap --- any attempt to use it for
anything causes a SEGV.
More specifically, the default typemap results in the following code
to return a smart pointer to python:
{
FooSmartptr * resultptr;
resultptr = new FooSmartptr((FooSmartptr &) result);
resultobj = SWIG_NewPointerObj((void *) resultptr,
SWIGTYPE_p_FooSmartptr, 1);
}
With the following typemap, the behavior can be improved (in my
opinion) so that None is returned if the smart pointer is NULL:
%typemap(out) FooSmartptr {
if( $1.operator->() ) {
FooSmartptr * resultptr;
resultptr = new FooSmartptr( $1 );
$result = SWIG_NewPointerObj( resultptr, SWIGTYPE_p_FooSmartptr, 1 );
} else {
(Continue reading)
RSS Feed