From: xchaos Date: Thu, 21 Aug 2008 17:53:48 +0000 (+0000) Subject: #3 some work on object programming demos X-Git-Url: https://git.harvie.cz/?p=svn%2FCll1h%2F.git;a=commitdiff_plain;h=02ca421af0e1463d1c1fb107daca667084f695cd #3 some work on object programming demos git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@83 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- diff --git a/demos/objects/objects-sample.c b/demos/objects/objects-community.c similarity index 100% rename from demos/objects/objects-sample.c rename to demos/objects/objects-community.c diff --git a/demos/objects/objects-hierarchy.c b/demos/objects/objects-hierarchy.c new file mode 100644 index 0000000..d81d271 --- /dev/null +++ b/demos/objects/objects-hierarchy.c @@ -0,0 +1,336 @@ +#include "cll1.h" + +/* This is example of advanced C<<1 object oriented features... */ + +def_type(Point); +def_type(Shape); + +def_mem(Interface) +{ + void method(draw) (Shape self); + void method(move) (Shape self, int x, int y); + str method(desc) (Shape self); + void method(rename) (Shape self, str name); + void method(reset) (Shape self, int x1, int y1, int x2, int y2, int x3, int y3); + float method(area) (Shape self); + int count; +}; + +/* This is example of hierarchical object architecture in C<<1 */ + +def_obj(Point) +{ + interface(Interface); + int x1, y1; + str desc; +} + +def_mem(Tri) +{ + interface(Interface); + Point point; + int x2, y2, x3, y3; +}; + +def_mem(Rect) +{ + interface(Interface); + Point point; + int x2, y2; +}; + +def_mem(Circ) +{ + interface(Interface); + Point point; + int r; +}; + +def_community(Shape, Interface); + +/* Note: interface(Interface); has to be at first position in all objects asociated in community */ + +def_mem(Object_list) +{ + Shape object; + list(Object_list); +}; + +/* implementation of methods */ + +void drawTri(Shape community) +{ + I_am(self, Tri); + + printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n", + self->point->desc, self->point->x, self->point->y, self->x2, self->y2, self->x3, self->y3); +}; + +void drawRect(Shape community) +{ + I_am(self, Rect); + + printf("Drawing %s: %d,%d - %d,%d .\n", + self->point->desc, self->x1, self->y1, self->x2, self->y2); +}; + +void drawCirc(Shape community) +{ + I_am(self, Circ); + + printf("Drawing %s: %d,%d - r=%d .\n", + self->comment, self->x1, self->y1, self->r); +}; + +void moveTri(Shape community, int x, int y) +{ + I_am(self, Tri); + + self->x1 += x; + self->y1 += y; + self->x2 += x; + self->y2 += y; + self->x3 += x; + self->y3 += y; +}; + +void moveRect(Shape community, int x, int y) +{ + I_am(self, Rect); + + self->x1 += x; + self->y1 += y; + self->x2 += x; + self->y2 += y; +}; + +void moveCirc(Shape community, int x, int y) +{ + I_am(self, Circ); + + self->x1 += x; + self->y1 += y; +}; + +str descTri(Shape community) +{ + I_am(self, Tri); + + return _(desc, self->point); +} + +str descRect(Shape community) +{ + I_am(self, Rect); + + return _(desc, self->point); +} + +str descCirc(Shape community) +{ + I_am(self, Circ); + + return _(desc, self->point); +} + +float calcAreaTri(Shape community) +{ + I_am(self, Tri); + + return 0; +} + +float calcAreaRect(Shape community) +{ + I_am(self, Rect); + + return (self->x2 - self->x1)*(self->y2 - self->y1); +} + +float calcAreaCirc(Shape community) +{ + I_am(self, Circ); + + return PI * self->r * self->r; +} + +void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3) +{ + I_am(self, Tri); + + self->x1 = x1; + self->y1 = y1; + self->x2 = x2; + self->y2 = y2; + self->x3 = x3; + self->y3 = y3; +} + +void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2) +{ + I_am(self, Rect); + + self->x1 = x1; + self->y1 = y1; + self->x2 = x2; + self->y2 = y2; +} + +void setCirc(Shape community, int x1, int y1, int r, int dummy1, int dummy2, int dummy3) +{ + I_am(self, Circ); + + self->x1 = x1; + self->y1 = y1; + self->r = r; +} + +void nameRect(Shape community, str newname) +{ + I_am(self, Rect); + + _(name, self->point, newname); +} + +/* Example of using object interface methods from inside constructor methods */ + +construct(Tri,Interface) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3) +{ + self->name = "TRIANGLE"; + interface_of(self)->count++; + _(reset, self, x1, y1, x2, y2, x3, y3); + + return self; +} + +construct(Rect,Interface) (Rect self, int x1, int y1, int x2, int y2) +{ + self->desc = "rectangle"; + interface_of(self)->count++; + _(reset, self, x1, y1, x2, y2, 0, 0); + + return self; +} + +construct(Circ,Interface) (Circ self, int x1, int y1, int r) +{ + self->comment = "Circle"; + interface_of(self)->count++; + _(reset, self, x1, y1, r, 0, 0, 0); + + return self; +} + +/* registration of implemented methods to three interfaces of the same type */ + +Interface pointInterface(void) +{ + Get_mem(this, Interface); + + this->draw = NULL; + this->move = movePoint; + this->desc = descPoint; + this->rename = NULL; + this->reset = setTri; + this->area = calcAreaTri; + this->count = 0; + + return this; +} + +Interface triInterface(void) +{ + Get_mem(this, Interface); + + this->draw = drawTri; + this->move = moveTri; + this->desc = descTri; + this->rename = NULL; + this->reset = setTri; + this->area = calcAreaTri; + this->count = 0; + + return this; +} + +Interface rectInterface(void) +{ + Get_mem(this, Interface); + + this->draw = drawRect; + this->move = moveRect; + this->desc = descRect; + this->rename = nameRect; + this->reset = setRect; + this->area = calcAreaRect; + this->count = 0; + + return this; +} + +Interface circInterface(void) +{ + Get_mem(this, Interface); + + this->draw = drawCirc; + this->move = moveCirc; + this->desc = descCirc; + this->rename = NULL; + this->reset = setCirc; + this->area = calcAreaCirc; + this->count = 0; + + return this; +} + +/* usage of objects inside C<<1 program */ + +program +{ + Interface triangles = triInterface(); + Interface rectangles = rectInterface(); + Interface circles = circInterface(); + Object_list all = NULL, one; + + one = get_mem(Object_list); + one->object = get_obj_as(Shape, Rect, rectangles, 0, 10, 1, 11); + append(one, all); + + one = get_mem(Object_list); + one->object = get_obj_as(Shape, Tri, triangles, 0, 0, 0, 4, 3, 0); + append(one, all); + + one = get_mem(Object_list); + one->object = get_obj_as(Shape, Rect, rectangles, 10, 0, 11, 1); + append(one, all); + + one = get_mem(Object_list); + one->object = get_obj_as(Shape, Circ, circles, 0, 10, 1); + append(one, all); + + printf("We have created %d triangles, %d rectangles and %d circles:\n", + triangles->count, rectangles->count, circles->count); + + for_each(one, all) + { + _(draw, one->object); + printf("Area of this %s is %f (square pixels).\n", _(desc, one->object), _(area, one->object)); + printf("We have created %d instances of this type of object.\n", interface_of(one->object)->count); + + if(interface_of(one->object) == rectangles) + { + _(reset, one->object, 0, 0, 1, 1, 0, 0); + _(rename, one->object, "Renamed rectangle"); + } + else + { + _(move, one->object, 10, 10); + } + } + + print("Rectangles were reset to new size and renamed, other objects were moved:"); + for_each(one, all) + { + _(draw, one->object); + } +}