def_interface() in object demo
[svn/Cll1h/.git] / demos / objects / objects-hierarchy.c
... / ...
CommitLineData
1#include "cll1.h"
2
3/* This is example of advanced C<<1 object oriented features... */
4
5def_type(Point);
6def_type(Shape);
7
8def_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
16def_mem(ShapeInterface)
17{
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);
23 float method(area) (Shape self);
24 int count;
25};
26
27/* This is example of hierarchical object architecture in C<<1 */
28
29def_obj(Point)
30{
31 interface(PointInterface);
32 int x1, y1;
33 str desc;
34}
35
36def_mem(Tri)
37{
38 interface(ShapeInterface);
39 Point point;
40 int x2, y2, x3, y3;
41};
42
43def_mem(Rect)
44{
45 interface(ShapeInterface);
46 Point point;
47 int x2, y2;
48};
49
50def_mem(Circ)
51{
52 interface(ShapeInterface);
53 Point point;
54 int r;
55};
56
57def_community(Shape, ShapeInterface);
58
59/* Note: interface(ShapeInterface); has to be at first position in all objects asociated in community */
60
61def_mem(Object_list)
62{
63 Shape object;
64 list(Object_list);
65};
66
67/* implementation of methods */
68
69void drawTri(Shape community)
70{
71 I_am(self, Tri);
72
73 printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n",
74 self->point->desc, self->point->x, self->point->y, self->x2, self->y2, self->x3, self->y3);
75};
76
77void drawRect(Shape community)
78{
79 I_am(self, Rect);
80
81 printf("Drawing %s: %d,%d - %d,%d .\n",
82 self->point->desc, self->x1, self->y1, self->x2, self->y2);
83};
84
85void drawCirc(Shape community)
86{
87 I_am(self, Circ);
88
89 printf("Drawing %s: %d,%d - r=%d .\n",
90 self->comment, self->x1, self->y1, self->r);
91};
92
93void moveTri(Shape community, int x, int y)
94{
95 I_am(self, Tri);
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;
103};
104
105void moveRect(Shape community, int x, int y)
106{
107 I_am(self, Rect);
108
109 self->x1 += x;
110 self->y1 += y;
111 self->x2 += x;
112 self->y2 += y;
113};
114
115void moveCirc(Shape community, int x, int y)
116{
117 I_am(self, Circ);
118
119 self->x1 += x;
120 self->y1 += y;
121};
122
123str descTri(Shape community)
124{
125 I_am(self, Tri);
126
127 return _(desc, self->point);
128}
129
130str descRect(Shape community)
131{
132 I_am(self, Rect);
133
134 return _(desc, self->point);
135}
136
137str descCirc(Shape community)
138{
139 I_am(self, Circ);
140
141 return _(desc, self->point);
142}
143
144float calcAreaTri(Shape community)
145{
146 I_am(self, Tri);
147
148 return 0;
149}
150
151float calcAreaRect(Shape community)
152{
153 I_am(self, Rect);
154
155 return (self->x2 - self->x1)*(self->y2 - self->y1);
156}
157
158float calcAreaCirc(Shape community)
159{
160 I_am(self, Circ);
161
162 return PI * self->r * self->r;
163}
164
165void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3)
166{
167 I_am(self, Tri);
168
169 self->x1 = x1;
170 self->y1 = y1;
171 self->x2 = x2;
172 self->y2 = y2;
173 self->x3 = x3;
174 self->y3 = y3;
175}
176
177void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2)
178{
179 I_am(self, Rect);
180
181 self->x1 = x1;
182 self->y1 = y1;
183 self->x2 = x2;
184 self->y2 = y2;
185}
186
187void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3)
188{
189 I_am(self, Circ);
190
191 self->x1 = x1;
192 self->y1 = y1;
193 self->r = r;
194}
195
196void nameRect(Shape community, str newname)
197{
198 I_am(self, Rect);
199
200 _(name, self->point, newname);
201}
202
203/* Example of using object interface methods from inside constructor methods */
204
205construct(Tri,ShapeInterface) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3)
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
214construct(Rect,ShapeInterface) (Rect self, int x1, int y1, int x2, int y2)
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
223construct(Circ,ShapeInterface) (Circ self, int x1, int y1, int r)
224{
225 self->comment = "Circle";
226 interface_of(self)->count++;
227 _(reset, self, x1, y1, r, 0, 0, 0);
228
229 return self;
230}
231
232/* registration of implemented methods to three interfaces of the same type */
233
234PointInterface pointInterface(void)
235{
236 Get_mem(this, PointInterface);
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
249ShapeInterface triInterface(void)
250{
251 Get_mem(this, ShapeInterface);
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
264ShapeInterface rectInterface(void)
265{
266 Get_mem(this, ShapeInterface);
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
279ShapeInterface circInterface(void)
280{
281 Get_mem(this, ShapeInterface);
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
296program
297{
298 ShapeInterface triangles = triInterface();
299 ShapeInterface rectangles = rectInterface();
300 ShapeInterface circles = circInterface();
301 Object_list all = NULL, one;
302
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);
316 one->object = get_obj_as(Shape, Circ, circles, 0, 10, 1);
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)
323 {
324 _(draw, one->object);
325 printf("Area of this %s is %f (square pixels).\n", _(desc, one->object), _(area, one->object));
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 }
337 }
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 }
344}
This page took 0.10495 seconds and 4 git commands to generate.