/tutorial/external/C/example1.e

http://github.com/tybor/Liberty · Specman e · 117 lines · 65 code · 16 blank · 36 comment · 1 complexity · 187dcb40b7e71020373884f17acd358a MD5 · raw file

  1. class 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. inherit
  15. ANY
  16. create {ANY}
  17. make
  18. feature {ANY}
  19. make
  20. local
  21. i: INTEGER; c: CHARACTER
  22. do
  23. -- To send an INTEGER to the C world:
  24. integer_to_c(6)
  25. -- To send a CHARACTER to the C world:
  26. character_to_c('a')
  27. -- To send a BOOLEAN to the C world:
  28. boolean_to_c(True)
  29. -- To send a REAL (i.e. a REAL_64) to the C world:
  30. real_64_to_c(3.5)
  31. -- To send a REAL_32 to the C world:
  32. real_32_to_c({REAL_32 3.5})
  33. -- To send the internal storage of a STRING to the C world:
  34. string_storage_to_c(("Hi C World %N").to_external)
  35. -- To send `Current' address to the C world:
  36. any_to_c(Current)
  37. -- To send a STRING to the C world:
  38. any_to_c(create {ANY_STRING}.make_from_string("Hi"))
  39. -- To get an INTEGER from the C world:
  40. i := c_int2eiffel
  41. io.put_integer(i)
  42. -- To get an CHARACTER from the C world:
  43. c := c_char2eiffel
  44. io.put_character(c)
  45. -- To pass the address on an INTEGER attribute:
  46. set_integer_attribute($integer_attribute)
  47. std_output.put_integer(integer_attribute)
  48. std_output.put_new_line
  49. -- Calling the hello C function:
  50. if hello then
  51. std_output.put_string(" C man !%N")
  52. end
  53. end
  54. feature {}
  55. integer_to_c (i: INTEGER)
  56. -- Call the C `integer_to_c' function with `i' as argument (see c_glue1.c).
  57. external "C"
  58. end
  59. character_to_c (c: CHARACTER)
  60. -- Call the C `character_to_c' function with `c' as argument (see c_glue1.c).
  61. external "C"
  62. end
  63. boolean_to_c (b: BOOLEAN)
  64. -- Call the C `boolean_to_c' function with `b' as argument (see c_glue1.c).
  65. external "C"
  66. end
  67. real_32_to_c (r: REAL_32)
  68. -- Call the C `real_32_to_c' function with `r' as argument (see c_glue1.c).
  69. external "C"
  70. end
  71. real_64_to_c (d: REAL_64)
  72. -- Call the C `real_64_to_c' function with `d' as argument (see c_glue1.c).
  73. external "C"
  74. end
  75. string_storage_to_c (s: POINTER)
  76. -- Call the C `string_storage_to_c' function with `s' as argument (see
  77. -- c_glue1.c).
  78. external "C"
  79. end
  80. any_to_c (a: ANY)
  81. -- Call the C `any_to_c' function with `a' as argument (see c_glue1.c).
  82. external "C"
  83. end
  84. c_int2eiffel: INTEGER
  85. -- Call the C `c_int2eiffel' function (see c_glue1.c).
  86. external "C"
  87. end
  88. c_char2eiffel: CHARACTER
  89. -- Call the C `c_char2eiffel' function (see c_glue1.c).
  90. external "C"
  91. end
  92. integer_attribute: INTEGER
  93. set_integer_attribute (integer_pointer: POINTER)
  94. -- Call the C `set_integer_attribute' function with the address of an
  95. -- INTEGER attribute (see c_glue1.c).
  96. external "C"
  97. end
  98. hello: BOOLEAN
  99. external "C"
  100. end
  101. end -- class EXAMPLE1