/tutorial/external/C/example2.e

http://github.com/tybor/Liberty · Specman e · 187 lines · 130 code · 20 blank · 37 comment · 0 complexity · 2280cc99c7c5d7daaac852960087a79e MD5 · raw file

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