Skip to content

Fixing incorrect enum information and add alternatives #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}