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