X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=demos%2Fobjects%2Fobjects-sample.c;h=e0e8914280ae82eb4efa715b6190f455adcbf06f;hb=96ec74e5789a747913ebe5b1a583e540e60fa94a;hp=cee195e046a042b2b417c99597d21178e098b2c4;hpb=47e4883a12f36914d419e9e5fd1a0ff97227c093;p=svn%2FCll1h%2F.git diff --git a/demos/objects/objects-sample.c b/demos/objects/objects-sample.c index cee195e..e0e8914 100644 --- a/demos/objects/objects-sample.c +++ b/demos/objects/objects-sample.c @@ -1,106 +1,156 @@ -#include +#include "cll1.h" -def_community(Drawable_object,Actions); +/* This is example of advanced C<<1 object oriented features... */ -def_mem(Actions) +def_type(Shape); + +def_mem(Interface) { - void method(init) (Drawable_object self, ...); - void method(draw) (Drawable_object self); - int method(desc) (Drawable_object self,int something); + 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); + int method(area) (Shape self); + int count; }; +/* We intentionaly define heterogenous objects, which share only the interface + binding. Not only we can use different order of variables, and different + names - but we can even store these data on remote systems, etc. */ + def_mem(Tri) { - interface(Actions); + interface(Interface); int x1, y1, x2, y2, x3, y3; + str name; }; def_mem(Rect) { - interface(Actions); + interface(Interface); + str desc; int x1, y1, x2, y2; }; -construct_obj(Tri,Actions); +def_mem(Circ) +{ + interface(Interface); + int x1, y1; + str comment; + int r; +}; -construct_obj(Rect,Actions); +def_community(Shape, Interface); +/* Note: interface(Interface); has to be at first position in all objects asociated in community */ def_mem(Object_list) { - Drawable_object object; + Shape object; list(Object_list); }; -Actions init_tri_interface(Actions this) -{ - this->init = init_tri; - this->draw = draw_tri; - this->desc = desc_tri; - - return this; -} - -Actions init_rect_interface(Actions this) -{ - this->init = init_tri; - this->draw = draw_rect; - this->desc = desc_rect; - return this; -} +/* implementation of methods */ -void draw_tri(Drawable_object community) +void drawTri(Shape community) { I_am(Tri); - printf("imagine I am drawing %d,%d - %d,%d - %d,%d\n", - self->x1,self->y1,self->x2,self->y2,self->x3,self->y3); + printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n", + self->name, self->x1, self->y1, self->x2, self->y2, self->x3, self->y3); }; -void draw_rect(Drawable_object community) +void drawRect(Shape community) { I_am(Rect); - printf("imagine I am drawing %d,%d - %d,%d\n", - self->x1,self->y1,self->x2,self->y2); + printf("Drawing %s: %d,%d - %d,%d .\n", + self->desc, self->x1, self->y1, self->x2, self->y2); }; -int desc_tri(Drawable_object community,int something) +void drawCirc(Shape community) { - I_am(Tri); + I_am(Circ); - printf("I am triangle %d,%d - %d,%d - %d,%d, method draw called with argument: %d\n", - self->x1,self->y1,self->x2,self->y2,self->x3,self->y3,something); + printf("Drawing %s: %d,%d - r=%d .\n", + self->comment, self->x1, self->y1, self->r); +}; - return something; +void moveTri(Shape community, int x, int y) +{ + I_am(Tri); + + self->x1 += x; + self->y1 += y; + self->x2 += x; + self->y2 += y; + self->x3 += x; + self->y3 += y; }; -int desc_rect(Drawable_object community,int something) +void moveRect(Shape community, int x, int y) { I_am(Rect); - printf("I am rectangle %d,%d - %d,%d, method draw called with argument: %d\n", - self->x1,self->y1,self->x2,self->y2,something); + self->x1 += x; + self->y1 += y; + self->x2 += x; + self->y2 += y; +}; - return something; +void moveCirc(Shape community, int x, int y) +{ + I_am(Circ); + + self->x1 += x; + self->y1 += y; }; -Tri init_tri(Drawable_object self, int x1, int y1, int x2, int y2, int x3, int y3) +str descTri(Shape community) +{ + return ""; +} + +str descRect(Shape community) +{ + return ""; +} + +str descCirc(Shape community) +{ + return ""; +} + +int calcAreaTri(Shape community) +{ + return 0; +} + +int calcAreaRect(Shape community) +{ + return 0; +} + +int calcAreaCirc(Shape community) +{ + return 0; +} + +void setTri(Shape community, int x1, int y1, int x2, int y2, int x3, int y3) { I_am(Tri); - + self->x1 = x1; self->y1 = y1; self->x2 = x2; self->y2 = y2; self->x3 = x3; self->y3 = y3; - - return self; } -Rect init_rect(Drawable_object self, int x1, int y1, int x2, int y2) +void setRect(Shape community, int x1, int y1, int x2, int y2, int dummy1, int dummy2) { I_am(Rect); @@ -108,26 +158,148 @@ Rect init_rect(Drawable_object self, int x1, int y1, int x2, int y2) 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(Circ); + + self->x1 = x1; + self->y1 = y1; + self->r = r; +} + +void nameRect(Shape community, str name) +{ + I_am(Rect); + + self->desc = name; +} + +/* Example of using object interface methods from inside constructor methods */ + +construct(Tri) (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) (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) (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 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 { - Init_mem(tri_interface,Actions,init_tri_interface); - Init_mem(rect_interface,Actions,init_rect_interface); - Object_list all = NULL, one = get_mem(Object_list); + Interface triangles = triInterface(); + Interface rectangles = rectInterface(); + Interface circles = circInterface(); + Object_list all = NULL, one; - one->object = init_obj_as(Drawable_data, Rect, rect_interface, 0, 10, 1, 11); - append(one,all); - one->object = init_obj_as(Drawable_data, Tri, tri_interface, 0, 0, 0, 4, 3, 0); - append(one,all); - one->object = init_obj_as(Drawable_data, Rect, rect_interface, 10, 0, 11, 1); - append(one,all); - - for_each(one,all) + 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, 10, 0, 5); + 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) { - printf("(return value %d)\n", _(one->object,desc)); - _(one->object,draw); + _(draw, one->object); + printf("Area of this %s is %d (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); + } }