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