[PyQt] Using Sip on template classes

Guðjón Guðjónsson gudjon.i.gudjonsson at gmail.com
Sat May 2 18:33:46 BST 2015


Hi David

Thanks a lot for your help.

The solution is the following

/*Header file*/
#include <string>
using namespace std;

template <typename T>
class TempSip {
    T var;
public:
    TempSip(T v);
    void Print();
};

/*Source file*/
#include "tempsip.h"
#include <iostream>
using namespace std;

template<typename T>
TempSip<T>::TempSip(T v)
{ var = v; }

template<typename T>
void TempSip<T>::Print()
{    cout << var << endl; }

template class TempSip<std::string>;
template class TempSip<double>;

/*Sip file*/
%Module(name=tempsip, keyword_arguments="Optional")

class TempSipDouble {
%TypeHeaderCode
#include "tempsip.h"
typedef TempSip<double> TempSipDouble;
%End
public:
    TempSipDouble(double v);
    void Print();
};

class TempSipString {
%TypeHeaderCode
#include <string>
#include "tempsip.h"
typedef TempSip<std::string> TempSipString;
%End
public:
    TempSipString(std::string v);
    void Print();
};


// Creates the mapping for std::string
// From:
http://www.riverbankcomputing.com/pipermail/pyqt/2009-July/023533.html

%MappedType std::string
{
%TypeHeaderCode
#include <string>
%End

%ConvertFromTypeCode
    // convert an std::string to a Python (unicode) string
    PyObject* newstring;
    newstring = PyUnicode_DecodeUTF8(sipCpp->c_str(), sipCpp->length(),
NULL);
    if(newstring == NULL) {
        PyErr_Clear();
        newstring = PyString_FromString(sipCpp->c_str());
    }
    return newstring;
%End

%ConvertToTypeCode
    // Allow a Python string (or a unicode string) whenever a string is
    // expected.
    // If argument is a Unicode string, just decode it to UTF-8
    // If argument is a Python string, assume it's UTF-8
    if (sipIsErr == NULL)
        return (PyString_Check(sipPy) || PyUnicode_Check(sipPy));
    if (sipPy == Py_None) {
        *sipCppPtr = new std::string;
        return 1;
    }
    if (PyUnicode_Check(sipPy)) {
        PyObject* s = PyUnicode_AsEncodedString(sipPy, "UTF-8", "");
        *sipCppPtr = new std::string(PyString_AS_STRING(s));
        Py_DECREF(s);
        return 1;
    }
    if (PyString_Check(sipPy)) {
        *sipCppPtr = new std::string(PyString_AS_STRING(sipPy));
        return 1;
    }
    return 0;
%End
};

#Python file
import tempsip
tempsip.TempSipDouble(4.5).Print()
tempsip.TempSipString('ciao').Print()

And it works :)

Now I can continue with the real library I am working on.

Regards
Gudjon


On Fri, May 1, 2015 at 2:54 PM, David Boddie <david at boddie.org.uk> wrote:

> On Fri, 1 May 2015 07:20:53 +0100, Guðjón Guðjónsson wrote:
>
> > Does anyone have a simple example on sip interface to a template class. I
> > have taken a look at qlist.sip and other information I have found on the
> > internet but I have to admit that I am too stupid to understand it.
> >    I wrote this very simple class and have been trying for several hours
> to
> > write a sip interface to it but without success.
>
> I think the SIP syntax is a little different to C++ syntax for these
> classes
> so it can be confusing. Also, a lot of code that deals with templates is
> typically for containers and the SIP equivalent uses the %MappedType
> directive to deal with those. However, that's not what you want in this
> case,
> I believe.
>
> I put some code in a couple of places that should help get you started:
>
>   https://github.com/dboddie/pyqt-mailing-list-tempsip
>   https://bitbucket.org/dboddie/pyqt-mailing-list-tempsip
>
> Get it from whichever site you're most comfortable with and configure it
> with
>
>   python configure.py
>
> Then run make as usual. The module should appear in the modules directory,
> but it won't be useful until you wrap specific classes of the template
> type.
>
> David
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150502/1d925280/attachment.html>


More information about the PyQt mailing list