[Pyrex] enums in C++ classes

Lenard Lindstrom len-l at telus.net
Wed Jan 10 17:16:22 UTC 2007


Ravi Lanka wrote:
> Hi,
>
>     I am using pyrex to write a wrapper to a C++ library.  class A given 
> here is a representative class of one of the classes I have in the C++ 
> library.  It has an enum declared inside the class and one of the 
> methods takes this enum as an argument. I am not exactly sure how to 
> wrap this enum and use it from the python module.
> I have included the code below that works fine and now I want to wrap 
> the "set" method.
>
> please advise.
> Ravi
>
>
> =====================================================
> code from a.h and a.cpp:
> class A {
>     public:
>        A(): { value = 0;}
>        typedef enum some_enum { zero, one, two, three };
>        void get() { return value;}
>        void set( some_enum eval, int i ) { value = i*eval ; }
>     private:
>        int value;
> };
>
> demo.pyx:
>     cdef extern from "a.h":
>         ctypedef struct  A "A":
>             int (*get) ()
>             #void (*set)(some_enum val, int i)
>         A* AFactory "new A"()
>         void deleteA "delete  " (void *o)
>
>     cdef class AWrapper:
>         def __new__( self ):
>             cdef A *first
>             cdef int i
>             first = AFactory()
>             print first.get()
> =======================================================================
>
> _______________________________________________
>   

This is untried but should work. Move the enum declaration outside the 
class and use C name specifiers to put it back in the class's namespace:

cdef extern from "a.h":
    cdef enum Asome_enum "A::some_enum":
        Azero "A::zero"
        Aone "A::one"
        Atwo "A::two"
        Athree "A::three"

-- 
Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list