2017-10-19 110 views
2

我有一个很大的C++代码,我用cython从python调用。我不确定在cython中应该如何调用枚举。这是我已经尝试过的。但这并不完整。帮帮我!如何访问cython中的枚举?

示范例子

class Foo{ 
public: 
    Foo1 bar(enum_is type = enum::any) const; 
} 
enum class enum_is:unsigned int{ 
any = 0, 
one = 1, 
two = 2, 
}; 

abc.pxd

cdef extern from "headerfile.hpp": 
    cdef cpp class enum_is: 
     pass 

cdef extern from "headerfile.hpp" namespace "enum_is": 
    cdef enum_is any 
    cdef enum_is one 
    cdef enum_is two 

abc.pyx

cdef class Pyenum_is: 
    cdef enum_is thisobj 
      def __cinit__(self,unsigned int val): 
       self.pin_isthisobj = <pin_is> val 
      def get_pin_is_type(self) 
       cdef r={<unsigned int> any:"any", 
         <unsigned int> one:"one", 
         <unsigned int> two:"two" 
         } 
       return r[<unsigned int>self.thisobj] 

我需要就如何实际使用Python的假设枚举函数帮助我有enum类正确包装

cdef class PyFoo1 
    cdef Foo1 Foo1thisobj 
    cdef Foo1* Foo1thisptr 

cdef class PyFoo 
    cdef Foo Foothisobj 
    cdef Foo* Foothisptr 
    def bar(self,#pass enum_is object here): 
     cdef Foo tempv = self.thisobj.bar(#pass enum here) 
     abc = PyFoo() 
     abc.Foo1thisobj = tempv 
     return abc 

有人能帮助我关于如何继续前进,在用Cython

回答

0

使用此枚举您可以用Cython声明枚举像

ctypedef enum options: OPT1, OPT2, OPT3

那么对于一个例子:

def main(): 
    cdef options test 
    test = OPT2 
    f(test) 

cdef void f(options inp): 
    if inp == OPT1: 
     print('OPT1') 
    elif inp == OPT2: 
     print('OPT2') 
    elif inp == OPT3: 
     print('OPT3') 
+0

我听不懂。你能否编辑你的答案来解释示范例子 –