[PyQt] %MappedType with non-class template arg?

Hans Meine meine at informatik.uni-hamburg.de
Fri Sep 10 08:47:20 BST 2010


Hi Phil,

Am Donnerstag 09 September 2010, 16:35:00 schrieb Phil Thompson:
> <meine at informatik.uni-hamburg.de> wrote:
> > I was happy when I saw that %MappedType supports templates, but then I
> > realized that I could not use it for my multi-dimensional array type [1]
> > since
> > it does not support non-class arguments (integer dimension in my case).
> > 
> > Would that be hard to fix?
> 
> It does support non-class arguments.

That sounds great, but...

> > [1] http://tinyurl.com/NumpyArray is what I am dealing with
> 
> I don't see the problem with this. Compare with PyQt's QPair<TYPE, int>
> where TYPE is a class.

I am not sure I see the connection.  QPair does take two types, one of them 
being int here.  Maybe "non-class argument" was a little bit misleading, I was 
thinking of

  template<class value_type> ...

which is a synonym (don't we all love ambiguity) for

  template<typename value_type>

in contrast to

  template<unsigned int DIMENSION> class FixedSizeVector ...

Of course, 'int' is a 'class' (/'typename') in the above template-arg sense 
and is supported by SIP, but I am interested in this exact use case:

  typedef NumpyArray<4, vigra::UInt16> UInt16Array4D;

I have attached a corresponding SIP file that does not work (SIP gives a 
syntax error at the above line), which you may try yourself with VIGRA [1].

Thanks for the reply,
  Hans

[1] NumpyArray is new in VIGRA 1.7.0; your distribution might come with an
    older version: http://hci.iwr.uni-heidelberg.de/vigra/
-------------- next part --------------
template<N, T>
%MappedType NumpyArray<N, T>
{
%TypeHeaderCode
// contains
//   #include <vigra/numpy_array.hxx>
// plus the same typedefs as at the bottom of this file:
#include "python_arrays.hxx"
%End

%ConvertToTypeCode
    if(sipIsErr == NULL)
        return NumpyArray<N, T>::isReferenceCompatible(sipPy);

    NumpyArray<N, T> *array = new NumpyArray<N, T>();
    bool ok = array->makeReference(sipPy);

    if(!ok)
    {
        // FIXME: set up proper exception
//        *sipIsErr = true;
        delete array;
        return 0; // there is no temporary instance
    }

    *sipCppPtr = array;

    // not really sure about this one; seems to be SIP
    // boilerplate-code:
    return sipGetState(sipTransferObj);
%End

%ConvertFromTypeCode
    PyObject *result = sipCpp->pyObject();
    if(!result)
    {
        if(!PyErr_Occurred())
            PyErr_SetString(PyExc_ValueError,
                            "Cannot convert invalid PythonArray to ndarray");
        return NULL;
    }
    Py_INCREF(result); // FIXME: what about sipTransferObj?
    return result;
%End
};

typedef NumpyArray<4, vigra::UInt16> UInt16Array4D;
typedef NumpyArray<2, vigra::UInt32> UInt32Array2D;


More information about the PyQt mailing list