Enumerations are the first part of the .h file to
convert.
To convert a C enumeration to Pyrex:
.h that is being
converted, typedef, replace it
with a ctypedef,enum,}) to after the
enum,, from the end of each line,
and{} blocking with a
: and indenting.For example, consider the following C enumeration, called
xosd_pos, that is contained in
xosd.h.
typedef enum
{
XOSD_top,
XOSD_bottom,
XOSD_middle
} xosd_pos;
The above enumeration consists of three elements:
XOSD_top,
XOSD_bottom and
XOSD_middle.
The Pyrex version of the enumeration looks similar.
cdef extern from "xosd.h": ctypedef enum xosd_pos: XOSD_top XOSD_bottom XOSD_middle
The first line tells Pyrex to
include the
file xosd.h in the generated C code.
cdefextern fromC Referencedeclaration, and
"xosd.h".h to include.The second line declares the enumeration that we want to use in Pyrex.
ctypedeftypedef declaration,enumxosd_posThe rest of the enumeration is then copied, with
whitespace replacing {} as the blocking
mechanism and the commas removed.
The XOSD_ could also be removed from the start of
each enumeration value, but I will leave them in to emphasise
the mechanical nature of the conversion!
A further example is the xosd_command
enumeration that is converted from the following C
enumeration.
typedef enum
{
XOSD_percentage,
XOSD_string,
XOSD_printf,
XOSD_slider
} xosd_command;
The Pyrex version is as follows.
ctypedef enum xosd_command:
XOSD_percentage
XOSD_string
XOSD_printf
XOSD_slider
Notice that the Pyrex xosd_command
enumeration is indented because it is
part of the same
cdef extern from
block as
the xosd_pos
enumeration we wrote earlier.