[PyKDE] sipping char[N] datatypes in structs

Jonathan Gardner jgardner at jonathangardner.net
Fri May 16 18:17:01 BST 2003


On Friday 16 May 2003 08:33, Jim Bublitz wrote:
> On Friday 16 May 2003 01:33, Phil Thompson wrote:
> > > This sucker won't sip, though:
> > >
> > > struct PGnotify
> > > {
> > > 	char		relname[NAMEDATALEN];
> > > 	int			be_pid;
> > > };
> > >
> > > If I do "char *relname", then it won't compile because char
> > > * is not the same as char[32] (NAMEDATALEN is defined to be
> > > 32).
> > >
> > > Any tips?
>
> Yes - avoid these at all costs :)
>

Avoided. I'll take the relname[NAMEDATALEN] and convert it to a null 
terminated string and then pass back a char * with a wrapper func.

> > SIP doesn't support arrays (as you have found). In fact I've
> > never used SIP to wrap a C library, so I'm pleasently
> > surprised you've got as far as you have.
> >
> > The only thing I can think of at the moment is to create a
> > thin C++ class library that implements exactly the API you
> > want and SIP that - but it's obviously not the bare metal.
>

It's close enough for my purposes. Besides, looking at it now, it makes  a 
whole lot of sense because it makes the library much more functional with 
very little work on my part.

>
> The other thing I'd point out (won't help here, but may be useful
> for other stuff) is that you can also treat structs as classes:
>
> struct SomeStruct { ... };
>
> is the same as:
>
> class SomeStruct
> {
> public:  // structs default to public, classes to private
>    ...
> };
>

Definitely. Actually, I am leaving the structs behind and building class 
wrappers for them all, per Phil's advice above. But sip didn't choke at all 
on struct SomeStruct { ... }; and did exactly what it would do if it was a 
class -- which is what I expected.

With these two bits of advice, and with a lot of rusty memories about C and 
C++ from several years ago when I was really good at it (it being the only 
option on windows at the time to develop apps), I am starting to hack 
together wrapper classes that will avoid the issues sip has trouble with.

And I have to admit, when it comes to C++, sip makes it EXTREMELY easy to 
write an interface. In literally a few minutes, I had most of the function 
calls for a connection mapped to methods of a connection class. Thanks to the 
tutorial by Donovan, everything hard (writing makefiles and how to invoke 
sip) was already solved.

I think I should have something rough yet mostly complete by the end of the 
week (and it ends in a couple of days here).

This is exciting because I can take these bare-metal library calls, and build 
the stuff to make them presentable on top of it. Python won't be missing 
anything C has when it comes to postgresql! This opens up the whole C world 
to sip -- all we have to do is write thin C++ layers to get it to work. After 
this, I want to take a stab at ImageMagick... ;-)

Thanks and kudos to Phil, Jim (for the section about sip in Boudewijn's book), 
Donovan, Wilken, and all the others for making it so easy to get started with 
sip.

-- 
Jonathan Gardner <jgardner at jonathangardner.net>
(was jgardn at alumni.washington.edu)
Live Free, Use Linux!




More information about the PyQt mailing list