<div dir="ltr"><div><div><div><div><div>Hi David<br><br></div>Thanks a lot for your help.<br><br></div>The solution is the following<br><br></div>/*Header file*/<br>#include <string><br>using namespace std;<br><br>template <typename T><br>class TempSip {<br> T var;<br>public:<br> TempSip(T v);<br> void Print();<br>};<br><br></div>/*Source file*/<br>#include "tempsip.h"<br>#include <iostream><br>using namespace std;<br><br>template<typename T><br>TempSip<T>::TempSip(T v)<br>{ var = v; }<br><br>template<typename T><br>void TempSip<T>::Print()<br>{ cout << var << endl; }<br><br>template class TempSip<std::string>;<br>template class TempSip<double>;<br><br></div>/*Sip file*/<br>%Module(name=tempsip, keyword_arguments="Optional")<br><br>class TempSipDouble {<br>%TypeHeaderCode<br>#include "tempsip.h"<br>typedef TempSip<double> TempSipDouble;<br>%End<br>public:<br> TempSipDouble(double v);<br> void Print();<br>};<br><br>class TempSipString {<br>%TypeHeaderCode<br>#include <string><br>#include "tempsip.h"<br>typedef TempSip<std::string> TempSipString;<br>%End<br>public:<br> TempSipString(std::string v);<br> void Print();<br>};<br><br><br>// Creates the mapping for std::string<br>// From: <a href="http://www.riverbankcomputing.com/pipermail/pyqt/2009-July/023533.html">http://www.riverbankcomputing.com/pipermail/pyqt/2009-July/023533.html</a><br><br>%MappedType std::string<br>{<br>%TypeHeaderCode<br>#include <string><br>%End<br><br>%ConvertFromTypeCode<br> // convert an std::string to a Python (unicode) string<br> PyObject* newstring;<br> newstring = PyUnicode_DecodeUTF8(sipCpp->c_str(), sipCpp->length(), NULL);<br> if(newstring == NULL) {<br> PyErr_Clear();<br> newstring = PyString_FromString(sipCpp->c_str());<br> }<br> return newstring;<br>%End<br><br>%ConvertToTypeCode<br> // Allow a Python string (or a unicode string) whenever a string is<br> // expected.<br> // If argument is a Unicode string, just decode it to UTF-8<br> // If argument is a Python string, assume it's UTF-8<br> if (sipIsErr == NULL)<br> return (PyString_Check(sipPy) || PyUnicode_Check(sipPy));<br> if (sipPy == Py_None) {<br> *sipCppPtr = new std::string;<br> return 1;<br> }<br> if (PyUnicode_Check(sipPy)) {<br> PyObject* s = PyUnicode_AsEncodedString(sipPy, "UTF-8", "");<br> *sipCppPtr = new std::string(PyString_AS_STRING(s));<br> Py_DECREF(s);<br> return 1;<br> }<br> if (PyString_Check(sipPy)) {<br> *sipCppPtr = new std::string(PyString_AS_STRING(sipPy));<br> return 1;<br> }<br> return 0;<br>%End<br>};<br><br><div><div><div>#Python file<br>import tempsip<br>tempsip.TempSipDouble(4.5).Print()<br>tempsip.TempSipString('ciao').Print()<br><br></div><div>And it works :)<br></div><div><br></div><div>Now I can continue with the real library I am working on.<br><br></div><div>Regards<br></div><div>Gudjon<br><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 1, 2015 at 2:54 PM, David Boddie <span dir="ltr"><<a href="mailto:david@boddie.org.uk" target="_blank">david@boddie.org.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Fri, 1 May 2015 07:20:53 +0100, Guðjón Guðjónsson wrote:<br>
<br>
> Does anyone have a simple example on sip interface to a template class. I<br>
> have taken a look at qlist.sip and other information I have found on the<br>
> internet but I have to admit that I am too stupid to understand it.<br>
> I wrote this very simple class and have been trying for several hours to<br>
> write a sip interface to it but without success.<br>
<br>
</span>I think the SIP syntax is a little different to C++ syntax for these classes<br>
so it can be confusing. Also, a lot of code that deals with templates is<br>
typically for containers and the SIP equivalent uses the %MappedType<br>
directive to deal with those. However, that's not what you want in this case,<br>
I believe.<br>
<br>
I put some code in a couple of places that should help get you started:<br>
<br>
<a href="https://github.com/dboddie/pyqt-mailing-list-tempsip" target="_blank">https://github.com/dboddie/pyqt-mailing-list-tempsip</a><br>
<a href="https://bitbucket.org/dboddie/pyqt-mailing-list-tempsip" target="_blank">https://bitbucket.org/dboddie/pyqt-mailing-list-tempsip</a><br>
<br>
Get it from whichever site you're most comfortable with and configure it with<br>
<br>
python configure.py<br>
<br>
Then run make as usual. The module should appear in the modules directory,<br>
but it won't be useful until you wrap specific classes of the template type.<br>
<br>
David<br>
_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></blockquote></div><br></div>