/tutorial/external/C/example2.e
Specman e | 187 lines | 130 code | 20 blank | 37 comment | 0 complexity | 2280cc99c7c5d7daaac852960087a79e MD5 | raw file
1class EXAMPLE2 2 -- 3 -- How to use the Eiffel $ operator which can be used to pass the address of 4 -- an Eiffel feature (to be called later from C). 5 -- 6 -- To compile this file: 7 -- 8 -- compile example2 c_glue2.c 9 -- 10 -- You may also prefer: 11 -- 12 -- gcc -c c_glue.c 13 -- compile example2 c_glue2.o 14 -- 15 16create {ANY} 17 make 18 19feature {ANY} 20 make 21 local 22 v: INTEGER 23 do 24 -- When a C function need to write some attribute: 25 io.put_string("Example #0%N") 26 write_integer_attribute($integer_attribute) 27 io.put_integer(integer_attribute) 28 io.put_new_line 29 -- Call back of `procedure_1': 30 io.put_string("Example #1%N") 31 call_back_1(Current, $procedure_1) 32 -- Call back of `function_1': 33 io.put_string("Example #2%N") 34 io.put_integer(call_back_2(Current, $function_1)) 35 io.put_new_line 36 -- Call back of `function_2': 37 io.put_string("Example #3%N") 38 io.put_integer(call_back_3(Current, $function_2, "Eiffel STRING%N")) 39 io.put_new_line 40 -- Call back of `function_3': 41 io.put_string("Example #4%N") 42 io.put_integer(call_back_4(Current, $function_3, "Eiffel STRIN")) 43 io.put_new_line 44 -- Call back of `routine5': 45 io.put_string("Example #5%N") 46 call_back_5(Current, $routine5) 47 io.put_new_line 48 -- Call back of `once_routine6': 49 io.put_string("Example #6%N") 50 call_back_6(Current, $once_routine6) 51 call_back_6(Current, $once_routine6) 52 call_back_6(Current, $once_routine6) 53 call_back_6(Current, $once_routine6) 54 check 55 integer_attribute = 6 56 end 57 -- Call back of `once_routine7': 58 io.put_string("Example #7%N") 59 v := call_back_7(Current, $once_routine7) 60 check 61 v = 10 62 end 63 v := call_back_7(Current, $once_routine7) 64 check 65 v = 10 66 end 67 io.put_integer(v) 68 io.put_new_line 69 -- Call back of `once_routine8': 70 io.put_string("Example #8%N") 71 v := call_back_7(Current, $once_routine8) 72 check 73 v = 10 74 end 75 v := call_back_7(Current, $once_routine8) 76 check 77 v = 10 78 end 79 io.put_integer(v) 80 io.put_new_line 81 end 82 83feature {ANY} 84 integer_attribute: INTEGER 85 86feature {} 87 write_integer_attribute (integer_pointer: POINTER) 88 -- (Corresponding C function defined in c_glue2.c) 89 external "C" 90 end 91 92 procedure_1 93 -- A procedure which only needs `Current'. 94 do 95 io.put_string("From `procedure_1' :%N") 96 io.put_integer(integer_attribute) 97 io.put_new_line 98 end 99 100 call_back_1 (c: like Current; a_routine: POINTER) 101 -- (Corresponding C function defined in c_glue2.c) 102 external "C" 103 end 104 105 function_1: INTEGER 106 -- A function which only needs `Current'. 107 do 108 Result := integer_attribute + 1 109 end 110 111 call_back_2 (c: like Current; a_routine: POINTER): INTEGER 112 -- (Corresponding C function defined in c_glue2.c) 113 external "C" 114 end 115 116 function_2 (s: STRING): INTEGER 117 -- A function which needs `Current' and a STRING as argument. 118 require 119 s.count > 0 120 do 121 io.put_string(s) 122 Result := integer_attribute + s.count 123 end 124 125 call_back_3 (c: like Current; a_routine: POINTER; str: STRING): INTEGER 126 -- (Corresponding C function defined in c_glue2.c) 127 external "C" 128 end 129 130 function_3 (s: STRING; c: CHARACTER): INTEGER 131 -- A function which needs two arguments. 132 require 133 s.count > 0 134 do 135 s.extend(c) 136 s.extend('%N') 137 io.put_string(s) 138 Result := s.count 139 end 140 141 call_back_4 (c: like Current; a_routine: POINTER; str: STRING): INTEGER 142 -- (Corresponding C function defined in c_glue2.c) 143 external "C" 144 end 145 146 routine5 (other: like Current) 147 -- A procedure. 148 require 149 other = Current 150 do 151 io.put_integer(integer_attribute + other.integer_attribute) 152 end 153 154 call_back_5 (c: like Current; a_routine: POINTER) 155 -- (Corresponding C function defined in c_glue2.c) 156 external "C" 157 end 158 159 once_routine6 160 once 161 integer_attribute := integer_attribute + 4 162 io.put_string("Only once routine6.%N") 163 end 164 165 call_back_6 (c: like Current; a_routine: POINTER) 166 -- (Corresponding C function defined in c_glue2.c) 167 external "C" 168 end 169 170 once_routine7: INTEGER 171 once 172 Result := integer_attribute + 4 173 end 174 175 call_back_7 (c: like Current; a_routine: POINTER): INTEGER 176 -- (Corresponding C function defined in c_glue2.c) 177 external "C" 178 end 179 180 once_routine8: INTEGER 181 -- Just to check that such a pre-computable once 182 -- function causes no pain. 183 once 184 Result := 10 185 end 186 187end -- class EXAMPLE2