96ec74e5 |
1 | #include "cll1.h" |
b27abb8a |
2 | |
96ec74e5 |
3 | /* This is example of advanced C<<1 object oriented features... */ |
b27abb8a |
4 | |
96ec74e5 |
5 | def_type(Shape); |
6 | |
7 | def_mem(Interface) |
b27abb8a |
8 | { |
96ec74e5 |
9 | void method(draw) (Shape self); |
10 | void method(move) (Shape self, int x, int y); |
11 | str method(desc) (Shape self); |
12 | void method(rename) (Shape self, str name); |
13 | void method(reset) (Shape self, int x1, int y1, int x2, int y2, int x3, int y3); |
14 | int method(area) (Shape self); |
15 | int count; |
b27abb8a |
16 | }; |
17 | |
96ec74e5 |
18 | /* We intentionaly define heterogenous objects, which share only the interface |
19 | binding. Not only we can use different order of variables, and different |
20 | names - but we can even store these data on remote systems, etc. */ |
21 | |
b27abb8a |
22 | def_mem(Tri) |
23 | { |
96ec74e5 |
24 | interface(Interface); |
b27abb8a |
25 | int x1, y1, x2, y2, x3, y3; |
96ec74e5 |
26 | str name; |
b27abb8a |
27 | }; |
28 | |
29 | def_mem(Rect) |
30 | { |
96ec74e5 |
31 | interface(Interface); |
32 | str desc; |
b27abb8a |
33 | int x1, y1, x2, y2; |
34 | }; |
35 | |
96ec74e5 |
36 | def_mem(Circ) |
37 | { |
38 | interface(Interface); |
39 | int x1, y1; |
40 | str comment; |
41 | int r; |
42 | }; |
b27abb8a |
43 | |
96ec74e5 |
44 | def_community(Shape, Interface); |
b27abb8a |
45 | |
96ec74e5 |
46 | /* Note: interface(Interface); has to be at first position in all objects asociated in community */ |
b27abb8a |
47 | |
48 | def_mem(Object_list) |
49 | { |
96ec74e5 |
50 | Shape object; |
b27abb8a |
51 | list(Object_list); |
52 | }; |
53 | |
b27abb8a |
54 | |
96ec74e5 |
55 | /* implementation of methods */ |
b27abb8a |
56 | |
96ec74e5 |
57 | void drawTri(Shape community) |
b27abb8a |
58 | { |
59 | I_am(Tri); |
60 | |
96ec74e5 |
61 | printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n", |
62 | self->name, self->x1, self->y1, self->x2, self->y2, self->x3, self->y3); |
b27abb8a |
63 | }; |
64 | |
96ec74e5 |
65 | void drawRect(Shape community) |
b27abb8a |
66 | { |
67 | I_am(Rect); |
68 | |
96ec74e5 |
69 | printf("Drawing %s: %d,%d - %d,%d .\n", |
70 | self->desc, self->x1, self->y1, self->x2, self->y2); |
b27abb8a |
71 | }; |
72 | |
96ec74e5 |
73 | void drawCirc(Shape community) |
b27abb8a |
74 | { |
96ec74e5 |
75 | I_am(Circ); |
b27abb8a |
76 | |
96ec74e5 |
77 | printf("Drawing %s: %d,%d - r=%d .\n", |
78 | self->comment, self->x1, self->y1, self->r); |
79 | }; |
b27abb8a |
80 | |
96ec74e5 |
81 | void moveTri(Shape community, int x, int y) |
82 | { |
83 | I_am(Tri); |
84 | |
85 | self->x1 += x; |
86 | self->y1 += y; |
87 | self->x2 += x; |
88 | self->y2 += y; |
89 | self->x3 += x; |
90 | self->y3 += y; |
b27abb8a |
91 | }; |
92 | |
96ec74e5 |
93 | void moveRect(Shape community, int x, int y) |
b27abb8a |
94 | { |
95 | I_am(Rect); |
96 | |
96ec74e5 |
97 | self->x1 += x; |
98 | self->y1 += y; |
99 | self->x2 += x; |
100 | self->y2 += y; |
101 | }; |
b27abb8a |
102 | |
96ec74e5 |
103 | void moveCirc(Shape community, int x, int y) |
104 | { |
105 | I_am(Circ); |
106 | |
107 | self->x1 += x; |
108 | self->y1 += y; |
b27abb8a |
109 | }; |
110 | |
96ec74e5 |
111 | str descTri(Shape community) |
112 | { |
113 | return ""; |
114 | } |
115 | |
116 | str descRect(Shape community) |
117 | { |
118 | return ""; |
119 | } |
120 | |
121 | str descCirc(Shape community) |
122 | { |
123 | return ""; |
124 | } |
125 | |
126 | int calcAreaTri(Shape community) |
127 | { |
128 | return 0; |
129 | } |
130 | |
131 | int calcAreaRect(Shape community) |
132 | { |
133 | return 0; |
134 | } |
135 | |
136 | int calcAreaCirc(Shape community) |
137 | { |
138 | return 0; |
139 | } |
140 | |
141 | void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3) |
b27abb8a |
142 | { |
143 | I_am(Tri); |
96ec74e5 |
144 | |
b27abb8a |
145 | self->x1 = x1; |
146 | self->y1 = y1; |
147 | self->x2 = x2; |
148 | self->y2 = y2; |
149 | self->x3 = x3; |
150 | self->y3 = y3; |
b27abb8a |
151 | } |
152 | |
96ec74e5 |
153 | void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2) |
b27abb8a |
154 | { |
155 | I_am(Rect); |
156 | |
157 | self->x1 = x1; |
158 | self->y1 = y1; |
159 | self->x2 = x2; |
160 | self->y2 = y2; |
96ec74e5 |
161 | } |
162 | |
163 | void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3) |
164 | { |
165 | I_am(Circ); |
166 | |
167 | self->x1 = x1; |
168 | self->y1 = y1; |
169 | self->r = r; |
170 | } |
171 | |
172 | void nameRect(Shape community, str name) |
173 | { |
174 | I_am(Rect); |
175 | |
176 | self->desc = name; |
177 | } |
178 | |
179 | /* Example of using object interface methods from inside constructor methods */ |
180 | |
181 | construct(Tri) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3) |
182 | { |
183 | self->name = "TRIANGLE"; |
184 | interface_of(self)->count++; |
185 | _(reset, self, x1, y1, x2, y2, x3, y3); |
186 | |
187 | return self; |
188 | } |
189 | |
190 | construct(Rect) (Rect self, int x1, int y1, int x2, int y2) |
191 | { |
192 | self->desc = "rectangle"; |
193 | interface_of(self)->count++; |
194 | _(reset, self, x1, y1, x2, y2, 0, 0); |
195 | |
196 | return self; |
197 | } |
198 | |
199 | construct(Circ) (Circ self, int x1, int y1, int r) |
200 | { |
201 | self->comment = "Circle"; |
202 | interface_of(self)->count++; |
203 | _(reset, self, x1, y1, r, 0, 0, 0); |
b27abb8a |
204 | |
205 | return self; |
206 | } |
207 | |
96ec74e5 |
208 | /* registration of implemented methods to three interfaces of the same type */ |
209 | |
210 | Interface triInterface(void) |
211 | { |
212 | Get_mem(this, Interface); |
213 | |
214 | this->draw = drawTri; |
215 | this->move = moveTri; |
216 | this->desc = descTri; |
217 | this->rename = NULL; |
218 | this->reset = setTri; |
219 | this->area = calcAreaTri; |
220 | this->count = 0; |
221 | |
222 | return this; |
223 | } |
224 | |
225 | Interface rectInterface(void) |
226 | { |
227 | Get_mem(this, Interface); |
228 | |
229 | this->draw = drawRect; |
230 | this->move = moveRect; |
231 | this->desc = descRect; |
232 | this->rename = nameRect; |
233 | this->reset = setRect; |
234 | this->area = calcAreaRect; |
235 | this->count = 0; |
236 | |
237 | return this; |
238 | } |
239 | |
240 | Interface circInterface(void) |
241 | { |
242 | Get_mem(this, Interface); |
243 | |
244 | this->draw = drawCirc; |
245 | this->move = moveCirc; |
246 | this->desc = descCirc; |
247 | this->rename = NULL; |
248 | this->reset = setCirc; |
249 | this->area = calcAreaCirc; |
250 | this->count = 0; |
251 | |
252 | return this; |
253 | } |
254 | |
255 | /* usage of objects inside C<<1 program */ |
256 | |
b27abb8a |
257 | program |
258 | { |
96ec74e5 |
259 | Interface triangles = triInterface(); |
260 | Interface rectangles = rectInterface(); |
261 | Interface circles = circInterface(); |
262 | Object_list all = NULL, one; |
b27abb8a |
263 | |
96ec74e5 |
264 | one = get_mem(Object_list); |
265 | one->object = get_obj_as(Shape, Rect, rectangles, 0, 10, 1, 11); |
266 | append(one, all); |
267 | |
268 | one = get_mem(Object_list); |
269 | one->object = get_obj_as(Shape, Tri, triangles, 0, 0, 0, 4, 3, 0); |
270 | append(one, all); |
271 | |
272 | one = get_mem(Object_list); |
273 | one->object = get_obj_as(Shape, Rect, rectangles, 10, 0, 11, 1); |
274 | append(one, all); |
275 | |
276 | one = get_mem(Object_list); |
277 | one->object = get_obj_as(Shape, Circ, circles, 10, 0, 5); |
278 | append(one, all); |
279 | |
280 | printf("We have created %d triangles, %d rectangles and %d circles:\n", |
281 | triangles->count, rectangles->count, circles->count); |
282 | |
283 | for_each(one, all) |
b27abb8a |
284 | { |
96ec74e5 |
285 | _(draw, one->object); |
286 | printf("Area of this %s is %d (square pixels).\n", _(desc, one->object), _(area, one->object)); |
287 | printf("We have created %d instances of this type of object.\n", interface_of(one->object)->count); |
288 | |
289 | if(interface_of(one->object) == rectangles) |
290 | { |
291 | _(reset, one->object, 0, 0, 1, 1, 0, 0); |
292 | _(rename, one->object, "Renamed rectangle"); |
293 | } |
294 | else |
295 | { |
296 | _(move, one->object, 10, 10); |
297 | } |
b27abb8a |
298 | } |
96ec74e5 |
299 | |
300 | print("Rectangles were reset to new size and renamed, other objects were moved:"); |
301 | for_each(one, all) |
302 | { |
303 | _(draw, one->object); |
304 | } |
b27abb8a |
305 | } |