X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=demos%2Fobjects%2Fobjects-wiki.c;h=8b6251af9462fe808ff53cf493eee8a6a08b4158;hb=9abd9073f7853f2a4cd5f5243152d71ff5e47e11;hp=127f959d096b9e74cc32483389f5767cb4af539a;hpb=1a8d78d9e9f1a4a2e8b7c3b740c5516195fbc04b;p=svn%2FCll1h%2F.git diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c index 127f959..8b6251a 100644 --- a/demos/objects/objects-wiki.c +++ b/demos/objects/objects-wiki.c @@ -1,13 +1,15 @@ #include "cll1.h" /* This is C<<1 rewrite of object polymorphism from - http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */ + http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming + + Of course there are easier ways to do the same thing... :-) + (G)2008 xChas */ def_type(Animal); def_mem(Actions) { - void method(init) (Animal self, str name); str method(talk) (Animal self); }; @@ -17,42 +19,50 @@ def_obj(Animal) char *name; }; -Animal construct(Animal) (Animal self, str name) +construct(Animal,Actions) (Animal self, str name) { self->name = name; return self; } -str cat_talk(Animal self) +/* implementation of methods */ + +str catTalk(Animal self) { return "Meow!"; } -str dog_talk(Animal self) +str dogTalk(Animal self) { return "Arf! Arf!"; } -Actions cat_actions(void) +/* registration of methods to object interface */ + +Actions catActions(void) { - Actions this=get_mem(Actions); - this->talk = cat_talk; + Get_mem(this, Actions); + + this->talk = catTalk; return this; } -Actions dog_actions(void) +Actions dogActions(void) { - Actions this=get_mem(Actions); - this->talk = dog_talk; + Get_mem(this, Actions); + + this->talk = dogTalk; return this; } +/* usage of objects inside C<<1 program */ + program { int i; Animal animal[3]; - Actions cat = cat_actions(); - Actions dog = dog_actions(); + Actions cat = catActions(); + Actions dog = dogActions(); animal[0] = get_obj(Animal, cat, "Missy"); animal[1] = get_obj(Animal, cat, "Mr. Bojangles"); @@ -60,6 +70,7 @@ program for_range(i, 0, 2) { - printf("%s: %s\n", animal[i]->name, _(talk, animal[i])); + print(animal[i]->name, ":", _(talk, animal[i])); } + }