Definition macro
Describes C union types to the c-ffi.
define C-union name [slot-spec; ...] [;] [type-options] [;] end [C-union] [name]
c-ffi
c-ffi
Describes C union types to the C-FFI. The syntax for the macro and its use are similar to define c-struct except that bitfield slots are not allowed. The designator created by the macro is a subclass of <c-union>.
Each of the slots in a union is laid out in memory on top of one another just as in C's union construct.
Example C declaration:
union Num {
int int_value;
double double_value;
};
Num *OneNum(); /* Returns a pointer to a Num */
Num *NumArray(); /* Returns a Num array */
Example FFI definition:
define C-union <Num> slot int-value :: <C-int>; slot double-value :: <C-double>; pointer-type-name: <Num*>; end C-union; define C-function one-num result num :: <Num*>; c-name: "OneNum"; end C-function; define C-function num-array result array :: <Num*>; c-name: "NumArray"; end C-function;
Example transactions:
? define variable n = one-num();
// Defined n.
? values(p.int-value, p.double-value);
154541
92832.e23 // or something
? define variable array = num-array();
// Defined array.
? array[5].object-class; // implicit conversion to
// the pointer type
{<Num> pointer #xff5e00}
? array[5].int-value := 0;
0
? array[5].double-value;
11232e-12 // or a different something