/tutorial/external/C/example1.e
Specman e | 117 lines | 65 code | 16 blank | 36 comment | 1 complexity | 187dcb40b7e71020373884f17acd358a MD5 | raw file
1class EXAMPLE1 2 -- 3 -- How to use external "C" features (i.e. calling C from Eiffel). 4 -- 5 -- To compile this file: 6 -- 7 -- compile example1 c_glue1.c 8 -- 9 -- You may also prefer: 10 -- 11 -- gcc -c c_glue1.c 12 -- compile example1 c_glue1.o 13 -- 14 15inherit 16 ANY 17 18create {ANY} 19 make 20 21feature {ANY} 22 make 23 local 24 i: INTEGER; c: CHARACTER 25 do 26 -- To send an INTEGER to the C world: 27 integer_to_c(6) 28 -- To send a CHARACTER to the C world: 29 character_to_c('a') 30 -- To send a BOOLEAN to the C world: 31 boolean_to_c(True) 32 -- To send a REAL (i.e. a REAL_64) to the C world: 33 real_64_to_c(3.5) 34 -- To send a REAL_32 to the C world: 35 real_32_to_c({REAL_32 3.5}) 36 -- To send the internal storage of a STRING to the C world: 37 string_storage_to_c(("Hi C World %N").to_external) 38 -- To send `Current' address to the C world: 39 any_to_c(Current) 40 -- To send a STRING to the C world: 41 any_to_c(create {ANY_STRING}.make_from_string("Hi")) 42 -- To get an INTEGER from the C world: 43 i := c_int2eiffel 44 io.put_integer(i) 45 -- To get an CHARACTER from the C world: 46 c := c_char2eiffel 47 io.put_character(c) 48 -- To pass the address on an INTEGER attribute: 49 set_integer_attribute($integer_attribute) 50 std_output.put_integer(integer_attribute) 51 std_output.put_new_line 52 -- Calling the hello C function: 53 if hello then 54 std_output.put_string(" C man !%N") 55 end 56 end 57 58feature {} 59 integer_to_c (i: INTEGER) 60 -- Call the C `integer_to_c' function with `i' as argument (see c_glue1.c). 61 external "C" 62 end 63 64 character_to_c (c: CHARACTER) 65 -- Call the C `character_to_c' function with `c' as argument (see c_glue1.c). 66 external "C" 67 end 68 69 boolean_to_c (b: BOOLEAN) 70 -- Call the C `boolean_to_c' function with `b' as argument (see c_glue1.c). 71 external "C" 72 end 73 74 real_32_to_c (r: REAL_32) 75 -- Call the C `real_32_to_c' function with `r' as argument (see c_glue1.c). 76 external "C" 77 end 78 79 real_64_to_c (d: REAL_64) 80 -- Call the C `real_64_to_c' function with `d' as argument (see c_glue1.c). 81 external "C" 82 end 83 84 string_storage_to_c (s: POINTER) 85 -- Call the C `string_storage_to_c' function with `s' as argument (see 86 -- c_glue1.c). 87 external "C" 88 end 89 90 any_to_c (a: ANY) 91 -- Call the C `any_to_c' function with `a' as argument (see c_glue1.c). 92 external "C" 93 end 94 95 c_int2eiffel: INTEGER 96 -- Call the C `c_int2eiffel' function (see c_glue1.c). 97 external "C" 98 end 99 100 c_char2eiffel: CHARACTER 101 -- Call the C `c_char2eiffel' function (see c_glue1.c). 102 external "C" 103 end 104 105 integer_attribute: INTEGER 106 107 set_integer_attribute (integer_pointer: POINTER) 108 -- Call the C `set_integer_attribute' function with the address of an 109 -- INTEGER attribute (see c_glue1.c). 110 external "C" 111 end 112 113 hello: BOOLEAN 114 external "C" 115 end 116 117end -- class EXAMPLE1