logo       

SWIG, std::list and Python: msg#00044

programming.swig

Subject: SWIG, std::list and Python

Hi,

I've been trying to wrap a (simplified verson of) std::list in conjunction with a larger wrapping
exercise. (I already managed to do the same for Java; I think SWIG is a great piece of SW !)

The main problem seems to be the "double indirection" caused by the reference parameter in
std::list - the elements of my list are pointers to a class. To isolate the problem I have heavily
simplified my interface file, and and extract of its main contents looks like following:


--- clip clip clip ---

// Reduce one level of indirection (_p_p_HASLIB__MOImpl should become _p_HASLIB__MOImpl)
// $*1_descriptor should do that
%typemap(in) HASLIB::MOImplPointer&
         "if ((SWIG_ConvertPtr($input,(void **) &$1, $*1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail;
          if ($1 == NULL) { PyErr_SetString(PyExc_TypeError,\"null reference\"); SWIG_fail; }"

// std::list
namespace std {
   template<class T> class list {
   public:
       typedef int size_type;

    // Heavily reduced for simplicity
    list();
    void push_front(const T& val);
   };
};

%template(MOImplList) std::list<HASLIB::MOImplPointer>;

--- clip clip clip ---


The actual class, HASLIB::MOImpl is now reduced to a strict minimum (just a constructor and adestructor).

MOImplPointer is a typedef in the c++ header file:
    typedef MOImpl* MOImplPointer;

The actual problem appears to be that SWIG somehow fails to recognize that MOImplPointer really is a pointer (?).
The trace from a test program trying to push an element into the list is below:

.....

Traceback (most recent call last):
  File "main.py", line 16, in ?
    moImpls.push_front(myMO);
  File "/home/enurmi/Devlmt/Python/HASLIB_BUG/HASLIB_BUG.py", line 29, in push_front
    def push_front(*args): return apply(_HASLIB_BUG.MOImplList_push_front,args)
TypeError: Type error. Expected _HASLIB__MOImplPointer

....


I believe SWIG should have been expecting a _p__HASLIB__MOImpl instead ??

Originally, the typedef was
    typedef MOImpl* MOImplPtr;


At one time, I was suspecting that this was somehow confused with the <ClassName>Ptr classes that SWIG
appears to generate for Python (and that appear pretty much undocumented ;;:- ?). Nevertheless, that appeared
not to be the cause.

Any help would be appreciated.

Is the very idea, that a typemap is necessary to handle the "double indirection" just a misconception ?
If no, would there be a more generic way to handle this with ""SWIGTYPE"" style typemaps, instead of resorting to
specific typemaps ?


Br,
Esa










<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise