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