[PyKDE] namespaces in sip

Jim Bublitz jbublitz at nwinternet.com
Thu Oct 16 08:43:00 BST 2003


On Wednesday October 15 2003 14:46, Patrick Stinson wrote:
> a.h
> ----------------------------------
> namespace PK
> {
> class A {};
> }
>
> b.h
> ------------------------------------
> namespace PK
> {
> class B{};
> }
>
> pkmod.sip
> -----------------------------------
> %Module PK
> %Include a.sip
> %Include b.sip
>
> a.sip
> -------------------------------------
> /* same as a.h */
>
> b.sip
> -------------------------------------
> /* same as b.h */
>
>
> I'm working on modifying my sip project to include a lot of
> classes all contained in the same C++ namespace. What is the
> correct way to organize the sip files? Do all classes in the
> namespace have to be declared within the namespace in the same
> sip file?

No - as in C++, namespaces are extensible. Classes (in sip files) 
aren't (nested classes have to be defined within the parent 
definition's scope).

> Right now I have added the namespace decl and brackets in each
> class' sip file, all of which are included in the module file,
> just like in PyQt. When it's like this, I get a sip parse
> error on the line including the second file with a namespace
> decl.

One of two possibilities (I'm not sure which at the moment). 
First, I believe sip wants to see '};' end a namespace, not just 
'}', so you can try that first.  Actually, there is no "second" 
- I was going to question whether sip allows two namespace decls 
in a single file, but I just added a sip file to PyKDE that does 
that and it works. I'd go for the semicolon.

Otherwise, the only thing you need to be aware of is that sip 
won't accept any implicit scoping, so for example:

namespace a
{
    class c
    {
    public:
            c ();
            c (c&); # wrong - should be c (a::c&);
    };
};

will tell you that 'c' is undefined in the  second constructor. 
You need to use "a::c" *everywhere* c is referenced except in 
class, ctor and dtor names - even within the namespace a and 
even within class c. Enums declared within the namespace must be 
referenced everywhere with fully-qualified names also.

Unfortunately, sip won't really tell you c is undefined in the 
second ctor, just that c is undefined - if you have a lot of 
namespace'd stuff, it can take a long time to get all of the 
names right if a lot of them use implicit naming.

In Python if you do:

    from <module> import a

you can then do:

    x = a.c ()

since importing the namespace imports all of its members.


Jim




More information about the PyQt mailing list