Wrapping Enumerations

Enumerations are the first part of the .h file to convert. To convert a C enumeration to Pyrex:

  1. Tell Pyrex the name of the .h that is being converted,
  2. If there is a typedef, replace it with a ctypedef,
  3. Write enum,
  4. Move the name of the enumeration from after the closing bracket (}) to after the enum,
  5. Remove , from the end of each line, and
  6. Replace the {} blocking with a : and indenting.

XOSD

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.

cdef
is a Pyrex (rather than a Python) declaration,
extern from
is a C Reference declaration, and
"xosd.h"
is the name of the .h to include.

The second line declares the enumeration that we want to use in Pyrex.

ctypedef
starts a typedef declaration,
enum
tells Pyrex that you are declaring an enumeration, and
xosd_pos
is the name of the enumeration.

The 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.