Advertisement
pjakma

Untitled

Mar 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4.  
  5. #include "poly_types.h"
  6.  
  7. /* declare a run-time polymorphic interface, 'poly' */
  8. iface_declare(poly,
  9.   poly *(*new) (int);
  10.   void (*set) (poly *, int);
  11.   void (*print) (poly *);
  12. );
  13.  
  14. /* Define some struct shared compatibly between 2 other types.
  15.  *
  16.  * Instances of which can be manipulated via the runtime polymorphic
  17.  * 'poly'
  18.  *
  19.  */
  20. typedef struct {
  21.   poly *poly_iface;
  22.   int i;
  23. } shared;
  24.  
  25. /* foo and bar, whatever sub-types of the above. Not going to setup
  26.  * actual sub-types, just typedefs to test for now */
  27. typedef shared foo;
  28. typedef shared bar;
  29.  
  30. iface_declare_casts(shared, poly, poly_iface);
  31. iface_declare_casts(foo, poly, poly_iface);
  32. iface_declare_casts(bar, poly, poly_iface);
  33.  
  34. /* a common setter method */
  35. void poly_set (poly *, int);
  36.  
  37. /* foo specific implementations */
  38. void foo_print (poly *);
  39. poly *foo_new (int);
  40. /* bar specific implementations */
  41. poly *bar_new (int);
  42. void bar_print (poly *);
  43.  
  44. /* foo's vtable / operations */
  45. poly foo_ops = {
  46.   .set = poly_set,
  47.   .new = foo_new,
  48.   .print = foo_print,
  49. };
  50.  
  51. /* .. and bar's */
  52. poly bar_ops = {
  53.   .set = poly_set,
  54.   .new = bar_new,
  55.   .print = bar_print,
  56. };
  57.  
  58.  
  59. void
  60. poly_set (poly *p, int i)
  61. {
  62.   poly2shared(p)->i = i;
  63. }
  64.  
  65. void
  66. foo_print (poly *p)
  67. {
  68.   foo *p1 = poly2foo (p);
  69.  
  70.   printf ("foo: %u\n", p1->i);
  71. }
  72.  
  73. poly *foo_new (int i)
  74. {
  75.   foo *f = calloc (1, sizeof (foo));
  76.  
  77.   foo_poly_ops_set (f, &foo_ops);
  78.  
  79.   poly_set (foo2poly (f), i);
  80.  
  81.   return foo2poly (f);
  82. }
  83.  
  84. poly *bar_new (int i)
  85. {
  86.   bar *p = calloc (1, sizeof (bar));
  87.  
  88.   bar_poly_ops_set (p, &bar_ops);
  89.  
  90.   poly_set (bar2poly (p), i);
  91.  
  92.   return bar2poly (p);
  93. }
  94.  
  95. void
  96. bar_print (poly *p)
  97. {
  98.   bar *p2 = poly2bar (p);
  99.  
  100.   printf ("bar: %u\n", p2->i);
  101. }
  102.  
  103. int main (void)
  104. {
  105.   /* declare an array of differently typed objects */
  106.   poly *polys [] = {
  107.     foo_new (1),
  108.     bar_new (2),
  109.   };
  110.  
  111.   /* however, can easily call polymorphic functions on these objects */
  112.   for (int i = 0; i < sizeof (polys) / sizeof(polys[0]); i++)
  113.     {
  114.       polys[i]->print (polys[i]);
  115.       ifacef (poly, print, polys[i]);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement