Advertisement
Guest User

Untitled

a guest
May 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. #define exec(Object, Method) Object->table.Method()
  6.  
  7. typedef int32_t int32;
  8. typedef uint8_t uint8;
  9.  
  10. typedef void (*set_fcn_ptr) (void*, int32);
  11. typedef int32 (*get_fcn_ptr) (void*);
  12. typedef void (*display_fcn_ptr) (void);
  13.  
  14. typedef struct {
  15.     set_fcn_ptr set_x;
  16.     get_fcn_ptr get_x;
  17.  
  18.     set_fcn_ptr set_y;
  19.     get_fcn_ptr get_y;
  20.  
  21.     display_fcn_ptr display;
  22. } VTable;
  23.  
  24. /*
  25. ================================================================================
  26. */
  27.  
  28. typedef struct {
  29.     VTable table;
  30.  
  31.     int32 x;
  32.     int32 y;
  33. } Shape;
  34.  
  35. void shape_set_x(Shape* shape, int32 x) { shape->x = x; }
  36. void shape_set_y(Shape* shape, int32 y) { shape->y = y; }
  37. int32 shape_get_x(Shape* shape) { return shape->x; }
  38. int32 shape_get_y(Shape* shape) { return shape->y; }
  39. void shape_display(Shape* shape) { fputs("Shape", stdout); }
  40.  
  41. Shape* make_shape(int32 x, int32 y)
  42. {
  43.     Shape* shape = malloc(sizeof(Shape));
  44.     VTable* vtable = &shape->table;
  45.  
  46.     vtable->set_x = shape_set_x;
  47.     vtable->set_y = shape_set_y;
  48.     vtable->get_x = shape_get_x;
  49.     vtable->get_y = shape_get_y;
  50.     vtable->display = shape_display;
  51.  
  52.     shape->x = x;
  53.     shape->y = y;
  54. }
  55.  
  56. /*
  57. ================================================================================
  58. */
  59.  
  60. typedef struct {
  61.     Shape base;
  62.  
  63.     size_t width;
  64.     size_t height;
  65. } Rectangle;
  66.  
  67. void rect_set_x(Rectangle* rect, int32 x) { rect->base.x = x; }
  68. void rect_set_y(Rectangle* rect, int32 y) { rect->base.y = y; }
  69. int32 rect_get_x(Rectangle* rect) { return rect->base.x; }
  70. int32 rect_get_y(Rectangle* rect) { return rect->base.y; }
  71. void rect_display(Rectangle* rect) { fputs("Rectangle", stdout); }
  72.  
  73. Rectangle* make_rectangle(int32 x, int32 y, size_t w, size_t h)
  74. {
  75.     Rectangle* rect = malloc(sizeof(Rectangle));
  76.  
  77.     VTable* vtable = &rect->base.table;
  78.  
  79.     vtable->set_x = rect_set_x;
  80.     vtable->set_y = rect_set_y;
  81.     vtable->get_x = rect_get_x;
  82.     vtable->get_y = rect_get_y;
  83.     vtable->display = rect_display;
  84.  
  85.     rect->base.x = x;
  86.     rect->base.y = y;
  87.     rect->width = w;
  88.     rect->height = h;
  89. }
  90.  
  91. /*
  92. ================================================================================
  93. */
  94.  
  95. int main()
  96. {
  97.     Shape* shapes[4];
  98.  
  99.     shapes[0] = make_shape(3, 5);
  100.     shapes[1] = make_rectangle(7, 2, 10, 10);
  101.     shapes[2] = make_shape(10, 2);
  102.     shapes[3] = make_rectangle(0, 7, 50, 50);
  103.  
  104.     for (uint8 i = 0; i < 4; ++i) {
  105.         exec(shapes[i], display);
  106.     }
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement