Robert Bradshaw | 4 Apr 2008 01:26
Favicon

Re: [Cython] [Pyrex] C++ forward declaration

On Apr 3, 2008, at 4:20 PM, Ravi Lanka wrote:
> Pyrex gurus,
>
>    I am trying to wrap two C++ classes in a Namespace called
> "MyNameSpace".  I was breezing through using some of the ideas  
> shared by
> Lenard and others.  I got stuck with a case of forward declaration as
> below.  The two classes I have, A and B, have methods that require the
> objects corresponding to B and A.  If I try to declare, say class B,
> without all the methods before class A and then try to create the
> complete code for class B, it complains that class B is re-declared.
> How do I get around this problem ?
>
> cdef extern from "A.h":
>     ctypedef struct A "MyNameSpace::A":
>         void (*foo)( B data )
>
> cdef extern from "B.h":
>     ctypedef struct B "MyNameSpace::B":
>         void (*bar)( A data )
>
> thanks
> Ravi

I believe you have to do a forward declaration. The following works  
for me:

cdef extern from "B.h":
     ctypedef struct B

(Continue reading)

Lenard Lindstrom | 5 Apr 2008 00:21

Re: C++ forward declaration

Robert Bradshaw wrote:
> On Apr 3, 2008, at 4:20 PM, Ravi Lanka wrote:
>   
>> Pyrex gurus,
>>
>>    I am trying to wrap two C++ classes in a Namespace called
>> "MyNameSpace".  I was breezing through using some of the ideas  
>> shared by
>> Lenard and others.  I got stuck with a case of forward declaration as
>> below.  The two classes I have, A and B, have methods that require the
>> objects corresponding to B and A.  If I try to declare, say class B,
>> without all the methods before class A and then try to create the
>> complete code for class B, it complains that class B is re-declared.
>> How do I get around this problem ?
>>
>> cdef extern from "A.h":
>>     ctypedef struct A "MyNameSpace::A":
>>         void (*foo)( B data )
>>
>> cdef extern from "B.h":
>>     ctypedef struct B "MyNameSpace::B":
>>         void (*bar)( A data )
>>
>> thanks
>> Ravi
>>     
>
> I believe you have to do a forward declaration. The following works  
> for me:
>
(Continue reading)

Robert Bradshaw | 5 Apr 2008 03:01
Favicon

Re: [Cython] [Pyrex] C++ forward declaration

On Apr 4, 2008, at 3:21 PM, Lenard Lindstrom wrote:

> First, the Pyrex 0.9.6.4 compiler rejected the above code given by
> Robert Bradshaw. Second, even when altered to use function pointers  
> the
> generated code was wrong.

Sorry, I just hastily typed this up in my email to demonstrate the  
concept of forward declaration (which was the missing piece). Also,  
you're right about needing to do function pointers if you're using  
Pyrex.

> The global variable b was delcared as "B", not
> "MyNameSpace::B". This example corrects both problems:
>
> cdef extern from "B.h":
>      ctypedef struct B "MyNameSpace::B"
>
> cdef extern from "A.h":
>      ctypedef struct A "MyNameSpace::A":
>          void (* foo)(B data)
>
> cdef extern from "B.h":
>      ctypedef struct B "MyNameSpace::B":
>          void (* bar)(A data)
>
> cdef A a
> cdef B b
>
> # this will compile...but don't run it
(Continue reading)

Ravi Lanka | 5 Apr 2008 00:47
Favicon

Re: C++ forward declaration

Lenard Lindstrom wrote:
> Robert Bradshaw wrote:
>   
>> On Apr 3, 2008, at 4:20 PM, Ravi Lanka wrote:
>>   
>>     
>>> Pyrex gurus,
>>>
>>>    I am trying to wrap two C++ classes in a Namespace called
>>> "MyNameSpace".  I was breezing through using some of the ideas  
>>> shared by
>>> Lenard and others.  I got stuck with a case of forward declaration as
>>> below.  The two classes I have, A and B, have methods that require the
>>> objects corresponding to B and A.  If I try to declare, say class B,
>>> without all the methods before class A and then try to create the
>>> complete code for class B, it complains that class B is re-declared.
>>> How do I get around this problem ?
>>>
>>> cdef extern from "A.h":
>>>     ctypedef struct A "MyNameSpace::A":
>>>         void (*foo)( B data )
>>>
>>> cdef extern from "B.h":
>>>     ctypedef struct B "MyNameSpace::B":
>>>         void (*bar)( A data )
>>>
>>> thanks
>>> Ravi
>>>     
>>>       
(Continue reading)


Gmane