3 /* This is example of advanced C<<1 object oriented features... */
8 def_mem(PointInterface
)
10 void method(move
) (Shape self
, int x
, int y
);
11 str
method(desc
) (Shape self
);
12 void method(rename
) (Shape self
, str name
);
16 def_mem(ShapeInterface
)
18 void method(draw
) (Shape self
);
19 void method(move
) (Shape self
, int x
, int y
);
20 str
method(desc
) (Shape self
);
21 void method(rename
) (Shape self
, str name
);
22 void method(reset
) (Shape self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
);
23 float method(area
) (Shape self
);
27 /* This is example of hierarchical object architecture in C<<1 */
31 interface(PointInterface
);
38 interface(ShapeInterface
);
45 interface(ShapeInterface
);
52 interface(ShapeInterface
);
57 def_community(Shape
, ShapeInterface
);
59 /* Note: interface(ShapeInterface); has to be at first position in all objects asociated in community */
67 /* implementation of methods */
69 void drawTri(Shape community
)
73 printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n",
74 self
->point
->desc
, self
->point
->x
, self
->point
->y
, self
->x2
, self
->y2
, self
->x3
, self
->y3
);
77 void drawRect(Shape community
)
81 printf("Drawing %s: %d,%d - %d,%d .\n",
82 self
->point
->desc
, self
->x1
, self
->y1
, self
->x2
, self
->y2
);
85 void drawCirc(Shape community
)
89 printf("Drawing %s: %d,%d - r=%d .\n",
90 self
->comment
, self
->x1
, self
->y1
, self
->r
);
93 void moveTri(Shape community
, int x
, int y
)
105 void moveRect(Shape community
, int x
, int y
)
115 void moveCirc(Shape community
, int x
, int y
)
123 str
descTri(Shape community
)
127 return _(desc
, self
->point
);
130 str
descRect(Shape community
)
134 return _(desc
, self
->point
);
137 str
descCirc(Shape community
)
141 return _(desc
, self
->point
);
144 float calcAreaTri(Shape community
)
151 float calcAreaRect(Shape community
)
155 return (self
->x2
- self
->x1
)*(self
->y2
- self
->y1
);
158 float calcAreaCirc(Shape community
)
162 return PI
* self
->r
* self
->r
;
165 void setTri(Shape community
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
177 void setRect(Shape community
, int x1
, int y1
, int x2
, int y2
, int dummy1
, int dummy2
)
187 void setCirc(Shape community
, int x1
, int y1
, int r
, int dummy1
, int dummy2
, int dummy3
)
196 void nameRect(Shape community
, str newname
)
200 _(name
, self
->point
, newname
);
203 /* Example of using object interface methods from inside constructor methods */
205 construct(Tri
,ShapeInterface
) (Tri self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
207 self
->name
= "TRIANGLE";
208 interface_of(self
)->count
++;
209 _(reset
, self
, x1
, y1
, x2
, y2
, x3
, y3
);
214 construct(Rect
,ShapeInterface
) (Rect self
, int x1
, int y1
, int x2
, int y2
)
216 self
->desc
= "rectangle";
217 interface_of(self
)->count
++;
218 _(reset
, self
, x1
, y1
, x2
, y2
, 0, 0);
223 construct(Circ
,ShapeInterface
) (Circ self
, int x1
, int y1
, int r
)
225 self
->comment
= "Circle";
226 interface_of(self
)->count
++;
227 _(reset
, self
, x1
, y1
, r
, 0, 0, 0);
232 /* registration of implemented methods to three interfaces of the same type */
234 PointInterface
pointInterface(void)
236 Get_mem(this, PointInterface
);
239 this->move
= movePoint
;
240 this->desc
= descPoint
;
242 this->reset
= setTri
;
243 this->area
= calcAreaTri
;
249 ShapeInterface
triInterface(void)
251 Get_mem(this, ShapeInterface
);
253 this->draw
= drawTri
;
254 this->move
= moveTri
;
255 this->desc
= descTri
;
257 this->reset
= setTri
;
258 this->area
= calcAreaTri
;
264 ShapeInterface
rectInterface(void)
266 Get_mem(this, ShapeInterface
);
268 this->draw
= drawRect
;
269 this->move
= moveRect
;
270 this->desc
= descRect
;
271 this->rename
= nameRect
;
272 this->reset
= setRect
;
273 this->area
= calcAreaRect
;
279 ShapeInterface
circInterface(void)
281 Get_mem(this, ShapeInterface
);
283 this->draw
= drawCirc
;
284 this->move
= moveCirc
;
285 this->desc
= descCirc
;
287 this->reset
= setCirc
;
288 this->area
= calcAreaCirc
;
294 /* usage of objects inside C<<1 program */
298 ShapeInterface triangles
= triInterface();
299 ShapeInterface rectangles
= rectInterface();
300 ShapeInterface circles
= circInterface();
301 Object_list all
= NULL
, one
;
303 one
= get_mem(Object_list
);
304 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 0, 10, 1, 11);
307 one
= get_mem(Object_list
);
308 one
->object
= get_obj_as(Shape
, Tri
, triangles
, 0, 0, 0, 4, 3, 0);
311 one
= get_mem(Object_list
);
312 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 10, 0, 11, 1);
315 one
= get_mem(Object_list
);
316 one
->object
= get_obj_as(Shape
, Circ
, circles
, 0, 10, 1);
319 printf("We have created %d triangles, %d rectangles and %d circles:\n",
320 triangles
->count
, rectangles
->count
, circles
->count
);
324 _(draw
, one
->object
);
325 printf("Area of this %s is %f (square pixels).\n", _(desc
, one
->object
), _(area
, one
->object
));
326 printf("We have created %d instances of this type of object.\n", interface_of(one
->object
)->count
);
328 if(interface_of(one
->object
) == rectangles
)
330 _(reset
, one
->object
, 0, 0, 1, 1, 0, 0);
331 _(rename
, one
->object
, "Renamed rectangle");
335 _(move
, one
->object
, 10, 10);
339 print("Rectangles were reset to new size and renamed, other objects were moved:");
342 _(draw
, one
->object
);