! Copyright (C) 2004, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: byte-arrays arrays assocs delegate kernel kernel.private math
-math.order math.parser namespaces make parser sequences strings
-words splitting cpu.architecture alien alien.accessors
-alien.strings quotations layouts system compiler.units io
-io.files io.encodings.binary io.streams.memory accessors
-combinators effects continuations fry classes vocabs
-vocabs.loader words.symbol macros ;
+USING: accessors alien alien.accessors arrays byte-arrays
+classes combinators compiler.units cpu.architecture delegate
+fry kernel layouts locals macros math math.order quotations
+sequences system words words.symbol ;
QUALIFIED: math
IN: alien.c-types
SINGLETON: void
-DEFER: <int>
-DEFER: *char
+DEFER: <ref>
+DEFER: deref
TUPLE: abstract-c-type
{ class class initial: object }
M: c-type base-type ;
-: little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
+: little-endian? ( -- ? ) 1 int <ref> char deref 1 = ; foldable
GENERIC: heap-size ( name -- size )
: c-type-clamp ( value c-type -- value' )
dup { float double } member-eq?
[ drop ] [ c-type-interval clamp ] if ; inline
+
+:: <ref> ( value c-type -- c-ptr )
+ c-type heap-size <byte-array> :> c-ptr
+ value c-ptr 0 c-type set-alien-value
+ c-ptr ; inline
+
+: deref ( c-ptr c-type -- value )
+ [ 0 ] dip alien-value ; inline