一:计算结构体成员变量偏移量宏
#ifdef LW_OOPC_USE_USER_DEFINED_OFFSETOF // 有些环境可能不支持,不过,这种情形极少出现 #define LW_OOPC_OFFSETOF(s,m) (size_t)&(((s*)0)->m) #endif
二:INTERFACE接口:接口(interface)是我们java里的一个关键字,接口只能定义抽象方法不能实现方法,接口就是一种特殊的abstract class,但是比abstract class更加抽象。
INTERFACE(type) :用来声明一个type型的的接口,并且声明相关函数。
#define INTERFACE(type) \ typedef struct type type; \ void type##_ctor(type* t); \ int type##_dtor(type* t); \ struct type
三:抽象类ABS_CLASS:抽象类既可以定义抽象方法,也可以实现方法。
ABS_CLASS(type):用来声明一个type型的的抽象类,并且声明相关函数。
#define ABS_CLASS(type) \ typedef struct type type; \ void type##_ctor(type* t); \ int type##_dtor(type* t); \ void type##_delete(type* t); \ struct type
四:类CLASS:类用来构造方法。
CLASS(type):用来声明一个type型的的类,并且声明相关函数。指针的所占内存大小是恒定的,32位为4字节。
#define CLASS(type) \ typedef struct type type; \ type* type##_new(lw_oopc_file_line_params); \ void type##_ctor(type* t); \ int type##_dtor(type* t); \ void type##_delete(type* t); \ struct type
五:构造类CTOR
CTOR(type) :用来初始化一个类,即为这个类分配内存空间。
END_CTOR:构造结束。
#ifdef LW_OOPC_SUPPORT_MEMORY_LEAK_DETECTOR #define CTOR(type) \ type* type##_new(const char* file, int line) { \ struct type *cthis; \ cthis = (struct type*)lw_oopc_malloc(sizeof(struct type), #type, file, line); \ if(!cthis) \ { \ return 0; \ } \ type##_ctor(cthis); \ return cthis; \ } \ \ void type##_ctor(type* cthis) { #else #define CTOR(type) \ type* type##_new() { \ struct type *cthis; \ cthis = (struct type*)malloc(sizeof(struct type)); \ if(!cthis) \ { \ return 0; \ } \ type##_ctor(cthis); \ return cthis; \ } \ \ void type##_ctor(type* cthis) { #endif #define END_CTOR }
六:DTOR:用来支持析构的概念。
DTOR(type) :释放一个类的内存空间。
END_DTOR:析构结束。
#define DTOR(type) \ void type##_delete(type* cthis) \ { \ if(type##_dtor(cthis)) \ { \ lw_oopc_free(cthis);\ } \ } \ int type##_dtor(type* cthis) \ { #define END_DTOR }
注意:这里的int type##_dtor(type* cthis)是释放内存的函数,是由我们自己实现的。而与构造类里的void type##_ctor(type* cthis)不同,type##_ctor(type* cthis)实现方法是调用下面定义的一些宏。
七:ABS_CTOR:用来构造可被继承的抽象类。
ABS_CTOR(type):用来初始化一个抽象类(基类)
END_ABS_CTOR:抽象类初始化结束。
#define ABS_CTOR(type) \ void type##_ctor(type* cthis) { #define END_ABS_CTOR }
八:FUNCTION_SETTING:关联类里的函数指针。
FUNCTION_SETTING(f1, f2):对于抽象类既可以实现抽象方法,也可以实现具体的方法。对于具体的类实现具体的方法。
#define FUNCTION_SETTING(f1, f2) cthis->f1 = f2;
九:#define IMPLEMENTS(type) struct type type和#define EXTENDS(type) struct type type都是用来实现继承的。
相当于在一个结构体里嵌套类另一个结构体(相当于父类)。
十: #define SUPER_PTR(cthis, father) ((father*)(&(cthis->father)))
#define SUPER_PTR_2(cthis, father, grandfather) \
SUPER_PTR(SUPER_PTR(cthis, father), grandfather)
#define SUPER_PTR_3(cthis, father, grandfather, greatgrandfather) \
SUPER_PTR(SUPER_PTR_2(cthis, father, grandfather), greatgrandfather)
该结构体指向其里面的另一个结构体(相当于父类),并且将其类型转换为父类,用于实例化方法。
十一:#define SUPER_CTOR(father) father##_ctor(SUPER_PTR(cthis, father));
将继承类转换为被继承类,为了支持子类调用父类的构造函数。和第十点意思相同。
十二:#define SUB_PTR(selfptr, self, child) \
((child*)((char*)selfptr - LW_OOPC_OFFSETOF(child, self)))
#define SUB_PTR_2(selfptr, self, child, grandchild) \
SUB_PTR(SUB_PTR(selfptr, self, child), child, grandchild)
#define SUB_PTR_3(selfptr, self, child, grandchild, greatgrandchild) \
SUB_PTR(SUB_PTR_2(selfptr, self, child, grandchild), grandchild, greatgrandchild)
现在不知道具体作用
十三:#define INHERIT_FROM(father, cthis, field) cthis->father.field
用来访问子类成员。
#define ABS_CTOR(type) \ void type##_ctor(type* cthis) { #define END_ABS_CTOR } #define FUNCTION_SETTING(f1, f2) cthis->f1 = f2; #define IMPLEMENTS(type) struct type type #define EXTENDS(type) struct type type #define SUPER_PTR(cthis, father) ((father*)(&(cthis->father))) #define SUPER_PTR_2(cthis, father, grandfather) \ SUPER_PTR(SUPER_PTR(cthis, father), grandfather) #define SUPER_PTR_3(cthis, father, grandfather, greatgrandfather) \ SUPER_PTR(SUPER_PTR_2(cthis, father, grandfather), greatgrandfather) #define SUPER_CTOR(father) \ father##_ctor(SUPER_PTR(cthis, father)); #define SUB_PTR(selfptr, self, child) \ ((child*)((char*)selfptr - LW_OOPC_OFFSETOF(child, self))) #define SUB_PTR_2(selfptr, self, child, grandchild) \ SUB_PTR(SUB_PTR(selfptr, self, child), child, grandchild) #define SUB_PTR_3(selfptr, self, child, grandchild, greatgrandchild) \ SUB_PTR(SUB_PTR_2(selfptr, self, child, grandchild), grandchild, greatgrandchild) #define INHERIT_FROM(father, cthis, field) cthis->father.field