097bac9a |
1 | #include <stdio.h> |
2 | #include <malloc.h> |
3 | |
4 | #define CONFEDERATION(IFACE,TYPE) struct IFACE *interface;struct TYPE *next |
5 | #define CREATE(TYPE) (struct TYPE *)malloc(sizeof(struct TYPE)) |
6 | #define APPEND(TYPE,MEMBER,LIST) (MEMBER?MEMBER->next=(struct TYPE *)LIST:0,LIST=(struct TYPE *)MEMBER) |
7 | |
8 | struct Drawable_data |
9 | { |
10 | CONFEDERATION(Drawable_interface,Drawable_data); |
11 | }; |
12 | |
13 | struct Drawable_interface |
14 | { |
15 | void (*draw)(void *self); |
16 | }; |
17 | |
18 | struct Tri |
19 | { |
20 | CONFEDERATION(Drawable_interface,Drawable_data); |
21 | int x1; int y1; int x2; int y2; int x3; int y3; |
22 | }; |
23 | |
24 | struct Rect |
25 | { |
26 | CONFEDERATION(Drawable_interface,Drawable_data); |
27 | int x1; int y1; int x2; int y2; |
28 | }; |
29 | |
30 | void draw_tri(void *_self) |
31 | { |
32 | struct Tri *self=(struct Tri *)_self; |
33 | printf("%d,%d - %d,%d - %d,%d\n",self->x1,self->y1,self->x2,self->y2,self->x3,self->y3); |
34 | }; |
35 | |
36 | void draw_rect(void *self) |
37 | { |
38 | struct Rect *r=(struct Rect *)self; |
39 | printf("%d,%d - %d,%d\n",r->x1,r->y1,r->x2,r->y2); |
40 | }; |
41 | |
42 | struct Drawable_interface *init_tri_interface(void) |
43 | { |
44 | struct Drawable_interface *i=CREATE(Drawable_interface); |
45 | /* vsechny metody je bohuzel potreba bindnout rucne, neda se svitit... */ |
46 | if(i) i->draw=draw_tri; |
47 | return i; |
48 | } |
49 | |
50 | struct Drawable_interface *init_rect_interface(void) |
51 | { |
52 | struct Drawable_interface *i=CREATE(Drawable_interface); |
53 | /* vsechny metody je bohuzel potreba bindnout rucne, neda se svitit... */ |
54 | if(i) i->draw=draw_rect; |
55 | return i; |
56 | } |
57 | |
58 | struct Tri *create_tri(struct Drawable_interface *i, int x1, int y1, int x2, int y2, int x3, int y3) |
59 | { |
60 | struct Tri *t=CREATE(Tri); |
61 | if(i && t) |
62 | { |
63 | t->interface=i; |
64 | t->x1=x1;t->y1=y1;t->x2=x2;t->y2=y2;t->x3=x3;t->y3=y3; |
65 | } |
66 | return t; |
67 | } |
68 | |
69 | struct Rect *create_rect(struct Drawable_interface *i, int x1, int y1, int x2, int y2) |
70 | { |
71 | struct Rect *r=CREATE(Rect); |
72 | if(i && r) |
73 | { |
74 | r->interface=i; |
75 | r->x1=x1;r->y1=y1;r->x2=x2;r->y2=y2; |
76 | } |
77 | return r; |
78 | } |
79 | |
80 | int main(void) |
81 | { |
82 | struct Drawable_interface *tri_interface=init_tri_interface(); |
83 | struct Drawable_interface *rect_interface=init_rect_interface(); |
84 | struct Drawable_data *all=NULL,*one; |
85 | struct Tri *t; |
86 | struct Rect *r; |
87 | |
88 | r=create_rect(rect_interface,0,10,1,11); |
89 | APPEND(Drawable_data,r,all); |
90 | t=create_tri(tri_interface,0,0,0,4,3,0); |
91 | APPEND(Drawable_data,t,all); |
92 | r=create_rect(rect_interface,10,0,11,1); |
93 | APPEND(Drawable_data,r,all); |
94 | |
95 | /* a nyni ... bylo to hodne prace, ale povedlo se ... ! */ |
96 | |
97 | for(one=all;one;one=one->next) (*(one->interface->draw))(one); |
98 | |
99 | /* a nyni pro efekt jeste instatni nudlovy kod s kanci prichuti: */ |
100 | |
101 | #define _(OBJECT,METHOD) OBJECT==NULL)?0:(*(OBJECT->interface->METHOD))(OBJECT |
102 | #define FOR_EACH(ONE,ALL) for(ONE=ALL;ONE;ONE=ONE->next) |
103 | |
104 | FOR_EACH(one,all) (_(one,draw)); |
105 | |
106 | return 0; |
107 | } |