Re: [Vala] Unions in Vala



Nice solution.
I didn't understood how a "vala struct" could be interpreted as a C union;
this example clarifies it well.

Thanks for sharing.

--
nadir

2012/7/20 Sebastian Reichel <sre ring0 de>

How about defining the union in C and making it available in Vala
with a vapi file?

== reg.h ==
#include <stdint.h>

typedef struct {
    uint8_t lo;
    uint8_t hi;
} WORD;

typedef union {
    uint16_t reg16;
    WORD reg8;
} REG;

== reg.vapi ==
[CCode (cheader_filename = "reg.h")]
public struct WORD {
    uint8 lo;
    uint8 hi;
}

[CCode (cheader_filename = "reg.h")]
public struct REG {
    uint16 reg16;
    WORD reg8;
}

== test.vala ==
public static int main(string[] args) {
    REG register = REG();
    register.reg16 = 0;
    register.reg8.lo = 0x23;
    register.reg8.hi = 0x42;
    stdout.printf("%04x\n", register.reg16);
    return 0;
}

== demo ==
$ valac test.vala reg.vapi
$ ./test
4223

-- Sebastian



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]