3 /* This is example of advanced C<<1 object oriented features... */
5 def_interface(Shape
, Interface
)
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
);
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. */
23 int x1
, y1
, x2
, y2
, x3
, y3
;
42 def_community(Shape
, Interface
);
44 /* Note: interface(Interface); has to be at first position in all objects asociated in community */
53 /* implementation of methods */
55 void drawTri(Shape community
)
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
);
63 void drawRect(Shape community
)
67 printf("Drawing %s: %d,%d - %d,%d .\n",
68 self
->desc
, self
->x1
, self
->y1
, self
->x2
, self
->y2
);
71 void drawCirc(Shape community
)
75 printf("Drawing %s: %d,%d - r=%d .\n",
76 self
->comment
, self
->x1
, self
->y1
, self
->r
);
79 void moveTri(Shape community
, int x
, int y
)
91 void moveRect(Shape community
, int x
, int y
)
101 void moveCirc(Shape community
, int x
, int y
)
109 str
descTri(Shape community
)
116 str
descRect(Shape community
)
123 str
descCirc(Shape community
)
127 return self
->comment
;
130 float calcAreaTri(Shape community
)
137 float calcAreaRect(Shape community
)
141 return (self
->x2
- self
->x1
)*(self
->y2
- self
->y1
);
144 float calcAreaCirc(Shape community
)
148 return PI
* self
->r
* self
->r
;
151 void setTri(Shape community
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
163 void setRect(Shape community
, int x1
, int y1
, int x2
, int y2
, int dummy1
, int dummy2
)
173 void setCirc(Shape community
, int x1
, int y1
, int r
, int dummy1
, int dummy2
, int dummy3
)
182 void nameRect(Shape community
, str name
)
189 /* Example of using object interface methods from inside constructor methods */
191 construct(Tri
,Interface
) (Tri self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
193 self
->name
= "TRIANGLE";
194 interface_of(self
)->count
++;
195 _(reset
, self
, x1
, y1
, x2
, y2
, x3
, y3
);
200 construct(Rect
,Interface
) (Rect self
, int x1
, int y1
, int x2
, int y2
)
202 self
->desc
= "rectangle";
203 interface_of(self
)->count
++;
204 _(reset
, self
, x1
, y1
, x2
, y2
, 0, 0);
209 construct(Circ
,Interface
) (Circ self
, int x1
, int y1
, int r
)
211 self
->comment
= "Circle";
212 interface_of(self
)->count
++;
213 _(reset
, self
, x1
, y1
, r
, 0, 0, 0);
218 /* registration of implemented methods to three interfaces of the same type */
220 Interface
triInterface(void)
222 Get_mem(this, Interface
);
224 this->draw
= drawTri
;
225 this->move
= moveTri
;
226 this->desc
= descTri
;
228 this->reset
= setTri
;
229 this->area
= calcAreaTri
;
235 Interface
rectInterface(void)
237 Get_mem(this, Interface
);
239 this->draw
= drawRect
;
240 this->move
= moveRect
;
241 this->desc
= descRect
;
242 this->rename
= nameRect
;
243 this->reset
= setRect
;
244 this->area
= calcAreaRect
;
250 Interface
circInterface(void)
252 Get_mem(this, Interface
);
254 this->draw
= drawCirc
;
255 this->move
= moveCirc
;
256 this->desc
= descCirc
;
258 this->reset
= setCirc
;
259 this->area
= calcAreaCirc
;
265 /* usage of object community inside C<<1 program */
269 Interface triangles
= triInterface();
270 Interface rectangles
= rectInterface();
271 Interface circles
= circInterface();
272 Object_list all
= NULL
, one
;
274 one
= get_mem(Object_list
);
275 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 0, 10, 1, 11);
278 one
= get_mem(Object_list
);
279 one
->object
= get_obj_as(Shape
, Tri
, triangles
, 0, 0, 0, 4, 3, 0);
282 one
= get_mem(Object_list
);
283 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 10, 0, 11, 1);
286 one
= get_mem(Object_list
);
287 one
->object
= get_obj_as(Shape
, Circ
, circles
, 0, 10, 1);
290 printf("We have created %d triangles, %d rectangles and %d circles:\n",
291 triangles
->count
, rectangles
->count
, circles
->count
);
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
);
299 if(interface_of(one
->object
) == rectangles
)
301 _(reset
, one
->object
, 0, 0, 1, 1, 0, 0);
302 _(rename
, one
->object
, "Renamed rectangle");
306 _(move
, one
->object
, 10, 10);
310 print("Rectangles were reset to new size and renamed, other objects were moved:");
313 _(draw
, one
->object
);
This page took 0.405192 seconds and 4 git commands to generate.