#3 some work on object programming demos
[svn/Cll1h/.git] / demos / objects / objects-hierarchy.c
CommitLineData
96ec74e5 1#include "cll1.h"
b27abb8a 2
96ec74e5 3/* This is example of advanced C<<1 object oriented features... */
b27abb8a 4
02ca421a 5def_type(Point);
96ec74e5 6def_type(Shape);
7
8def_mem(Interface)
b27abb8a 9{
96ec74e5 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);
53004c3f 15 float method(area) (Shape self);
96ec74e5 16 int count;
b27abb8a 17};
18
02ca421a 19/* This is example of hierarchical object architecture in C<<1 */
20
21def_obj(Point)
22{
23 interface(Interface);
24 int x1, y1;
25 str desc;
26}
96ec74e5 27
b27abb8a 28def_mem(Tri)
29{
96ec74e5 30 interface(Interface);
02ca421a 31 Point point;
32 int x2, y2, x3, y3;
b27abb8a 33};
34
35def_mem(Rect)
36{
96ec74e5 37 interface(Interface);
02ca421a 38 Point point;
39 int x2, y2;
b27abb8a 40};
41
96ec74e5 42def_mem(Circ)
43{
44 interface(Interface);
02ca421a 45 Point point;
96ec74e5 46 int r;
47};
b27abb8a 48
96ec74e5 49def_community(Shape, Interface);
b27abb8a 50
96ec74e5 51/* Note: interface(Interface); has to be at first position in all objects asociated in community */
b27abb8a 52
53def_mem(Object_list)
54{
96ec74e5 55 Shape object;
b27abb8a 56 list(Object_list);
57};
58
96ec74e5 59/* implementation of methods */
b27abb8a 60
96ec74e5 61void drawTri(Shape community)
b27abb8a 62{
e0467a3b 63 I_am(self, Tri);
b27abb8a 64
96ec74e5 65 printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n",
02ca421a 66 self->point->desc, self->point->x, self->point->y, self->x2, self->y2, self->x3, self->y3);
b27abb8a 67};
68
96ec74e5 69void drawRect(Shape community)
b27abb8a 70{
e0467a3b 71 I_am(self, Rect);
b27abb8a 72
96ec74e5 73 printf("Drawing %s: %d,%d - %d,%d .\n",
02ca421a 74 self->point->desc, self->x1, self->y1, self->x2, self->y2);
b27abb8a 75};
76
96ec74e5 77void drawCirc(Shape community)
b27abb8a 78{
e0467a3b 79 I_am(self, Circ);
b27abb8a 80
96ec74e5 81 printf("Drawing %s: %d,%d - r=%d .\n",
82 self->comment, self->x1, self->y1, self->r);
83};
b27abb8a 84
96ec74e5 85void moveTri(Shape community, int x, int y)
86{
e0467a3b 87 I_am(self, Tri);
96ec74e5 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;
b27abb8a 95};
96
96ec74e5 97void moveRect(Shape community, int x, int y)
b27abb8a 98{
e0467a3b 99 I_am(self, Rect);
b27abb8a 100
96ec74e5 101 self->x1 += x;
102 self->y1 += y;
103 self->x2 += x;
104 self->y2 += y;
105};
b27abb8a 106
96ec74e5 107void moveCirc(Shape community, int x, int y)
108{
e0467a3b 109 I_am(self, Circ);
96ec74e5 110
111 self->x1 += x;
112 self->y1 += y;
b27abb8a 113};
114
96ec74e5 115str descTri(Shape community)
116{
e0467a3b 117 I_am(self, Tri);
53004c3f 118
02ca421a 119 return _(desc, self->point);
96ec74e5 120}
121
122str descRect(Shape community)
123{
e0467a3b 124 I_am(self, Rect);
53004c3f 125
02ca421a 126 return _(desc, self->point);
96ec74e5 127}
128
129str descCirc(Shape community)
130{
e0467a3b 131 I_am(self, Circ);
53004c3f 132
02ca421a 133 return _(desc, self->point);
96ec74e5 134}
135
53004c3f 136float calcAreaTri(Shape community)
96ec74e5 137{
e0467a3b 138 I_am(self, Tri);
53004c3f 139
96ec74e5 140 return 0;
141}
142
53004c3f 143float calcAreaRect(Shape community)
96ec74e5 144{
e0467a3b 145 I_am(self, Rect);
53004c3f 146
e0467a3b 147 return (self->x2 - self->x1)*(self->y2 - self->y1);
96ec74e5 148}
149
53004c3f 150float calcAreaCirc(Shape community)
96ec74e5 151{
e0467a3b 152 I_am(self, Circ);
53004c3f 153
e0467a3b 154 return PI * self->r * self->r;
96ec74e5 155}
156
157void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3)
b27abb8a 158{
e0467a3b 159 I_am(self, Tri);
96ec74e5 160
b27abb8a 161 self->x1 = x1;
162 self->y1 = y1;
163 self->x2 = x2;
164 self->y2 = y2;
165 self->x3 = x3;
166 self->y3 = y3;
b27abb8a 167}
168
96ec74e5 169void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2)
b27abb8a 170{
e0467a3b 171 I_am(self, Rect);
b27abb8a 172
173 self->x1 = x1;
174 self->y1 = y1;
175 self->x2 = x2;
176 self->y2 = y2;
96ec74e5 177}
178
179void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3)
180{
e0467a3b 181 I_am(self, Circ);
96ec74e5 182
183 self->x1 = x1;
184 self->y1 = y1;
185 self->r = r;
186}
187
02ca421a 188void nameRect(Shape community, str newname)
96ec74e5 189{
e0467a3b 190 I_am(self, Rect);
96ec74e5 191
02ca421a 192 _(name, self->point, newname);
96ec74e5 193}
194
195/* Example of using object interface methods from inside constructor methods */
196
24cfa6bd 197construct(Tri,Interface) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3)
96ec74e5 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
24cfa6bd 206construct(Rect,Interface) (Rect self, int x1, int y1, int x2, int y2)
96ec74e5 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
24cfa6bd 215construct(Circ,Interface) (Circ self, int x1, int y1, int r)
96ec74e5 216{
217 self->comment = "Circle";
218 interface_of(self)->count++;
219 _(reset, self, x1, y1, r, 0, 0, 0);
b27abb8a 220
221 return self;
222}
223
96ec74e5 224/* registration of implemented methods to three interfaces of the same type */
225
02ca421a 226Interface 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
96ec74e5 241Interface 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
256Interface 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
271Interface 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
b27abb8a 288program
289{
96ec74e5 290 Interface triangles = triInterface();
291 Interface rectangles = rectInterface();
292 Interface circles = circInterface();
293 Object_list all = NULL, one;
b27abb8a 294
96ec74e5 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);
53004c3f 308 one->object = get_obj_as(Shape, Circ, circles, 0, 10, 1);
96ec74e5 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)
b27abb8a 315 {
96ec74e5 316 _(draw, one->object);
53004c3f 317 printf("Area of this %s is %f (square pixels).\n", _(desc, one->object), _(area, one->object));
96ec74e5 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 }
b27abb8a 329 }
96ec74e5 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 }
b27abb8a 336}
This page took 0.396549 seconds and 4 git commands to generate.