[PyKDE] [RFC] [PATCH] Adding support for templated container types to sip

Jim Bublitz jbublitz at nwinternet.com
Thu Mar 3 06:05:27 GMT 2005

On Wednesday 02 March 2005 11:39, Bryan O'Sullivan wrote:

> I've been working on a patch to sip 4.2 that adds support for templated
> container types.

> This lets sip automatically generate code to handle mapping between a
> templated C++ container type and a Python collection of the appropriate
> variety.

Cool idea.

> Some background: right now, if you want to map between, for example
> std::vector<double> and a Python list, you have to explicitly write the
> conversion code between the two.  Worse, you cannot reuse this code to
> convert between a std::list<double> and a Python list.  So every kind of
> templated container you use requires a pile of hand-written mapping
> code, coupling drudgery with plenty of potential for error.

> I've added to sip the ability to automatically map between C++
> containers and Python collections.  You must still declare the C++
> containers you want mapped, but now it's simply one line of code for
> each, instead of a potentially large amount of source.
> I've modified the grammar accepted by sip as follows.  In addition to
> the current form of %MappedType ...
>         %MappedType std::vector<blah> {
>           // %TypeHeaderCode, %ConvertFromTypeCode and %ConvertToTypeCode
>         };
> sip now also accepts this:
>         %MappedType std::vector<blah>;
> It treats the lack of a body here as a cue to autogenerate the code that
> will convert between a std::vector of blah and a Python collection.

> If you want explicit control over the mapping code, you can still
> specify a body, and sip will behave as it currently does.

> For non-simple value types (the things inside the containers), you must
> tell sip how to handle the value type, or it will produce an error
> message.  No surprise there.

Could you expand on this a little more? Will it automatically pick up the 
sip*Convert[To,From]_* functions that sip creates (either from binding or 
other %MappedType code)?

For example, std::list<QWidget> or std::list<long long> (long long has 
%MappedType code in PyKDE).

> If you try to get sip to autogenerate code for an unsupported container
> type (e.g. a std::pair), it will produce an error message.

> I've attached a prerelease of the patch against sip 4.2, to get
> feedback.  It does not affect any of the existing functionality of sip,
> but simply adds the autogeneration feature.  I've attempted to follow
> sip's coding style, so the patch will integrate cleanly.

> The patch has the following caveats:

>       * No support yet for Qt containers such as QValueList<>, which I
>         don't use.  I don't even know if KDE has its own containers, so
>         needless to say, there's no support for them, if they exist.

KDE has no containers of it's own I can think of (it uses Qt's). It does have 
non-container template types like KSharedPtr (eg KSharedPtr<KService>) and 
probably some others that aren't coming to mind at the moment. (see the 
%MappedType code in kio/kservice.sip - there are a number of similar 
instances in the kio module).

>       * No support yet for std::map or related types.
>       * I suspect that the error checking in some of the generated code
>         is wrong in cases of memory allocation failure, but that should
>         be easy to fix.
>       * I haven't attached a diff against sipgen/parser.c, since the
>         diff is huge.  You must use bison to regenerate parser.c from
>         parser.y, because sip's build process doesn't create an explicit
>         Makefile rule telling make to do this (so make will either do
>         nothing or use yacc, both of which are wrong).

> Here's a diffstat summary:
>  gencode.c |  392
> +++++++++++++++++++++++++++++++++++++++++++++----------------- parser.y  | 
>  14 ++
>  sip.h     |    7 +
>  3 files changed, 310 insertions(+), 103 deletions(-)


More information about the PyQt mailing list