Re: [Vala] Unions in Vala
- From: Bruce Reidenbach <bereiden gmail com>
- To: vala-list gnome org
- Subject: Re: [Vala] Unions in Vala
- Date: Thu, 19 Jul 2012 18:10:28 -0700
Clever!
Nice solution, Sebastian
From: Sebastian Reichel
Sent: 7/19/2012 7:43 PM
To: Nadir Sampaoli
Cc: vala-list gnome org
Subject: Re: [Vala] Unions in Vala
On Wed, Jul 18, 2012 at 08:19:29PM +0200, Nadir Sampaoli wrote:
I'm porting some code I wrote from procedural C to objective Vala and I'm
looking for the Vala-way to implement a union. I'm using a union in C
because I need a structure where one uin16_t and two uint8_t are aligned:
typedef struct {
uint8_t lo;
uint8_t hi;
} WORD;
typedef union {
uint16_t reg16;
WORD reg8;
} REG;
This way if I change reg8.hi's (or .lo's, for what matters) value,
reg16changes automatically.Now, in Vala, as far as I have been able to
find in
the documentation (I just started studying Vala, coming from a C#
background) there's no union keyword, and pointers are missing too.
If there's no union-like thing in Vala, is there at least some way to make
a built-in type referenceable (something like .NET's Int vs int)?
I have a last resort solution, but it's rather inefficient, so I'd like to
know if I can reproduce the original C code behaviour.
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]