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