| 1 | #include <cll1h> |
| 2 | |
| 3 | def_community(Drawable_object,Actions); |
| 4 | |
| 5 | def_mem(Actions) |
| 6 | { |
| 7 | void method(init) (Drawable_object self, ...); |
| 8 | void method(draw) (Drawable_object self); |
| 9 | int method(desc) (Drawable_object self,int something); |
| 10 | }; |
| 11 | |
| 12 | def_mem(Tri) |
| 13 | { |
| 14 | interface(Actions); |
| 15 | int x1, y1, x2, y2, x3, y3; |
| 16 | }; |
| 17 | |
| 18 | def_mem(Rect) |
| 19 | { |
| 20 | interface(Actions); |
| 21 | int x1, y1, x2, y2; |
| 22 | }; |
| 23 | |
| 24 | construct_obj(Tri,Actions); |
| 25 | |
| 26 | construct_obj(Rect,Actions); |
| 27 | |
| 28 | |
| 29 | def_mem(Object_list) |
| 30 | { |
| 31 | Drawable_object object; |
| 32 | list(Object_list); |
| 33 | }; |
| 34 | |
| 35 | Actions init_tri_interface(Actions this) |
| 36 | { |
| 37 | this->init = init_tri; |
| 38 | this->draw = draw_tri; |
| 39 | this->desc = desc_tri; |
| 40 | |
| 41 | return this; |
| 42 | } |
| 43 | |
| 44 | Actions init_rect_interface(Actions this) |
| 45 | { |
| 46 | this->init = init_tri; |
| 47 | this->draw = draw_rect; |
| 48 | this->desc = desc_rect; |
| 49 | |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | void draw_tri(Drawable_object community) |
| 54 | { |
| 55 | I_am(Tri); |
| 56 | |
| 57 | printf("imagine I am drawing %d,%d - %d,%d - %d,%d\n", |
| 58 | self->x1,self->y1,self->x2,self->y2,self->x3,self->y3); |
| 59 | }; |
| 60 | |
| 61 | void draw_rect(Drawable_object community) |
| 62 | { |
| 63 | I_am(Rect); |
| 64 | |
| 65 | printf("imagine I am drawing %d,%d - %d,%d\n", |
| 66 | self->x1,self->y1,self->x2,self->y2); |
| 67 | }; |
| 68 | |
| 69 | int desc_tri(Drawable_object community,int something) |
| 70 | { |
| 71 | I_am(Tri); |
| 72 | |
| 73 | printf("I am triangle %d,%d - %d,%d - %d,%d, method draw called with argument: %d\n", |
| 74 | self->x1,self->y1,self->x2,self->y2,self->x3,self->y3,something); |
| 75 | |
| 76 | return something; |
| 77 | }; |
| 78 | |
| 79 | int desc_rect(Drawable_object community,int something) |
| 80 | { |
| 81 | I_am(Rect); |
| 82 | |
| 83 | printf("I am rectangle %d,%d - %d,%d, method draw called with argument: %d\n", |
| 84 | self->x1,self->y1,self->x2,self->y2,something); |
| 85 | |
| 86 | return something; |
| 87 | }; |
| 88 | |
| 89 | Tri init_tri(Drawable_object self, int x1, int y1, int x2, int y2, int x3, int y3) |
| 90 | { |
| 91 | I_am(Tri); |
| 92 | |
| 93 | self->x1 = x1; |
| 94 | self->y1 = y1; |
| 95 | self->x2 = x2; |
| 96 | self->y2 = y2; |
| 97 | self->x3 = x3; |
| 98 | self->y3 = y3; |
| 99 | |
| 100 | return self; |
| 101 | } |
| 102 | |
| 103 | Rect init_rect(Drawable_object self, int x1, int y1, int x2, int y2) |
| 104 | { |
| 105 | I_am(Rect); |
| 106 | |
| 107 | self->x1 = x1; |
| 108 | self->y1 = y1; |
| 109 | self->x2 = x2; |
| 110 | self->y2 = y2; |
| 111 | |
| 112 | return self; |
| 113 | } |
| 114 | |
| 115 | program |
| 116 | { |
| 117 | Init_mem(tri_interface,Actions,init_tri_interface); |
| 118 | Init_mem(rect_interface,Actions,init_rect_interface); |
| 119 | Object_list all = NULL, one = get_mem(Object_list); |
| 120 | |
| 121 | one->object = init_obj_as(Drawable_data, Rect, rect_interface, 0, 10, 1, 11); |
| 122 | append(one,all); |
| 123 | one->object = init_obj_as(Drawable_data, Tri, tri_interface, 0, 0, 0, 4, 3, 0); |
| 124 | append(one,all); |
| 125 | one->object = init_obj_as(Drawable_data, Rect, rect_interface, 10, 0, 11, 1); |
| 126 | append(one,all); |
| 127 | |
| 128 | for_each(one,all) |
| 129 | { |
| 130 | printf("(return value %d)\n", _(one->object,desc)); |
| 131 | _(one->object,draw); |
| 132 | } |
| 133 | } |