X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=demos%2Fobjects%2Fobjects-wiki.c;h=f60351411f6ce528d41286ca3691c535b75ceb98;hb=53004c3f042bbacd06e82c2ca774ca5b9d136ab4;hp=e55cb7439a3a2ebcfb6612ee3cac2bd811ad8f10;hpb=b27abb8a41cb8e876112626ef3f5e2c156943bc2;p=svn%2FCll1h%2F.git diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c index e55cb74..f603514 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) { - Animal method(init) (Animal self, str name); str method(talk) (Animal self); }; @@ -17,48 +19,57 @@ def_obj(Animal) char *name; }; -str cat_talk(Animal self) +construct(Animal) (Animal self, str name) { - return "Meow!"; + self->name = name; + return self; } -str dog_talk(Animal self) +/* implementation of methods */ + +str catTalk(Animal self) { - return "Arf! Arf!"; + return "Meow!"; } -Animal animal_init(Animal self, str name) +str dogTalk(Animal self) { - self->name = name; - return self; + return "Arf! Arf!"; } -Actions cat_actions(Actions this) +/* registration of methods to object interface */ + +Actions catActions(void) { - this->init = animal_init; - this->talk = cat_talk; + Get_mem(this, Actions); + + this->talk = catTalk; + return this; } -Actions dog_actions(Actions this) +Actions dogActions(void) { - this->init = animal_init; - 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] = init_obj(Animal, Actions, cat) - "Missy"); - animal[1] = init_obj(Animal, cat, "Mr. Bojangles"); - animal[2] = init_obj(Animal, dog, "Lassie"); + animal[0] = get_obj(Animal, cat, "Missy"); + animal[1] = get_obj(Animal, cat, "Mr. Bojangles"); + animal[2] = get_obj(Animal, dog, "Lassie"); for_range(i, 0, 2) { - printf("%s: %s\n", animal[i].name, _(talk, animal[i])); + print(animal[i]->name, ":", _(talk,animal[i])); } }