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); |
53004c3f |
14 | float method(area) (Shape self); |
96ec74e5 |
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 | { |
e0467a3b |
59 | I_am(self, Tri); |
b27abb8a |
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 | { |
e0467a3b |
67 | I_am(self, Rect); |
b27abb8a |
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 | { |
e0467a3b |
75 | I_am(self, 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 | { |
e0467a3b |
83 | I_am(self, Tri); |
96ec74e5 |
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 | { |
e0467a3b |
95 | I_am(self, Rect); |
b27abb8a |
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 | { |
e0467a3b |
105 | I_am(self, Circ); |
96ec74e5 |
106 | |
107 | self->x1 += x; |
108 | self->y1 += y; |
b27abb8a |
109 | }; |
110 | |
96ec74e5 |
111 | str descTri(Shape community) |
112 | { |
e0467a3b |
113 | I_am(self, Tri); |
53004c3f |
114 | |
115 | return self->name; |
96ec74e5 |
116 | } |
117 | |
118 | str descRect(Shape community) |
119 | { |
e0467a3b |
120 | I_am(self, Rect); |
53004c3f |
121 | |
122 | return self->desc; |
96ec74e5 |
123 | } |
124 | |
125 | str descCirc(Shape community) |
126 | { |
e0467a3b |
127 | I_am(self, Circ); |
53004c3f |
128 | |
129 | return self->comment; |
96ec74e5 |
130 | } |
131 | |
53004c3f |
132 | float calcAreaTri(Shape community) |
96ec74e5 |
133 | { |
e0467a3b |
134 | I_am(self, Tri); |
53004c3f |
135 | |
96ec74e5 |
136 | return 0; |
137 | } |
138 | |
53004c3f |
139 | float calcAreaRect(Shape community) |
96ec74e5 |
140 | { |
e0467a3b |
141 | I_am(self, Rect); |
53004c3f |
142 | |
e0467a3b |
143 | return (self->x2 - self->x1)*(self->y2 - self->y1); |
96ec74e5 |
144 | } |
145 | |
53004c3f |
146 | float calcAreaCirc(Shape community) |
96ec74e5 |
147 | { |
e0467a3b |
148 | I_am(self, Circ); |
53004c3f |
149 | |
e0467a3b |
150 | return PI * self->r * self->r; |
96ec74e5 |
151 | } |
152 | |
153 | void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3) |
b27abb8a |
154 | { |
e0467a3b |
155 | I_am(self, Tri); |
96ec74e5 |
156 | |
b27abb8a |
157 | self->x1 = x1; |
158 | self->y1 = y1; |
159 | self->x2 = x2; |
160 | self->y2 = y2; |
161 | self->x3 = x3; |
162 | self->y3 = y3; |
b27abb8a |
163 | } |
164 | |
96ec74e5 |
165 | void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2) |
b27abb8a |
166 | { |
e0467a3b |
167 | I_am(self, Rect); |
b27abb8a |
168 | |
169 | self->x1 = x1; |
170 | self->y1 = y1; |
171 | self->x2 = x2; |
172 | self->y2 = y2; |
96ec74e5 |
173 | } |
174 | |
175 | void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3) |
176 | { |
e0467a3b |
177 | I_am(self, Circ); |
96ec74e5 |
178 | |
179 | self->x1 = x1; |
180 | self->y1 = y1; |
181 | self->r = r; |
182 | } |
183 | |
184 | void nameRect(Shape community, str name) |
185 | { |
e0467a3b |
186 | I_am(self, Rect); |
96ec74e5 |
187 | |
188 | self->desc = name; |
189 | } |
190 | |
191 | /* Example of using object interface methods from inside constructor methods */ |
192 | |
24cfa6bd |
193 | construct(Tri,Interface) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3) |
96ec74e5 |
194 | { |
195 | self->name = "TRIANGLE"; |
196 | interface_of(self)->count++; |
197 | _(reset, self, x1, y1, x2, y2, x3, y3); |
198 | |
199 | return self; |
200 | } |
201 | |
24cfa6bd |
202 | construct(Rect,Interface) (Rect self, int x1, int y1, int x2, int y2) |
96ec74e5 |
203 | { |
204 | self->desc = "rectangle"; |
205 | interface_of(self)->count++; |
206 | _(reset, self, x1, y1, x2, y2, 0, 0); |
207 | |
208 | return self; |
209 | } |
210 | |
24cfa6bd |
211 | construct(Circ,Interface) (Circ self, int x1, int y1, int r) |
96ec74e5 |
212 | { |
213 | self->comment = "Circle"; |
214 | interface_of(self)->count++; |
215 | _(reset, self, x1, y1, r, 0, 0, 0); |
b27abb8a |
216 | |
217 | return self; |
218 | } |
219 | |
96ec74e5 |
220 | /* registration of implemented methods to three interfaces of the same type */ |
221 | |
222 | Interface triInterface(void) |
223 | { |
224 | Get_mem(this, Interface); |
225 | |
226 | this->draw = drawTri; |
227 | this->move = moveTri; |
228 | this->desc = descTri; |
229 | this->rename = NULL; |
230 | this->reset = setTri; |
231 | this->area = calcAreaTri; |
232 | this->count = 0; |
233 | |
234 | return this; |
235 | } |
236 | |
237 | Interface rectInterface(void) |
238 | { |
239 | Get_mem(this, Interface); |
240 | |
241 | this->draw = drawRect; |
242 | this->move = moveRect; |
243 | this->desc = descRect; |
244 | this->rename = nameRect; |
245 | this->reset = setRect; |
246 | this->area = calcAreaRect; |
247 | this->count = 0; |
248 | |
249 | return this; |
250 | } |
251 | |
252 | Interface circInterface(void) |
253 | { |
254 | Get_mem(this, Interface); |
255 | |
256 | this->draw = drawCirc; |
257 | this->move = moveCirc; |
258 | this->desc = descCirc; |
259 | this->rename = NULL; |
260 | this->reset = setCirc; |
261 | this->area = calcAreaCirc; |
262 | this->count = 0; |
263 | |
264 | return this; |
265 | } |
266 | |
267 | /* usage of objects inside C<<1 program */ |
268 | |
b27abb8a |
269 | program |
270 | { |
96ec74e5 |
271 | Interface triangles = triInterface(); |
272 | Interface rectangles = rectInterface(); |
273 | Interface circles = circInterface(); |
274 | Object_list all = NULL, one; |
b27abb8a |
275 | |
96ec74e5 |
276 | one = get_mem(Object_list); |
277 | one->object = get_obj_as(Shape, Rect, rectangles, 0, 10, 1, 11); |
278 | append(one, all); |
279 | |
280 | one = get_mem(Object_list); |
281 | one->object = get_obj_as(Shape, Tri, triangles, 0, 0, 0, 4, 3, 0); |
282 | append(one, all); |
283 | |
284 | one = get_mem(Object_list); |
285 | one->object = get_obj_as(Shape, Rect, rectangles, 10, 0, 11, 1); |
286 | append(one, all); |
287 | |
288 | one = get_mem(Object_list); |
53004c3f |
289 | one->object = get_obj_as(Shape, Circ, circles, 0, 10, 1); |
96ec74e5 |
290 | append(one, all); |
291 | |
292 | printf("We have created %d triangles, %d rectangles and %d circles:\n", |
293 | triangles->count, rectangles->count, circles->count); |
294 | |
295 | for_each(one, all) |
b27abb8a |
296 | { |
96ec74e5 |
297 | _(draw, one->object); |
53004c3f |
298 | printf("Area of this %s is %f (square pixels).\n", _(desc, one->object), _(area, one->object)); |
96ec74e5 |
299 | printf("We have created %d instances of this type of object.\n", interface_of(one->object)->count); |
300 | |
301 | if(interface_of(one->object) == rectangles) |
302 | { |
303 | _(reset, one->object, 0, 0, 1, 1, 0, 0); |
304 | _(rename, one->object, "Renamed rectangle"); |
305 | } |
306 | else |
307 | { |
308 | _(move, one->object, 10, 10); |
309 | } |
b27abb8a |
310 | } |
96ec74e5 |
311 | |
312 | print("Rectangles were reset to new size and renamed, other objects were moved:"); |
313 | for_each(one, all) |
314 | { |
315 | _(draw, one->object); |
316 | } |
b27abb8a |
317 | } |