#3 objects-wiki.c compiles with -Wall and runs
[svn/Cll1h/.git] / demos / objects / objects-sample.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 int 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(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(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(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(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(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(Circ);
106
107 self->x1 += x;
108 self->y1 += y;
109 };
110
111 str descTri(Shape community)
112 {
113 return "";
114 }
115
116 str descRect(Shape community)
117 {
118 return "";
119 }
120
121 str descCirc(Shape community)
122 {
123 return "";
124 }
125
126 int calcAreaTri(Shape community)
127 {
128 return 0;
129 }
130
131 int calcAreaRect(Shape community)
132 {
133 return 0;
134 }
135
136 int calcAreaCirc(Shape community)
137 {
138 return 0;
139 }
140
141 void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3)
142 {
143 I_am(Tri);
144
145 self->x1 = x1;
146 self->y1 = y1;
147 self->x2 = x2;
148 self->y2 = y2;
149 self->x3 = x3;
150 self->y3 = y3;
151 }
152
153 void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2)
154 {
155 I_am(Rect);
156
157 self->x1 = x1;
158 self->y1 = y1;
159 self->x2 = x2;
160 self->y2 = y2;
161 }
162
163 void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3)
164 {
165 I_am(Circ);
166
167 self->x1 = x1;
168 self->y1 = y1;
169 self->r = r;
170 }
171
172 void nameRect(Shape community, str name)
173 {
174 I_am(Rect);
175
176 self->desc = name;
177 }
178
179 /* Example of using object interface methods from inside constructor methods */
180
181 construct(Tri) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3)
182 {
183 self->name = "TRIANGLE";
184 interface_of(self)->count++;
185 _(reset, self, x1, y1, x2, y2, x3, y3);
186
187 return self;
188 }
189
190 construct(Rect) (Rect self, int x1, int y1, int x2, int y2)
191 {
192 self->desc = "rectangle";
193 interface_of(self)->count++;
194 _(reset, self, x1, y1, x2, y2, 0, 0);
195
196 return self;
197 }
198
199 construct(Circ) (Circ self, int x1, int y1, int r)
200 {
201 self->comment = "Circle";
202 interface_of(self)->count++;
203 _(reset, self, x1, y1, r, 0, 0, 0);
204
205 return self;
206 }
207
208 /* registration of implemented methods to three interfaces of the same type */
209
210 Interface triInterface(void)
211 {
212 Get_mem(this, Interface);
213
214 this->draw = drawTri;
215 this->move = moveTri;
216 this->desc = descTri;
217 this->rename = NULL;
218 this->reset = setTri;
219 this->area = calcAreaTri;
220 this->count = 0;
221
222 return this;
223 }
224
225 Interface rectInterface(void)
226 {
227 Get_mem(this, Interface);
228
229 this->draw = drawRect;
230 this->move = moveRect;
231 this->desc = descRect;
232 this->rename = nameRect;
233 this->reset = setRect;
234 this->area = calcAreaRect;
235 this->count = 0;
236
237 return this;
238 }
239
240 Interface circInterface(void)
241 {
242 Get_mem(this, Interface);
243
244 this->draw = drawCirc;
245 this->move = moveCirc;
246 this->desc = descCirc;
247 this->rename = NULL;
248 this->reset = setCirc;
249 this->area = calcAreaCirc;
250 this->count = 0;
251
252 return this;
253 }
254
255 /* usage of objects inside C<<1 program */
256
257 program
258 {
259 Interface triangles = triInterface();
260 Interface rectangles = rectInterface();
261 Interface circles = circInterface();
262 Object_list all = NULL, one;
263
264 one = get_mem(Object_list);
265 one->object = get_obj_as(Shape, Rect, rectangles, 0, 10, 1, 11);
266 append(one, all);
267
268 one = get_mem(Object_list);
269 one->object = get_obj_as(Shape, Tri, triangles, 0, 0, 0, 4, 3, 0);
270 append(one, all);
271
272 one = get_mem(Object_list);
273 one->object = get_obj_as(Shape, Rect, rectangles, 10, 0, 11, 1);
274 append(one, all);
275
276 one = get_mem(Object_list);
277 one->object = get_obj_as(Shape, Circ, circles, 10, 0, 5);
278 append(one, all);
279
280 printf("We have created %d triangles, %d rectangles and %d circles:\n",
281 triangles->count, rectangles->count, circles->count);
282
283 for_each(one, all)
284 {
285 _(draw, one->object);
286 printf("Area of this %s is %d (square pixels).\n", _(desc, one->object), _(area, one->object));
287 printf("We have created %d instances of this type of object.\n", interface_of(one->object)->count);
288
289 if(interface_of(one->object) == rectangles)
290 {
291 _(reset, one->object, 0, 0, 1, 1, 0, 0);
292 _(rename, one->object, "Renamed rectangle");
293 }
294 else
295 {
296 _(move, one->object, 10, 10);
297 }
298 }
299
300 print("Rectangles were reset to new size and renamed, other objects were moved:");
301 for_each(one, all)
302 {
303 _(draw, one->object);
304 }
305 }
This page took 0.430216 seconds and 4 git commands to generate.