[Prev] Thread [Next]  |  [Prev] Date [Next]

[Factor-talk] Symbolic enums in the FFI Joe Groff Tue Apr 20 21:00:26 2010

In the latest git sources, Erik Charlebois and I have changed enum handling in 
the FFI to deal directly with Factor symbols. The old C-ENUM: word has been 
replaced with a new ENUM: word that creates a new C type. Symbols of this type 
will automatically be converted from Factor symbols to integers and back by the 
FFI. New words in the "alien.enums" vocab, enum>number and number>enum, are 
also available to perform the conversion explicitly.

ENUM: triforce courage power wisdom ;

UNION-STRUCT: water-temple { t triforce } { i integer } ;

water-temple <struct>
        wisdom >>t
        i>> .           ! gives 2
water-temple <struct>
        1 >>i
        t>> .           ! gives power

You can also optionally control the base integer type of ENUM: types, and 
specify explicit values for your enumeration symbols:

ENUM: foo < ushort a b c ;
ENUM: bar < longlong { x HEX: 1,0000,0000 } y z ;

Integers that don't map to enum values pass through the FFI retaining integer 
values. Here are the incompatible changes you may need to update code for:

- Code that uses C-ENUM: only to define constants will need to be changed to 

C-ENUM: f A B C ;       ! old

CONSTANT: A 0           ! new

- Code that needs the integer value of an enum symbol will need to use 
- Conversely, enum values that are destined for the FFI no longer need to be 
force-evaluated with $ in literal expressions:

C-ENUM: foo a b c ;                     ! old
FUNCTION: int twib ( foo x ) ;
{ $ a $ b $ c } [ twib ] map

ENUM: foo a b c ;                               ! new
FUNCTION: int twib ( foo x ) ;
{ a b c } [ twib ] map

Factor-talk mailing list