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