Wrap enum class for cython

后端 未结 3 1661
面向向阳花
面向向阳花 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条回答
  •  Happy的楠姐
    2021-01-02 03:45

    Another alternative that allows using PEP-435 Enums as mentioned in Cython docs is as follows:

    foo.h

    namespace foo {
    enum class Bar : uint32_t {
        Zero = 0,
        One = 1
    };
    }
    

    foo.pxd

    from libc.stdint cimport uint32_t
    
    cdef extern from "foo.h" namespace 'foo':
    
        cdef enum _Bar 'foo::Bar':
            _Zero 'foo::Bar::Zero'
            _One  'foo::Bar::One'
    
    
    cpdef enum Bar:
        Zero =  _Zero
        One  =  _One
    

    main.pyx

    from foo cimport Bar
    
    print(Bar.Zero)
    print(Bar.One)
    
    # or iterate over elements
    for value in Bar:
        print(value)
    

提交回复
热议问题