From 78d96788ba38feed801433a843b0e7a01f6cc5da Mon Sep 17 00:00:00 2001 From: xchaos Date: Thu, 10 Jan 2008 13:38:31 +0000 Subject: [PATCH] preliminary work on object oriented tools (no useful macros yet) git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@8 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- objects/cobjects.c | 107 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 objects/cobjects.c diff --git a/objects/cobjects.c b/objects/cobjects.c new file mode 100644 index 0000000..15f55d6 --- /dev/null +++ b/objects/cobjects.c @@ -0,0 +1,107 @@ +#include +#include + +#define CONFEDERATION(IFACE,TYPE) struct IFACE *interface;struct TYPE *next +#define CREATE(TYPE) (struct TYPE *)malloc(sizeof(struct TYPE)) +#define APPEND(TYPE,MEMBER,LIST) (MEMBER?MEMBER->next=(struct TYPE *)LIST:0,LIST=(struct TYPE *)MEMBER) + +struct Drawable_data +{ + CONFEDERATION(Drawable_interface,Drawable_data); +}; + +struct Drawable_interface +{ + void (*draw)(struct Drawable_data *); +}; + +struct Tri +{ + CONFEDERATION(Drawable_interface,Drawable_data); + int x1; int y1; int x2; int y2; int x3; int y3; +}; + +struct Rect +{ + CONFEDERATION(Drawable_interface,Drawable_data); + int x1; int y1; int x2; int y2; +}; + +void draw_tri(struct Drawable_data *v) +{ + struct Tri *t=(struct Tri *)v; + printf("%d,%d - %d,%d - %d,%d\n",t->x1,t->y1,t->x2,t->y2,t->x3,t->y3); +}; + +void draw_rect(struct Drawable_data *v) +{ + struct Rect *r=(struct Rect *)v; + printf("%d,%d - %d,%d\n",r->x1,r->y1,r->x2,r->y2); +}; + +struct Drawable_interface *init_tri_interface(void) +{ + struct Drawable_interface *i=CREATE(Drawable_interface); + /* vsechny metody je bohuzel potreba bindnout rucne, neda se svitit... */ + if(i) i->draw=draw_tri; + return i; +} + +struct Drawable_interface *init_rect_interface(void) +{ + struct Drawable_interface *i=CREATE(Drawable_interface); + /* vsechny metody je bohuzel potreba bindnout rucne, neda se svitit... */ + if(i) i->draw=draw_rect; + return i; +} + +struct Tri *create_tri(struct Drawable_interface *i, int x1, int y1, int x2, int y2, int x3, int y3) +{ + struct Tri *t=CREATE(Tri); + if(i && t) + { + t->interface=i; + t->x1=x1;t->y1=y1;t->x2=x2;t->y2=y2;t->x3=x3;t->y3=y3; + } + return t; +} + +struct Rect *create_rect(struct Drawable_interface *i, int x1, int y1, int x2, int y2) +{ + struct Rect *r=CREATE(Rect); + if(i && r) + { + r->interface=i; + r->x1=x1;r->y1=y1;r->x2=x2;r->y2=y2; + } + return r; +} + +int main(void) +{ + struct Drawable_interface *tri_interface=init_tri_interface(); + struct Drawable_interface *rect_interface=init_rect_interface(); + struct Drawable_data *all=NULL,*one; + struct Tri *t; + struct Rect *r; + + r=create_rect(rect_interface,0,10,1,11); + APPEND(Drawable_data,r,all); + t=create_tri(tri_interface,0,0,0,4,3,0); + APPEND(Drawable_data,t,all); + r=create_rect(rect_interface,10,0,11,1); + APPEND(Drawable_data,r,all); + + /* a nyni ... bylo to hodne prace, ale povedlo se ... ! */ + + for(one=all;one;one=one->next) (*(one->interface->draw))(one); + + /* a nyni pro efekt jeste instatni nudlovy kod s kanci prichuti: */ + +#define _(OBJECT,METHOD) OBJECT==NULL)?0:(*(OBJECT->interface->METHOD))(OBJECT +#define FOR_EACH(ONE,ALL) for(ONE=ALL;ONE;ONE=ONE->next) + + FOR_EACH(one,all) (_(one,draw)); + + return 0; +} -- 2.30.2