Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////file class.h
- #ifndef CLASS_H_ /* Include guard */
- #define CLASS_H_
- #define CLASS typedef struct
- #define NEW(type) type##_new()
- #define NEW_1(type, param) type##_new_1(param)
- #endif
- /////////////////////////////////////////////////file base.h
- #ifndef BASE_H_ /* Include guard */
- #define BASE_H_
- #include "class.h"
- typedef struct _public public_t;
- #define GET_PUB(self) (public_t*)(self + (((base_t*)self)->size - sizeof(public_t)));
- typedef struct _public{
- public_t* (*get)(void* self);
- void (*draw)(void* self);
- int (*getId)(void* self);
- void (*setPoint)(void* self, int x, int y);
- void (*inherits)(public_t *pub_src, public_t *pub_dest);
- } public_t;
- CLASS base{
- int size;
- int id;
- public_t pub;
- } __attribute__((packed)) base_t;
- void base_t_new(base_t* bse);
- #endif
- /////////////////////////////////////////////////file base.c
- #include <stdlib.h>
- #include <stdio.h>
- #include "base.h"
- static int getId(void* self){
- printf("base:");
- fflush(stdout);
- return ((base_t*)self)->id;
- }
- static public_t* get(void* self){
- int size = ((base_t*)self)->size - sizeof(public_t);
- return self + size;
- }
- static void inherits(public_t* pub_src, public_t* pub_dest){
- printf("inherits base\n");
- fflush(stdout);
- pub_dest->getId = pub_src->getId;
- pub_dest->get = pub_src->get;
- //pub_dest.inherits = pub_src.inherits;
- }
- void base_t_new(base_t* bse){
- printf("new base\n");
- fflush(stdout);
- bse->size = sizeof(base_t);//init size object
- bse->pub.getId = getId; //link with method
- bse->pub.get = get; //link with method
- bse->pub.inherits = inherits; //link with method
- }
- /////////////////////////////////////////////////file child.h
- #ifndef CHILD_H_ /* Include guard */
- #define CHILD_H_
- #include "base.h"
- typedef struct child{
- base_t super;
- char name[10];
- public_t pub;
- } __attribute__((packed)) child_t;
- void child_t_new(child_t* chld);
- #endif
- /////////////////////////////////////////////////file child.c
- #include <stdlib.h>
- #include <stdio.h>
- #include "base.h"
- #include "child.h"
- static int getId(void* self){
- printf("child:");
- fflush(stdout);
- base_t *bse = &((child_t*)self)->super;
- return bse->pub.getId(bse);
- }
- static void inherits(public_t *pub_src, public_t* pub_dest){
- printf("inherits child\n");
- fflush(stdout);
- pub_dest->getId = pub_src->getId;
- }
- void child_t_new(child_t *chld){
- printf("new child \n");
- fflush(stdout);
- base_t_new(&chld->super); //call base constructor
- base_t* bse = (base_t*)chld;
- bse->size = sizeof(child_t); //init size object
- chld->super.pub.inherits(&chld->super.pub, &chld->pub);//call base inherits
- chld->pub.inherits = inherits; //inherits functions for child
- chld->pub.getId = getId;
- }
- /////////////////////////////////////////////////file point.h
- #ifndef POINT_H_ /* Include guard */
- #define POINT_H_
- #include "child.h"
- ////////////// Point
- typedef struct point{
- child_t super;
- // base_t super; //<-- link owner
- // //size int;<-- base
- // //int id;<-- base
- // //public_t pub; <-- base
- // public_t pub;
- int x, y; //new fields
- public_t pub;
- } __attribute__((packed)) point_t;
- void point_t_new(point_t* pt);
- #endif
- /////////////////////////////////////////////////file point.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "base.h"
- #include "child.h"
- #include "point.h"
- static int getId(void* self){
- return 0;//((point_t*)self)->super->getId(((point_t*)self)->super);
- }
- static void setPoint(void* self, int x, int y){
- point_t* pt = (point_t*)self;
- pt->x = x;
- pt->y = y;
- }
- static void inherits(public_t *pub_src, public_t *pub_dest){
- printf("inherits point\n");
- fflush(stdout);
- pub_dest->setPoint = pub_src->setPoint;
- }
- void point_t_new(point_t* pt){
- printf("new point \n");
- fflush(stdout);
- child_t_new(&pt->super); //call base constructor
- base_t* bse = (base_t*)pt;
- bse->size = sizeof(point_t); //init size object
- pt->super.pub.inherits(&pt->super.pub, &pt->pub); //inherits functions
- pt->pub.setPoint = setPoint; //set new functions
- //pt->pub.getId = pt->super.pub.getId;
- }
- /////////////////////////////////////////////////file main.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include "base.h"
- #include "child.h"
- #include "point.h"
- #define N 3
- int main(int argc, char **argv){
- base_t *bse = malloc(sizeof(base_t));
- child_t chl;
- point_t pt;
- bse->id = 10;
- chl.super.id = 1;
- pt.super.super.id = 12; //may use var before call constructor
- base_t_new(bse); //call constructor
- child_t_new(&chl);
- point_t_new(&pt);
- void* mass[N] = {&chl, bse, &pt};
- for (int i=0; i<N; i++){
- public_t *pub = ((base_t*)mass[i])->pub.get(mass[i]);
- //public_t *pub = GET_PUB(mass[i]); //may use macros GET_PUB
- int id = pub->getId(mass[i]);
- printf("id=%i\n", id);
- }
- pt.pub.setPoint(&pt, 1, 1);
- free(bse);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement