some work
[svn/Cll1h/.git] / demos / objects / objects-community.c
... / ...
CommitLineData
1#include "cll1.h"
2
3/* This is example of advanced C<<1 object oriented features... */
4
5def_interface(Shape, Interface)
6{
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);
12 float method(area) (Shape self);
13 int count;
14};
15
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
20def_mem(Tri)
21{
22 interface(Interface);
23 int x1, y1, x2, y2, x3, y3;
24 str name;
25};
26
27def_mem(Rect)
28{
29 interface(Interface);
30 str desc;
31 int x1, y1, x2, y2;
32};
33
34def_mem(Circ)
35{
36 interface(Interface);
37 int x1, y1;
38 str comment;
39 int r;
40};
41
42def_community(Shape, Interface);
43
44/* Note: interface(Interface); has to be at first position in all objects asociated in community */
45
46def_mem(Object_list)
47{
48 Shape object;
49 list(Object_list);
50};
51
52
53/* implementation of methods */
54
55void drawTri(Shape community)
56{
57 I_am(self, Tri);
58
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);
61};
62
63void drawRect(Shape community)
64{
65 I_am(self, Rect);
66
67 printf("Drawing %s: %d,%d - %d,%d .\n",
68 self->desc, self->x1, self->y1, self->x2, self->y2);
69};
70
71void drawCirc(Shape community)
72{
73 I_am(self, Circ);
74
75 printf("Drawing %s: %d,%d - r=%d .\n",
76 self->comment, self->x1, self->y1, self->r);
77};
78
79void moveTri(Shape community, int x, int y)
80{
81 I_am(self, Tri);
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;
89};
90
91void moveRect(Shape community, int x, int y)
92{
93 I_am(self, Rect);
94
95 self->x1 += x;
96 self->y1 += y;
97 self->x2 += x;
98 self->y2 += y;
99};
100
101void moveCirc(Shape community, int x, int y)
102{
103 I_am(self, Circ);
104
105 self->x1 += x;
106 self->y1 += y;
107};
108
109str descTri(Shape community)
110{
111 I_am(self, Tri);
112
113 return self->name;
114}
115
116str descRect(Shape community)
117{
118 I_am(self, Rect);
119
120 return self->desc;
121}
122
123str descCirc(Shape community)
124{
125 I_am(self, Circ);
126
127 return self->comment;
128}
129
130float calcAreaTri(Shape community)
131{
132 I_am(self, Tri);
133
134 return 0;
135}
136
137float calcAreaRect(Shape community)
138{
139 I_am(self, Rect);
140
141 return (self->x2 - self->x1)*(self->y2 - self->y1);
142}
143
144float calcAreaCirc(Shape community)
145{
146 I_am(self, Circ);
147
148 return PI * self->r * self->r;
149}
150
151void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3)
152{
153 I_am(self, Tri);
154
155 self->x1 = x1;
156 self->y1 = y1;
157 self->x2 = x2;
158 self->y2 = y2;
159 self->x3 = x3;
160 self->y3 = y3;
161}
162
163void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2)
164{
165 I_am(self, Rect);
166
167 self->x1 = x1;
168 self->y1 = y1;
169 self->x2 = x2;
170 self->y2 = y2;
171}
172
173void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3)
174{
175 I_am(self, Circ);
176
177 self->x1 = x1;
178 self->y1 = y1;
179 self->r = r;
180}
181
182void nameRect(Shape community, str name)
183{
184 I_am(self, Rect);
185
186 self->desc = name;
187}
188
189/* Example of using object interface methods from inside constructor methods */
190
191construct(Tri,Interface) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3)
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
200construct(Rect,Interface) (Rect self, int x1, int y1, int x2, int y2)
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
209construct(Circ,Interface) (Circ self, int x1, int y1, int r)
210{
211 self->comment = "Circle";
212 interface_of(self)->count++;
213 _(reset, self, x1, y1, r, 0, 0, 0);
214
215 return self;
216}
217
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
265/* usage of object community inside C<<1 program */
266
267program
268{
269 Interface triangles = triInterface();
270 Interface rectangles = rectInterface();
271 Interface circles = circInterface();
272 Object_list all = NULL, one;
273
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);
287 one->object = get_obj_as(Shape, Circ, circles, 0, 10, 1);
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)
294 {
295 _(draw, one->object);
296 printf("Area of this %s is %f (square pixels).\n", _(desc, one->object), _(area, one->object));
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 }
308 }
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 }
315}
This page took 0.134309 seconds and 4 git commands to generate.