Next Previous Up Top Contents Index

1.6 Defining types

define C-union

Definition macro

Summary

Describes C union types to the c-ffi.

Signature

define C-union name 
  [slot-spec; ...] [;]
  [type-options] [;]
end [C-union] [name]

Arguments

name
A Dylan variable name.

slot-spec

type-options
A property list.

Library

c-ffi

Module

c-ffi

Description

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


C FFI and Win 32 Reference - 31 MAR 2000

Next Previous Up Top Contents Index