On 12/17/2010 19:35, John Cummings wrote:
> My apologies for the long email. Just to not bore everyone, I'll send
> two messages. The first one quickly summarizing my observations and
> questions. In the next one, I'll reply to myself with  the details.
>
> I am able to use a class inside a namespace if I use this general
> pattern in the header file:
>
> namespace bar {
>     class foo;
> }
> class bar::foo {
>    ...
> };
>
> However, I cannot get things to work using this pattern:
>
> namespace bar {
>     class foo {
>         ...
>     };
> }
>
> Has anyone else seen this behavior? Is there another way to have a bind
> a class that is inside a C++ namespace?
>
> Yes, I  read the namespace type in the typesystem documentation:
>
> http://www.pyside.org/docs/apiextractor/typesystem_specifying_types.html#namespace-type
>
> That document seems to indicate that you cannot have classes in a
> namespace. To quote: "Note that within namespaces, the generator only
> supports enums (i.e., no functions or classes)." So, I understand that
> the answer may be that it is impossible right now. I am just perplexed
> why it works one way and not the other.
>
>
>   

Now for the gory details. First, I am tried all of these things on
OpenSuse 11.3, 64-bit with Qt 4.6.3 and PySide beta1. I am trying to
wrap an existing C++ library and thus started with the PySide Binding
Generation Tutorial here:

http://developer.qt.nokia.com/wiki/PySide_Binding_Generation_Tutorial

The initial steps worked just fine and I generally understand them. I
even started trying to wrap an existing library. However, the existing
library has most of its classes in a C++ namespace. That didn't work the
first time. As a test, I took a class out of the namespace and
everything seemed to work just fine. However, I do not want to repeat
this process for every class in the library.

So, to better understand things, I modified the libfoo example to move
the Math class in the C++ namespace bar. My first iteration is the
typical way many coders (meaning me) put classes in a namespace, namely
in foo.h:

namespace bar {
    class Math : public QObject
    {
        Q_OBJECT
    public:
        Math() {}
        virtual ~Math() {}
        int squared(int x);
    };
} //end namespace bar

and then I foo.cpp we have:

int bar::Math::squared(int x)
{
    return x * x;
}

That causes several things to change and ultimately not work. My first
clue was a warning from the bindings generator that said, "type
'bar::Math' is specified in typesystem, but not defined. This could
potentially lead to compilation errors." That warning caused me to add a
namespace-type line to the typesystem file like so:

<namespace-type name="bar" />

That addition silenced the warning. Next, I noticed that the binding
generator also chooses different names for its output files such that I
had to change CMakeLists.txt file as follows:

bar_math_wrapper.cpp instead of math_wrapper.cpp

With that change, everything  compiles fine. However,  when trying to
run the python test script, it fails with an error about an undefined
symbol that said, "ImportError: .../foo.so: undefined symbol:
_Z8init_barP7_object."

I then went through several failed attempts to compile in the new
bar_wrapper.cpp that the generator also created. As I said, none of
those worked, so I eventually tried again by redoing foo.h as follows:

namespace bar {
   class Math;
}
class bar::Math : public QObject
{
    Q_OBJECT
public:
    Math() {}
    virtual ~Math() {}
    int squared(int x);
};

With this change along with prefixing the class with "bar::" in the
typesystem.xml file and the resulting file names changes in the 
CMakeLists.txt file, everything works as you would expect.

So, that leads me to my questions again. Why does it work with the
forward declaration syntax but not the inline syntax? My best guess
would be a generator parser limitation based on the warning message.
However, I have not attempted to delve into the code at all.

I would be happy to send my modified files, which are purposefully not
much different than the libfoo example.

Thank you
John Cummings

_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to