[PyQt] Templated classes with specialization in SIP

Nate Reid gnatty7 at hotmail.com
Tue Oct 5 18:13:26 BST 2010


There may be syntax/programming errors here, but I was wondering if by 
specifying something like the following:



// blah.h

#ifndef BLAH_DEFINED

#define BLAH_DEFINED

#include <sstream>

template<typename T>

class Blah {

public:

   QString get(const T& v);

};



template<typename T>

QString Blah<T>::get(const T& v) {

   std::ostringstream s;

   s << "Value is: " << v;

   return QString(s.str().c_str());

}



// Specialization for 'int'

template<>

QString Blah<int>::get(const int& v) {

   std::ostringstream s;

   s << "Value (int) is: " << v;

   return QString(s.str().c_str());

}

#endif



// blah.sip

%Module blah 0



template <typename T>

class Blah {

%TypeHeaderCode

#include "blah.h"

%End

public:

   QString __getitem__(const T& val);

%MethodCode

   sipRes = new QString(sipCpp->get(*a0));

%End

};





You could produce a wrapped class that would work like follows:



>>> import blah

>>> b = blah.Blah()

>>> str(b['hello'])

Value is: hello

>>> str(b[3.2])

Value is: 3.2

>>> str(b[10])

Value (int) is: 10



It seems like without explicit typedefs, SIP won't know how to generate 
the templated wrapped Blah.  But, is there a preferred way to get the 
above interface with minimal code without necessarily resorting to using 
%MappedTypes?



Thanks,

-Nate
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101005/4f161af4/attachment-0001.html>


More information about the PyQt mailing list