From 097bac9a8882d0e781ca25e53b846981f4f9e05c Mon Sep 17 00:00:00 2001 From: xchaos Date: Sun, 13 Jan 2008 22:03:33 +0000 Subject: [PATCH] some prelimanary work - will not compile git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@15 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 26 +++++++-- demos/objects/objects-ansi.c | 107 +++++++++++++++++++++++++++++++++++ demos/objects/objects-wiki.c | 58 +++++++++++++++++++ 3 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 demos/objects/objects-ansi.c create mode 100644 demos/objects/objects-wiki.c diff --git a/cll1.h b/cll1.h index 7fe6026..261cd81 100644 --- a/cll1.h +++ b/cll1.h @@ -18,9 +18,10 @@ along with C<<1 header file; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - ------------------------------------------------------------------- */ -#ifndef __CLL1H__ -#define __CLL1H__ +*/ + +#ifndef _CLL1_H +#define _CLL1_H #include #include @@ -193,11 +194,26 @@ #define textfile(TYPE,LINE) struct TYPE { char *LINE; char _eoln; list(TYPE); } #define date(S) { time_t _T; _T=time(NULL); duplicate(ctime(&_T),S); } -#ifdef USE_LIBRARY_MYSQL /* ------------------------------------------------------------------- */ /* MySQL Infrastructure and basic macros - updated 2007-12-19 by xCh. */ /* ------------------------------------------------------------------- */ +#ifdef USE_LIBRARY_MYSQL #define mysql_connect(S,U,P,D) (create(_Matom,_MStack) && push(_Matom,_Mstack) && (_Matom->M=mysql_init(NULL)),(_Matom->M && mysql_real_connect(_Matom->M,S,U,P,D,0,NULL,0)?_Matom->M:NULL)) #endif - +/* ------------------------------------------------------------------- */ +/* Object infrastructure and basic macros - updated 2008-01-13 by xCh. */ +/* unfinished */ +#define uses_interface(TYPE) struct TYPE *__class +#define method(NAME) +#define interface_implementation(IFACE,TYPE) struct TYPE *__get_##_##IFACE(void) { struct TYPE *_FACE=get_mem(TYPE); __bind_##_##IFACE(_FACE); return _IFC; } void __bind_##_##IFACE(struct TYPE *_FACE) +#define bind_method(METHOD,FUNCTION) _IFACE->METHOD=FUNCTION +#define get_interface(IFACE) __get_##_##IFACE(void) +// must be function #define get_object(TYPE) (_CLL1_MALLOC(TYPE),CLL1_ASSERT(),) +#define _(OBJECT,FUNCTION) OBJECT==NULL?NULL::(*(OBJECT->__class->METHOD))(OBJECT #endif +/* ------------------------------------------------------------------- */ +/* some general ideas, unfinished */ +#define _CLL1_THROW(ERR) +#define _CLL1_TRY _NEW_E for(;;) if () {.... if(_Eatom->SYS) { if(_Eatom->RET) return; else break;} else { } +#define _CLL1_ASSERT(EXPR) assert(EXPR) +#define _CLL1_MALLOC(TYPE) malloc(sizeof(TYPE)) diff --git a/demos/objects/objects-ansi.c b/demos/objects/objects-ansi.c new file mode 100644 index 0000000..d75befb --- /dev/null +++ b/demos/objects/objects-ansi.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)(void *self); +}; + +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(void *_self) +{ + struct Tri *self=(struct Tri *)_self; + printf("%d,%d - %d,%d - %d,%d\n",self->x1,self->y1,self->x2,self->y2,self->x3,self->y3); +}; + +void draw_rect(void *self) +{ + struct Rect *r=(struct Rect *)self; + 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; +} diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c new file mode 100644 index 0000000..7d57c70 --- /dev/null +++ b/demos/objects/objects-wiki.c @@ -0,0 +1,58 @@ +#include + +/* This is C<<1 rewrite of object polymorphism from + http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */ + +struct Animal +{ + use_interface(AnimalActions); + char *name; +}; + +struct AnimalActions +{ + const char *(method(talk)); +} + +struct *Animal get_animal(char *name, struct AnimalActions *interface) +{ + struct *Animal animal=get_object(Animal,interface); + animal->name=name; + return animal; +} + +const char *cat_talk(void *_self) +{ + return "Meow!"; +} + +const char *dog_talk(void *_self) +{ + return "Arf! Arf!"; +} + +interface_implementation(CatActions,AnimalActions) +{ + bind_method(talk,cat_talk); +} + +interface_implementation(DogActions,AnimalActions) +{ + bind_method(talk,dog_talk); +} + +program +{ + int i; + struct Animals *animal[3]; + struct AnimalActions *cat = get_interface(CatActions); + struct AnimalActions *dog = get_interface(DogActions); + + animal[0]=get_animal("Missy",cat); + animal[0]=get_animal("Mr. Bojangles",cat); + animal[0]=get_animal("Lassie",dog); + + for_range(i,0,2) + printf("%s: %s\n",animal[i].name,(_(animal[i],talk))); + +} -- 2.30.2