[PyKDE] Interfacing to C++ standard library

Paul F. Kunz Paul_Kunz at SLAC.Stanford.EDU
Sat Dec 21 20:32:00 GMT 2002


>>>>> On Wed, 11 Dec 2002 22:23:38 +0100, Torsten Marek <shlomme at gmx.net> said:


> It should just need string being replaced by std::string and
> sipForceConvertTo_string(...) renamed to
> sipForceConvertTo_std_string(...), but I'm not really sure about the
> naming scheme, so please try yourself, before I post incorrect code.

   Thanks for your tips.   The following works for me with gcc 2.95.3...

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

%ConvertFromTypeCode
     const char *s = sipCpp->c_str();
     return PyString_FromString(s);
%End

%ConvertToTypeCode
         // Allow a Python string whenever a string is expected.
         if (sipIsErr == NULL)
             return PyString_Check(sipPy);
         if (sipPy == Py_None) {
             *sipCppPtr = NULL;
             return 0;
         }
         if (PyString_Check(sipPy)) {
             *sipCppPtr = new string(PyString_AS_STRING(sipPy));
             return 0;
         }
         *sipCppPtr = (string *)sipForceConvertTo_std_string(sipPy,sipIsErr);
         return 1;
%End

};

I eventually need to port to Windows where string is in namespace std,
so needed the std::string stuff.

Thanks again for this code.




More information about the PyQt mailing list