PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/external/C/example4.e

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