Bridge.h (1766B)
1 // 2 // Bridge.h 3 // uimac 4 // 5 // Created by Craig Federighi on 4/25/07. 6 // Copyright 2007 __MyCompanyName__. All rights reserved. 7 // 8 #import <Cocoa/Cocoa.h> 9 10 /* 11 Bridge supports safe calling from C back to OCaml by using daemon threads 12 spawned from OCaml to make the actual calls and converting all argument / return values 13 in the OCaml thread (when in possession of the OCaml lock) 14 */ 15 @interface Bridge : NSObject { 16 } 17 + (void)startup:(const char **)argv; 18 @end 19 20 /* 21 ocamlCall(sig, funcName, [args...]); 22 23 Call ocaml function (via safe thread handoff mechanism). 24 Args/return values are converted to/from C/OCaml according to the 25 supplied type signture string. Type codes are: 26 x - void (for return type) 27 i - long 28 s - char * 29 S - NSString * 30 N - NSNumber * 31 @ - OCamlValue (see below) 32 33 Examples: 34 long count = (long)ocamlCall("iS", "lengthOfString", @"Some String"); 35 36 (void)ocamlCall("x", "someVoidOCamlFunction"); 37 38 OCamlValue *v = (id)ocamlCall("@Si", "makeArray", @"Some String", 10); 39 NSString s = [v getField:0 withType:'S']; 40 */ 41 extern void *ocamlCall(const char *argTypes, ...); 42 43 // Wrapper/proxy for unconverted OCaml values 44 @interface OCamlValue : NSObject { 45 long _v; 46 } 47 - initWithValue:(long)v; 48 49 - (void *)getField:(long)i withType:(char)t; 50 // get value by position. See ocamlCall for list of type conversion codes 51 52 - (long)count; 53 // count of items in array 54 55 - (long)value; 56 // returns Ocaml value directly -- not safe to use except in direct callback from OCaml 57 // (i.e. in the OCaml thread) 58 @end