Wrap enum class for cython

后端 未结 3 1664
面向向阳花
面向向阳花 2021-01-02 03:15

I am trying to wrap an enum class in a c++ header file for use in a cython project.I have googled around and can not find out how to achieve this - is it supported?

3条回答
  •  情歌与酒
    2021-01-02 04:00

    CPP class

    enum class Color {red, green = 20, blue}; 
    

    Definition of type

    cdef extern from "colors.h":
      cdef cppclass Color:
        pass
    

    Definition of color types

    cdef extern from "colors.h" namespace "Color":
      cdef Color red
      cdef Color green
      cdef Color blue
    

    Python implementation

    cdef class PyColor:
      cdef Color thisobj
      def __cinit__(self, int val):
        self.thisobj =  val
    
      def get_color_type(self):
        cdef c = {red : "red",  green : "green",  blue : "blue"}
        return c[self.thisobj]
    

提交回复
热议问题