diff --git a/source/developer-guides/bindings/writing-a-vapi-manually/04-00-recognizing-vala-semantics-in-c-code/04-02-enums-and-flags.rst b/source/developer-guides/bindings/writing-a-vapi-manually/04-00-recognizing-vala-semantics-in-c-code/04-02-enums-and-flags.rst index df2c2d2..d0f539b 100644 --- a/source/developer-guides/bindings/writing-a-vapi-manually/04-00-recognizing-vala-semantics-in-c-code/04-02-enums-and-flags.rst +++ b/source/developer-guides/bindings/writing-a-vapi-manually/04-00-recognizing-vala-semantics-in-c-code/04-02-enums-and-flags.rst @@ -64,4 +64,78 @@ There is also a common tendency to use combinable bit patterns. These are conver CREATE } -In Vala, enums and flags may have member functions. In particular, ``strerr``-like functions are best converted to member functions. +Supersets +--------- + +If one set of enums or flags is a superset of another, but they are logically separate, You can create a different set of enums that refer to the same enum +or defines. + +For example: + +.. code-block:: c + + #define FOO_A 1 + #define FOO_B 2 + #define FOO_C 3 + #define FOO_D 4 + /* takes FOO_A or B only */ + void do_something(int); + /* takes any FOO_ value */ + void do_something_else(int); + +Can become this: + +.. code-block:: vala + + [CCode (cname = "int", cprefix = "FOO_", has_type_id = false)] + public enum Foo { A, B } + [CCode (cname = "int", cprefix = "FOO_", has_type_id = false)] + public enum FooExtended { C, D } + +You can then cast one enum to another: + +.. code-block:: vala + + var foo_enum = (Foo) FooExtended.C; + +Member functions and constants +------------------------------ + +In Vala, enums and flags may have member functions and constants. +In particular, ``strerr``-like functions are best converted to member functions. + +Enum aliases and ``to_string()`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The default function ``to_string()`` can cause problems if the enum has aliases (the C error ``duplicate case`` will trigger). So to solve this, +you can create additional constants after the enum has been declared to add the missing enum aliases. + +For example this enum: + +.. code-block:: c + + typedef enum { + BAR_A, + BAR_B, + BAR_C, + BAR_D, + BAR_FIRST = BAR_A, + BAR_LAST = BAR_D, + } bar_e; + +Will become: + +.. code-block:: vala + + [CCode (cname = "bar_e", cprefix = "BAR_", has_type_id = false)] + public enum Bar { + A, + B, + C; + + [CCode (cname = "BAR_")] + public const Bar FIRST; + + [CCode (cname = "BAR_")] + public const Bar LAST; + }