[PyKDE] Interfacing to C++ standard library

Jim Bublitz jbublitz at nwinternet.com
Wed Dec 11 22:54:00 GMT 2002


On 11-Dec-02 Torsten Marek wrote:
> Jim Bublitz schrieb:
>> On 11-Dec-02 Paul F. Kunz wrote:
>> 
>>>   To complete my SIP based Python extension module, I need to
>>>convert from C++ to Python and vica versa an std::vector<double>
>>>and std::vector<std::string>.   I found some hints in this
>>>mailing list's archive dated 13 Apr 2002.   Is there a better
>>>source of information on how to proceed?
>> 
>> 
>> ...
>> You'll might also need to use the same methods for std::string
>> to convert to/from either a Python string or QString if it's
>> used
>> outside the mapped type/member code for
>> std::vector<std::string>.
> This is the mapping code for string:
> 
> %MappedType 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_string(sipPy,sipIsErr);
>          return 1;
> %End
> };
> 
> 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.

Looks OK to me, but I'm not familiar with the std lib semantics. The
only change I would make is

%MappedType std::string

if that's how it's referenced in the code.

The std::vector types would be similar in style, although obviously
different Py* conversion functions and a little more involved,
since it's necessary to iterate over the lists.

Jim




More information about the PyQt mailing list