some work
[svn/Cll1h/.git] / demos / objects / objects-confederation.c
diff --git a/demos/objects/objects-confederation.c b/demos/objects/objects-confederation.c
new file mode 100644 (file)
index 0000000..61b4798
--- /dev/null
@@ -0,0 +1,342 @@
+#include "cll1.h"
+
+/* This is example of advanced C<<1 object oriented features... */ 
+
+def_interface(Point, PointInterface)
+{
+ void method(move) (Shape self, int x, int y);
+ str method(desc) (Shape self);
+ void method(rename) (Shape self, str name);
+ int count;
+};
+
+def_interface(Shape, ShapeInterface)
+{
+ federation(Point);
+ void method(draw) (Shape self);
+ 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 non-hierarchical object federation architecture in C<<1 */
+
+def_obj(Point)
+{
+ interface(PointInterface);
+ shape Shape; /* this is optional, but we will use it do demonstrate power of C<<1 */
+ int x1, y1;
+ str desc;
+}
+
+def_mem(Tri)
+{
+ interface(ShapeInterface);
+ Point point;
+ int x2, y2, x3, y3;
+};
+
+def_mem(Rect)
+{
+ interface(ShapeInterface);
+ Point point;
+ int x2, y2;
+};
+
+def_mem(Circ)
+{
+ interface(ShapeInterface);
+ Point point;
+ int r;
+};
+
+def_community(Shape, ShapeInterface); 
+
+/* Note: interface(ShapeInterface); 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,ShapeInterface) (Tri self, int x1, int y1, int x2, int y2, int x3, int y3)
+{
+ self->point = confederate_obj(Point);
+ _(move, self->point, x1, x2);
+ self->name = "TRIANGLE";
+ interface_of(self)->count++;
+ _(reset, self, x2, y2, x3, y3);
+
+ return self;
+}
+
+construct(Rect,ShapeInterface) (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,ShapeInterface) (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 */
+
+PointInterface pointInterface(void)
+{
+ Get_mem(this, PointInterface);
+
+ this->draw = NULL;
+ this->move = movePoint;
+ this->desc = descPoint;
+ this->rename = NULL;
+ this->reset = setTri;
+ this->area = calcAreaTri;
+ this->count = 0;
+
+ return this;
+}
+
+ShapeInterface triInterface(void)
+{
+ Get_mem(this, ShapeInterface);
+
+ this->draw = drawTri;
+ this->move = moveTri;
+ this->desc = descTri;
+ this->rename = NULL;
+ this->reset = setTri;
+ this->area = calcAreaTri;
+ this->count = 0;
+
+ return this;
+}
+
+ShapeInterface rectInterface(void)
+{
+ Get_mem(this, ShapeInterface);
+
+ this->draw = drawRect;
+ this->move = moveRect;
+ this->desc = descRect;
+ this->rename = nameRect;
+ this->reset = setRect;
+ this->area = calcAreaRect;
+ this->count = 0;
+
+ return this;
+}
+
+ShapeInterface circInterface(void)
+{
+ Get_mem(this, ShapeInterface);
+
+ 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
+{
+ ShapeInterface triangles = triInterface();
+ ShapeInterface rectangles = rectInterface();
+ ShapeInterface 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.205581 seconds and 4 git commands to generate.