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