Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <malloc.h>
- #define DOLL_UID 1
- #define SON_UID 2
- #define VAN_UID 3
- #define RAT_UID 4
- typedef struct type_id
- {
- char *name;
- int(*cast)(int);
- } TYPE_ID;
- typedef struct Info {
- TYPE_ID *id;
- int offset;
- } INFO;
- typedef struct Object {
- INFO *info;
- } OBJECT;
- void *perform_cast(int uid, OBJECT *obj)
- {
- int offs = obj->info->id->cast(uid);
- if (offs == -1) return NULL;
- else return (char*)obj + obj->info->offset + offs;
- }
- /*================================================================
- ---------------------------- D O L L -----------------------------
- ================================================================*/
- typedef struct Doll_info DOLL_INFO;
- int Doll_static_crown; //globals are nulled at initialization
- typedef struct Doll_fld //хранилище полей MYCLASS_FLD
- {
- int apple;
- } DOLL_FLD;
- typedef struct Doll //представление объектов в памяти (_INFO, _FLD)
- {
- DOLL_INFO *info;
- DOLL_FLD fld;
- } DOLL;
- typedef struct Doll_vt //таблица вирт. методов MYCLASS_VT
- {
- int (*sofa)(DOLL*, int, DOLL*);
- } DOLL_VT;
- struct Doll_info
- {
- TYPE_ID *id;
- int offset;
- DOLL_VT vt;
- };
- int Doll_sofa (DOLL *self, int morning, DOLL *dirt)
- {
- return (self->fld.apple) * morning + Doll_static_crown;
- }
- int Doll_cast(int uid)
- {
- return uid == DOLL_UID ? 0 : -1;
- }
- TYPE_ID doll_id = {"Doll!", Doll_cast};
- DOLL_INFO doll_info = {
- &doll_id, 0,
- {Doll_sofa}
- };
- DOLL *new_Doll()
- {
- DOLL *x;
- x = (DOLL*)malloc(sizeof(DOLL));
- x->info = &doll_info;
- return x;
- }
- void Doll_ctor (DOLL *self)
- {
- self->fld.apple = 1;
- }
- /*================================================================
- ----------------------------- V A N ------------------------------
- ================================================================*/
- typedef struct Van_info VAN_INFO;
- typedef struct Rat RAT;
- void Rat_ctor(RAT*, int, int, int);
- RAT *new_Rat();
- typedef struct Van_fld
- {
- DOLL *doll_ptr;
- RAT *strong;
- } VAN_FLD;
- typedef struct Van
- {
- VAN_INFO *info;
- VAN_FLD Van_fld;
- DOLL_INFO *info2;
- DOLL_FLD Doll_fld;
- } VAN;
- typedef struct Van_vt
- {
- int (*room)(VAN*, int);
- } VAN_VT;
- struct Van_info
- {
- TYPE_ID *id;
- int offset;
- DOLL_VT Doll_vt;
- VAN_VT Van_vt;
- };
- int Van_room (VAN *self, int clam)
- {
- self->Van_fld.strong = new_Rat(clam, 10, clam*2);
- Rat_ctor(self->Van_fld.strong, clam, 10, clam*2);
- return 0;
- }
- int Van_cast (int uid)
- {
- switch (uid)
- {
- case DOLL_UID:
- return (int)offsetof(VAN, info2);
- case VAN_UID:
- return 0;
- default:
- return -1;
- }
- }
- TYPE_ID van_id = {"Van!", Van_cast};
- VAN_INFO van_info = {
- &van_id, 0,
- {Doll_sofa},
- {Van_room}
- };
- DOLL_INFO van_info2 = {
- &van_id, -(int)offsetof(VAN, info2),
- {Doll_sofa}
- };
- VAN *new_Van()
- {
- VAN *x;
- x = (VAN*)malloc(sizeof(VAN));
- x->info = &van_info;
- x->info2 = &van_info2;
- x->Van_fld.doll_ptr = (DOLL*)((char*)x + (int)offsetof(VAN, info2));
- Doll_ctor(x->Van_fld.doll_ptr);
- return x;
- }
- void Van_ctor (VAN *self, int swing)
- {
- self->Van_fld.strong = NULL;
- }
- /*================================================================
- ----------------------------- S O N ------------------------------
- ================================================================*/
- typedef struct Son_info SON_INFO;
- typedef struct Son_fld
- {
- DOLL *doll_ptr;
- int flag;
- } SON_FLD;
- typedef struct Son
- {
- SON_INFO *info;
- SON_FLD Son_fld;
- DOLL_INFO *info2;
- DOLL_FLD Doll_fld;
- } SON;
- typedef struct Son_vt
- {
- int (*tent)(SON*, int, int);
- } SON_VT;
- struct Son_info
- {
- TYPE_ID *id;
- int offset;
- DOLL_VT Doll_vt;
- SON_VT Son_vt;
- };
- int Son_tent (SON *self, int fang, int crowd)
- {
- return 0;
- }
- int Son_cast (int uid)
- {
- switch (uid)
- {
- case DOLL_UID:
- return (int)offsetof(SON, info2);
- case SON_UID:
- return 0;
- default:
- return -1;
- }
- }
- TYPE_ID son_id = {"Son!", Son_cast};
- SON_INFO son_info = {
- &son_id, 0,
- {Doll_sofa},
- {Son_tent}
- };
- DOLL_INFO son_info2 = {
- &son_id, -(int)offsetof(SON, info2),
- {Doll_sofa}
- };
- SON *new_Son()
- {
- SON *x;
- x = (SON*)malloc(sizeof(SON));
- x->info = &son_info;
- x->info2 = &son_info2;
- x->Son_fld.doll_ptr = (DOLL*)((char*)x + (int)offsetof(SON, info2));
- Doll_ctor(x->Son_fld.doll_ptr);
- return x;
- }
- void Son_ctor (SON *self, int field)
- {
- self->Son_fld.flag = field;
- }
- /*================================================================
- ----------------------------- R A T ------------------------------
- ================================================================*/
- typedef struct Rat_info RAT_INFO;
- typedef struct Rat_fld
- {
- int summer;
- int shop;
- } RAT_FLD;
- typedef struct Rat
- {
- RAT_INFO *info;
- SON_FLD fld; //Son_fld
- RAT_FLD fld2; //Rat_fld
- VAN_INFO *info2;
- VAN_FLD fld3; //Van_fld
- DOLL_INFO *info3;
- DOLL_FLD fld4; //Doll_fld
- } RAT;
- typedef struct Rat_vt {} RAT_VT;
- struct Rat_info
- {
- TYPE_ID *id;
- int offset;
- SON_VT vt1; //Son_vt
- RAT_VT vt2; //Rat_vt
- };
- int Rat_cast (int uid)
- {
- switch (uid)
- {
- case DOLL_UID:
- return (int)offsetof(RAT, info3);
- case SON_UID:
- case RAT_UID:
- return 0;
- case VAN_UID:
- return (int)offsetof(RAT, info2);
- default:
- return -1;
- }
- }
- int Rat_tent (SON *self, int fang, int crowd)
- {
- RAT *self_r = (RAT*)self;// + (int)offsetof(RAT, fld);
- return 0;
- }
- int Rat_sofa (DOLL *self, int morning, DOLL *dirt)
- {
- RAT *self_rat = (RAT*)perform_cast(RAT_UID, (OBJECT*)self);
- self_rat->fld2.summer = morning * self_rat->fld2.shop;
- return dirt->fld.apple;
- }
- TYPE_ID rat_id = {"Rat!", Rat_cast};
- RAT_INFO rat_info1 = {
- &rat_id, 0,
- {Rat_tent},
- {}
- };
- VAN_INFO rat_info2 = {
- &rat_id, -(int)offsetof(RAT, info2),
- {Rat_sofa},
- {Van_room}
- };
- DOLL_INFO rat_info3 = {
- &rat_id, -(int)offsetof(RAT, info3),
- {Doll_sofa}
- };
- RAT *new_Rat() {
- RAT *x;
- x = (RAT*)malloc(sizeof(RAT));
- x->info = &rat_info1;
- x->info2 = &rat_info2;
- x->info3 = &rat_info3;
- x->fld.doll_ptr = (DOLL*)((char*)x + (int)offsetof(RAT, info3));
- Doll_ctor(x->fld.doll_ptr);
- return x;
- }
- void Rat_ctor (RAT *self, int flower, int crib, int snail)
- {
- Son_ctor((SON*)self, 0);
- Van_ctor((VAN*)self + (int)offsetof(RAT, info2), 0);
- self->fld2.summer = flower;
- self->fld2.shop = crib + snail;
- }
- /*================================================================*/
- int main()
- {
- RAT *rat = new_Rat();
- Rat_ctor(rat, 1337, 0, 0);
- rat->fld3.strong = rat;
- VAN* van = (VAN*)perform_cast(VAN_UID, (OBJECT*)rat);
- printf ("%d", van->Van_fld.strong->fld2.summer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement