X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=demos%2Fobjects%2Fobjects-wiki.c;h=08403cb91af186a2a8830537b0a509af8217d5c2;hb=29c30fdf2f28e2540573b40d95440e919553d0ae;hp=50e1288f2d82cd9fd58a158991751649d7a12beb;hpb=47e4883a12f36914d419e9e5fd1a0ff97227c093;p=svn%2FCll1h%2F.git diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c index 50e1288..08403cb 100644 --- a/demos/objects/objects-wiki.c +++ b/demos/objects/objects-wiki.c @@ -6,62 +6,69 @@ Of course there are easier ways to do the same thing... :-) (G)2008 xChas */ -def_mem(Actions) +def_interface(Animal,Actions) { - str method(talk) (anonymous self); + str method(talk) (Animal self); }; -def_mem(Animal) +def_obj(Animal) { interface(Actions); - char *name; + str name; }; -construct(Animal) (Animal self, str name) +construct(Animal,Actions) (Animal self, str name) { self->name = name; return self; } -str cat_talk(anonymous self) +/* implementation of methods */ + +str catTalk(Animal self) { return "Meow!"; } -str dog_talk(anonymous self) +str dogTalk(Animal self) { return "Arf! Arf!"; } -Actions cat_actions(void) +/* registration of methods to object interface */ + +Actions catActions(void) { - Get_mem(this,Actions); + Get_mem(this, Actions); - this->talk = cat_talk; + this->talk = catTalk; return this; } -Actions dog_actions(void) +Actions dogActions(void) { - Get_mem(this,Actions); + Get_mem(this, Actions); - this->talk = dog_talk; + 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"); - animal[2] = get_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) { - print(animal[i]->name,":",_(talk,animal[i])); + print(animal[i]->name, ":", _(talk, animal[i])); } + }