3 /* This is example of advanced C<<1 object oriented features... */
5 def_interface(Point
, PointInterface
)
7 void method(move
) (Shape self
, int x
, int y
);
8 str
method(desc
) (Shape self
);
9 void method(rename
) (Shape self
, str name
);
13 def_interface(Shape
, ShapeInterface
)
16 void method(draw
) (Shape self
);
17 void method(reset
) (Shape self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
);
18 float method(area
) (Shape self
);
22 /* This is example of non-hierarchical object federation architecture in C<<1 */
26 interface(PointInterface
);
27 shape Shape
; /* this is optional, but we will use it do demonstrate power of C<<1 */
34 interface(ShapeInterface
);
41 interface(ShapeInterface
);
48 interface(ShapeInterface
);
53 def_community(Shape
, ShapeInterface
);
55 /* Note: interface(ShapeInterface); has to be at first position in all objects asociated in community */
63 /* implementation of methods */
65 void drawTri(Shape community
)
69 printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n",
70 self
->point
->desc
, self
->point
->x
, self
->point
->y
, self
->x2
, self
->y2
, self
->x3
, self
->y3
);
73 void drawRect(Shape community
)
77 printf("Drawing %s: %d,%d - %d,%d .\n",
78 self
->point
->desc
, self
->x1
, self
->y1
, self
->x2
, self
->y2
);
81 void drawCirc(Shape community
)
85 printf("Drawing %s: %d,%d - r=%d .\n",
86 self
->comment
, self
->x1
, self
->y1
, self
->r
);
89 void moveTri(Shape community
, int x
, int y
)
101 void moveRect(Shape community
, int x
, int y
)
111 void moveCirc(Shape community
, int x
, int y
)
119 str
descTri(Shape community
)
123 return _(desc
, self
->point
);
126 str
descRect(Shape community
)
130 return _(desc
, self
->point
);
133 str
descCirc(Shape community
)
137 return _(desc
, self
->point
);
140 float calcAreaTri(Shape community
)
147 float calcAreaRect(Shape community
)
151 return (self
->x2
- self
->x1
)*(self
->y2
- self
->y1
);
154 float calcAreaCirc(Shape community
)
158 return PI
* self
->r
* self
->r
;
161 void setTri(Shape community
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
173 void setRect(Shape community
, int x1
, int y1
, int x2
, int y2
, int dummy1
, int dummy2
)
183 void setCirc(Shape community
, int x1
, int y1
, int r
, int dummy1
, int dummy2
, int dummy3
)
192 void nameRect(Shape community
, str newname
)
196 _(name
, self
->point
, newname
);
199 /* Example of using object interface methods from inside constructor methods */
201 construct(Tri
,ShapeInterface
) (Tri self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
203 self
->point
= get_federation(Point
);
204 _(move
, self
->point
, x1
, x2
);
205 self
->name
= "TRIANGLE";
206 interface_of(self
)->count
++;
207 _(reset
, self
, x2
, y2
, x3
, y3
);
212 construct(Rect
,ShapeInterface
) (Rect self
, int x1
, int y1
, int x2
, int y2
)
214 self
->desc
= "rectangle";
215 interface_of(self
)->count
++;
216 _(reset
, self
, x1
, y1
, x2
, y2
, 0, 0);
221 construct(Circ
,ShapeInterface
) (Circ self
, int x1
, int y1
, int r
)
223 self
->comment
= "Circle";
224 interface_of(self
)->count
++;
225 _(reset
, self
, x1
, y1
, r
, 0, 0, 0);
230 /* registration of implemented methods to three interfaces of the same type */
232 PointInterface
pointInterface(void)
234 Get_mem(this, PointInterface
);
237 this->move
= movePoint
;
238 this->desc
= descPoint
;
240 this->reset
= setTri
;
241 this->area
= calcAreaTri
;
247 ShapeInterface
triInterface(void)
249 Get_mem(this, ShapeInterface
);
251 this->draw
= drawTri
;
252 this->move
= moveTri
;
253 this->desc
= descTri
;
255 this->reset
= setTri
;
256 this->area
= calcAreaTri
;
262 ShapeInterface
rectInterface(void)
264 Get_mem(this, ShapeInterface
);
266 this->draw
= drawRect
;
267 this->move
= moveRect
;
268 this->desc
= descRect
;
269 this->rename
= nameRect
;
270 this->reset
= setRect
;
271 this->area
= calcAreaRect
;
277 ShapeInterface
circInterface(void)
279 Get_mem(this, ShapeInterface
);
281 this->draw
= drawCirc
;
282 this->move
= moveCirc
;
283 this->desc
= descCirc
;
285 this->reset
= setCirc
;
286 this->area
= calcAreaCirc
;
292 /* usage of objects inside C<<1 program */
296 ShapeInterface triangles
= triInterface();
297 ShapeInterface rectangles
= rectInterface();
298 ShapeInterface circles
= circInterface();
299 Object_list all
= NULL
, one
;
301 one
= get_mem(Object_list
);
302 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 0, 10, 1, 11);
305 one
= get_mem(Object_list
);
306 one
->object
= get_obj_as(Shape
, Tri
, triangles
, 0, 0, 0, 4, 3, 0);
309 one
= get_mem(Object_list
);
310 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 10, 0, 11, 1);
313 one
= get_mem(Object_list
);
314 one
->object
= get_obj_as(Shape
, Circ
, circles
, 0, 10, 1);
317 printf("We have created %d triangles, %d rectangles and %d circles:\n",
318 triangles
->count
, rectangles
->count
, circles
->count
);
322 _(draw
, one
->object
);
323 printf("Area of this %s is %f (square pixels).\n", _(desc
, one
->object
), _(area
, one
->object
));
324 printf("We have created %d instances of this type of object.\n", interface_of(one
->object
)->count
);
326 if(interface_of(one
->object
) == rectangles
)
328 _(reset
, one
->object
, 0, 0, 1, 1, 0, 0);
329 _(rename
, one
->object
, "Renamed rectangle");
333 _(move
, one
->object
, 10, 10);
337 print("Rectangles were reset to new size and renamed, other objects were moved:");
340 _(draw
, one
->object
);