/tutorial/external/C/example6.e

http://github.com/tybor/Liberty · Specman e · 70 lines · 50 code · 8 blank · 12 comment · 0 complexity · 933304167592e355d0ce83a1dc3dcc0d MD5 · raw file

  1. class EXAMPLE6
  2. --
  3. -- How to use C struct from Eiffel.
  4. --
  5. -- To compile this file:
  6. --
  7. -- compile example6 c_glue6.c
  8. --
  9. create {ANY}
  10. make
  11. feature {ANY}
  12. point_pointer: POINTER
  13. make
  14. local
  15. r: REAL; size: INTEGER
  16. do
  17. -- Calling the C `malloc_point' function from Eiffel:
  18. point_pointer := malloc_point
  19. check
  20. point_pointer.is_not_null
  21. end
  22. -- Setting fields of the Point* struct:
  23. set_x(point_pointer, 1.0)
  24. -- Getting the x field with the `get_x' function:
  25. r := get_x(point_pointer)
  26. io.put_string(r.to_string + "%N")
  27. -- Getting the C sizeof using an inline:
  28. size := sizeof_point
  29. io.put_string("sizeof(Point) = " + size.to_string + "%N")
  30. end
  31. feature {}
  32. malloc_point: POINTER
  33. -- Call the C `malloc_point' function (see c_glue4.c).
  34. external "[
  35. C use "c_glue6.h"
  36. ]"
  37. ensure
  38. Result.is_not_null
  39. end
  40. set_x (point_star: POINTER; x_value: REAL)
  41. require
  42. point_star.is_not_null
  43. external "[
  44. C struct Point access x type float use "c_glue6.h"
  45. ]"
  46. end
  47. get_x (point_star: POINTER): REAL
  48. external "[
  49. C struct Point access x use "c_glue6.h"
  50. ]"
  51. end
  52. sizeof_point: INTEGER
  53. external "[
  54. C
  55. inline
  56. use "c_glue6.h"
  57. ]"
  58. alias "sizeof(Point)"
  59. ensure
  60. Result >= 0
  61. end
  62. end -- class EXAMPLE6