Skip to content

4.3. Simple Type Structs

C libraries often define new types for numeric handles, sizes and offsets. To translate these to a VAPI file, just use the SimpleType attribute with a struct and inherit from the same simple type in the C header.

An example:

c
typedef uint32_t people_inside;

would be defined in the VAPI file as:

vala
[SimpleType]
[CCode (cname = "people_inside", has_type_id = false)]
public struct PeopleInside : uint32 {
}

When inheriting from an existing type, all the methods will be carried forward. For sizes and offsets, this is probably desirable; for handles, it is probably not. For example, a UNIX file descriptor is stored in an integer, but adding or multiplying two file handles has no sense. In this case, it is preferable not to inherit from a numeric type and add the attribute IntegerType (rank=X) so the Vala compiler can automatically cast a type into an integer of an appropriate size when needed (e.g., initialising from an integral constant).

An example from XCB:

c
typedef uint32_t xcb_atom_t;

would be defined in the VAPI file as:

vala
[SimpleType]
[IntegerType (rank = 9)] 
[CCode (cname ="xcb_atom_t", has_type_id = false)] 
public struct AtomT {
    //...
}

The ranks for the common types, as defined in the glib-2.0.vapi and posix.vapi files, are:

RankTypes in glib-2.0Other Use
1gint8
gfloat
2gchar
gdouble
3guchar
guint8
Posix.cc_t
4gshort
gint16
5gushort
guint16
6gint
gint32
Posix.pid_t
7guint
guint32
gunichar
Posix.speed_t
Posix.tcflag_t
8glong
gssize
time_t
Posix.clock_t
9gulong
gsize
Posix.nfds_t
Posix.key_t
Posix.fsblkcnt_t
Posix.fsfilcnt_t
Posix.off_t
Posix.uid_t
Posix.gid_t
Posix.mode_t
Posix.dev_t
Posix.ino_t
Posix.nlink_t
Posix.blksize_t
Posix.blkcnt_t
10gint64
11guint64