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