preparing 0.9 version, rename to crl1.h
[svn/Cll1h/.git] / demos / objects / objects-community.c
diff --git a/demos/objects/objects-community.c b/demos/objects/objects-community.c
deleted file mode 100644 (file)
index 0854766..0000000
+++ /dev/null
@@ -1,315 +0,0 @@
-#include "cll1.h"
-
-/* This is example of advanced C<<1 object oriented features... */ 
-
-def_interface(Shape, 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;
-};
-
-/* 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(Interface);
- int x1, y1, x2, y2, x3, y3;
- str name;
-};
-
-def_mem(Rect)
-{
- interface(Interface);
- str desc;
- int x1, y1, x2, y2;
-};
-
-def_mem(Circ)
-{
- interface(Interface);
- int x1, y1;
- str comment;
- 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->name, self->x1, self->y1, 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->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 self->name;
-}
-
-str descRect(Shape community)
-{
- I_am(self, Rect);
-
- return self->desc;
-}
-
-str descCirc(Shape community)
-{
- I_am(self, Circ);
- return self->comment;
-}
-
-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 name)
-{
- I_am(self, Rect);
- self->desc = name;
-}
-
-/* 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 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 object community 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);
- } 
-}
This page took 0.166419 seconds and 4 git commands to generate.