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)
10 CONFEDERATION(Drawable_interface
,Drawable_data
);
13 struct Drawable_interface
15 void (*draw
)(void *self
);
20 CONFEDERATION(Drawable_interface
,Drawable_data
);
21 int x1
; int y1
; int x2
; int y2
; int x3
; int y3
;
26 CONFEDERATION(Drawable_interface
,Drawable_data
);
27 int x1
; int y1
; int x2
; int y2
;
30 void draw_tri(void *_self
)
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
);
36 void draw_rect(void *self
)
38 struct Rect
*r
=(struct Rect
*)self
;
39 printf("%d,%d - %d,%d\n",r
->x1
,r
->y1
,r
->x2
,r
->y2
);
42 struct Drawable_interface
*init_tri_interface(void)
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
;
50 struct Drawable_interface
*init_rect_interface(void)
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
;
58 struct Tri
*create_tri(struct Drawable_interface
*i
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
60 struct Tri
*t
=CREATE(Tri
);
64 t
->x1
=x1
;t
->y1
=y1
;t
->x2
=x2
;t
->y2
=y2
;t
->x3
=x3
;t
->y3
=y3
;
69 struct Rect
*create_rect(struct Drawable_interface
*i
, int x1
, int y1
, int x2
, int y2
)
71 struct Rect
*r
=CREATE(Rect
);
75 r
->x1
=x1
;r
->y1
=y1
;r
->x2
=x2
;r
->y2
=y2
;
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
;
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
);
95 /* a nyni ... bylo to hodne prace, ale povedlo se ... ! */
97 for(one
=all
;one
;one
=one
->next
) (*(one
->interface
->draw
))(one
);
99 /* a nyni pro efekt jeste instatni nudlovy kod s kanci prichuti: */
101 #define _(OBJECT,METHOD) OBJECT==NULL)?0:(*(OBJECT->interface->METHOD))(OBJECT
102 #define FOR_EACH(ONE,ALL) for(ONE=ALL;ONE;ONE=ONE->next)
104 FOR_EACH(one
,all
) (_(one
,draw
));
This page took 1.52171 seconds and 4 git commands to generate.