Advertisement
Ladies_Man

LowLevel - Res(works)

Dec 25th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.10 KB | None | 0 0
  1. #include <stddef.h>
  2. #include <malloc.h>
  3.  
  4. #define DOLL_UID 1
  5. #define SON_UID 2
  6. #define VAN_UID 3
  7. #define RAT_UID 4
  8.  
  9. typedef struct type_id
  10. {
  11.     char *name;
  12.     int(*cast)(int);
  13. } TYPE_ID;
  14.  
  15. typedef struct Info {
  16.     TYPE_ID *id;
  17.     int offset;
  18. } INFO;
  19.  
  20. typedef struct Object {
  21.     INFO *info;
  22. } OBJECT;
  23.  
  24. void *perform_cast(int uid, OBJECT *obj)
  25. {
  26.     int offs = obj->info->id->cast(uid);
  27.     if (offs == -1) return NULL;
  28.     else return (char*)obj + obj->info->offset + offs;
  29. }
  30.  
  31. /*================================================================
  32. ---------------------------- D O L L -----------------------------
  33. ================================================================*/
  34. typedef struct Doll_info DOLL_INFO;
  35.  
  36. int Doll_static_crown;             //globals are nulled at initialization
  37.  
  38. typedef struct Doll_fld     //хранилище полей MYCLASS_FLD
  39. {
  40.     int apple;
  41. } DOLL_FLD;
  42.  
  43. typedef struct Doll         //представление объектов в памяти (_INFO, _FLD)
  44. {
  45.     DOLL_INFO *info;
  46.     DOLL_FLD fld;
  47. } DOLL;
  48.  
  49. typedef struct Doll_vt      //таблица вирт. методов MYCLASS_VT
  50. {
  51.     int (*sofa)(DOLL*, int, DOLL*);
  52. } DOLL_VT;
  53.  
  54. struct Doll_info
  55. {
  56.     TYPE_ID *id;
  57.     int offset;
  58.     DOLL_VT vt;
  59. };
  60.  
  61. int Doll_sofa (DOLL *self, int morning, DOLL *dirt)
  62. {
  63.     return (self->fld.apple) * morning + Doll_static_crown;
  64. }
  65.  
  66. int Doll_cast(int uid)
  67. {
  68.     return uid == DOLL_UID ? 0 : -1;
  69. }
  70.  
  71. TYPE_ID doll_id = {"Doll!", Doll_cast};
  72.  
  73. DOLL_INFO doll_info = {
  74.     &doll_id, 0,
  75.     {Doll_sofa}
  76. };
  77.  
  78. DOLL *new_Doll()    
  79. {
  80.     DOLL *x;
  81.     x = (DOLL*)malloc(sizeof(DOLL));
  82.     x->info = &doll_info;
  83.     return x;
  84. }
  85.  
  86. void Doll_ctor (DOLL *self)
  87. {
  88.     self->fld.apple = 1;
  89. }
  90.  
  91.  
  92.  
  93. /*================================================================
  94. ----------------------------- V A N ------------------------------
  95. ================================================================*/
  96. typedef struct Van_info VAN_INFO;
  97.  
  98. typedef struct Rat RAT;
  99. void Rat_ctor(RAT*, int, int, int);
  100. RAT *new_Rat();
  101.  
  102. typedef struct Van_fld
  103. {
  104.     DOLL *doll_ptr;
  105.     RAT *strong;
  106. } VAN_FLD;
  107.  
  108. typedef struct Van
  109. {
  110.     VAN_INFO *info;
  111.     VAN_FLD Van_fld;
  112.     DOLL_INFO *info2;
  113.     DOLL_FLD Doll_fld;
  114. } VAN;
  115.  
  116. typedef struct Van_vt
  117. {
  118.     int (*room)(VAN*, int);
  119. } VAN_VT;
  120.  
  121. struct Van_info
  122. {
  123.     TYPE_ID *id;
  124.     int offset;
  125.     DOLL_VT Doll_vt;
  126.     VAN_VT Van_vt;
  127. };
  128.  
  129. int Van_room (VAN *self, int clam)
  130. {
  131.     self->Van_fld.strong = new_Rat(clam, 10, clam*2);
  132.     Rat_ctor(self->Van_fld.strong, clam, 10, clam*2);
  133.     return 0;
  134. }
  135.  
  136. int Van_cast (int uid)
  137. {
  138.     switch (uid)
  139.     {
  140.     case DOLL_UID:
  141.         return (int)offsetof(VAN, info2);
  142.     case VAN_UID:
  143.         return 0;
  144.     default:
  145.         return -1;
  146.     }
  147. }
  148.  
  149. TYPE_ID van_id = {"Van!", Van_cast};
  150.  
  151. VAN_INFO van_info = {
  152.     &van_id, 0,
  153.     {Doll_sofa},
  154.     {Van_room}
  155. };
  156.  
  157. DOLL_INFO van_info2 = {
  158.     &van_id, -(int)offsetof(VAN, info2),
  159.     {Doll_sofa}
  160. };
  161.  
  162. VAN *new_Van()          
  163. {
  164.     VAN *x;
  165.     x = (VAN*)malloc(sizeof(VAN));
  166.     x->info = &van_info;
  167.     x->info2 = &van_info2;
  168.     x->Van_fld.doll_ptr = (DOLL*)((char*)x + (int)offsetof(VAN, info2));
  169.     Doll_ctor(x->Van_fld.doll_ptr);
  170.     return x;
  171. }
  172.  
  173. void Van_ctor (VAN *self, int swing)
  174. {
  175.     self->Van_fld.strong = NULL;
  176. }
  177.  
  178.  
  179.  
  180. /*================================================================
  181. ----------------------------- S O N ------------------------------
  182. ================================================================*/
  183. typedef struct Son_info SON_INFO;
  184.  
  185. typedef struct Son_fld
  186. {
  187.     DOLL *doll_ptr;
  188.     int flag;
  189. } SON_FLD;
  190.  
  191. typedef struct Son
  192. {
  193.     SON_INFO *info;
  194.     SON_FLD Son_fld;
  195.     DOLL_INFO *info2;
  196.     DOLL_FLD Doll_fld;
  197. } SON;
  198.  
  199. typedef struct Son_vt
  200. {
  201.     int (*tent)(SON*, int, int);
  202. } SON_VT;
  203.  
  204. struct Son_info
  205. {
  206.     TYPE_ID *id;
  207.     int offset;
  208.     DOLL_VT Doll_vt;
  209.     SON_VT Son_vt;
  210. };
  211.  
  212. int Son_tent (SON *self, int fang, int crowd)
  213. {
  214.     return 0;
  215. }
  216.  
  217. int Son_cast (int uid)
  218. {
  219.     switch (uid)
  220.     {
  221.     case DOLL_UID:
  222.         return (int)offsetof(SON, info2);
  223.     case SON_UID:
  224.         return 0;
  225.     default:
  226.         return -1;
  227.     }
  228. }
  229.  
  230. TYPE_ID son_id = {"Son!", Son_cast};
  231.  
  232. SON_INFO son_info = {
  233.     &son_id, 0,
  234.     {Doll_sofa},
  235.     {Son_tent}
  236. };
  237.  
  238. DOLL_INFO son_info2 = {
  239.     &son_id, -(int)offsetof(SON, info2),
  240.     {Doll_sofa}
  241. };
  242.  
  243. SON *new_Son()    
  244. {
  245.     SON *x;
  246.     x = (SON*)malloc(sizeof(SON));
  247.     x->info = &son_info;
  248.     x->info2 = &son_info2;
  249.     x->Son_fld.doll_ptr = (DOLL*)((char*)x + (int)offsetof(SON, info2));
  250.     Doll_ctor(x->Son_fld.doll_ptr);
  251.     return x;
  252. }
  253.  
  254. void Son_ctor (SON *self, int field)
  255. {
  256.     self->Son_fld.flag = field;
  257. }
  258.  
  259.  
  260.  
  261. /*================================================================
  262. ----------------------------- R A T ------------------------------
  263. ================================================================*/
  264. typedef struct Rat_info RAT_INFO;
  265.  
  266. typedef struct Rat_fld
  267. {
  268.     int summer;
  269.     int shop;
  270. } RAT_FLD;
  271.  
  272. typedef struct Rat
  273. {
  274.     RAT_INFO *info;
  275.     SON_FLD fld;    //Son_fld
  276.     RAT_FLD fld2;    //Rat_fld
  277.     VAN_INFO *info2;
  278.     VAN_FLD fld3;    //Van_fld
  279.     DOLL_INFO *info3;
  280.     DOLL_FLD fld4;  //Doll_fld
  281. } RAT;
  282.  
  283. typedef struct Rat_vt {} RAT_VT;
  284.  
  285. struct Rat_info
  286. {
  287.     TYPE_ID *id;
  288.     int offset;
  289.     SON_VT vt1;  //Son_vt
  290.     RAT_VT vt2;  //Rat_vt
  291. };
  292.  
  293. int Rat_cast (int uid)
  294. {
  295.     switch (uid)
  296.     {
  297.     case DOLL_UID:
  298.         return (int)offsetof(RAT, info3);
  299.     case SON_UID:
  300.     case RAT_UID:
  301.         return 0;
  302.     case VAN_UID:
  303.         return (int)offsetof(RAT, info2);
  304.     default:
  305.         return -1;
  306.     }
  307. }
  308.  
  309. int Rat_tent (SON *self, int fang, int crowd)
  310. {
  311.     RAT *self_r = (RAT*)self;// + (int)offsetof(RAT, fld);
  312.     return 0;
  313. }
  314.  
  315. int Rat_sofa (DOLL *self, int morning, DOLL *dirt)
  316. {
  317.     RAT *self_rat = (RAT*)perform_cast(RAT_UID, (OBJECT*)self);
  318.     self_rat->fld2.summer = morning * self_rat->fld2.shop;
  319.     return dirt->fld.apple;
  320. }
  321.  
  322. TYPE_ID rat_id = {"Rat!", Rat_cast};
  323.  
  324. RAT_INFO rat_info1 = {
  325.     &rat_id, 0,
  326.     {Rat_tent},
  327.     {}
  328. };
  329.  
  330. VAN_INFO rat_info2 = {
  331.     &rat_id, -(int)offsetof(RAT, info2),
  332.     {Rat_sofa},
  333.     {Van_room}
  334. };
  335.  
  336. DOLL_INFO rat_info3 = {
  337.     &rat_id, -(int)offsetof(RAT, info3),
  338.     {Doll_sofa}
  339. };
  340.  
  341. RAT *new_Rat() {
  342.     RAT *x;
  343.     x = (RAT*)malloc(sizeof(RAT));
  344.     x->info = &rat_info1;
  345.     x->info2 = &rat_info2;
  346.     x->info3 = &rat_info3;
  347.     x->fld.doll_ptr = (DOLL*)((char*)x + (int)offsetof(RAT, info3));
  348.     Doll_ctor(x->fld.doll_ptr);
  349.     return x;
  350. }
  351.  
  352. void Rat_ctor (RAT *self, int flower, int crib, int snail)
  353. {
  354.     Son_ctor((SON*)self, 0);
  355.     Van_ctor((VAN*)self + (int)offsetof(RAT, info2), 0);
  356.     self->fld2.summer = flower;
  357.     self->fld2.shop = crib + snail;
  358. }
  359.  
  360. /*================================================================*/
  361.  
  362. int main()
  363. {
  364.  
  365.     RAT *rat = new_Rat();
  366.     Rat_ctor(rat, 1337, 0, 0);
  367.  
  368.     rat->fld3.strong = rat;
  369.  
  370.     VAN* van = (VAN*)perform_cast(VAN_UID, (OBJECT*)rat);
  371.  
  372.     printf ("%d", van->Van_fld.strong->fld2.summer);
  373.     return 0;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement