27 #ifndef __CLASS_CLASS_H__
28 #define __CLASS_CLASS_H__
31 #include <sys/types.h>
37 #ifndef _ISOC99_SOURCE
38 #define _ISOC99_SOURCE
41 #define CLASS_MAGIC 0xFEFE
45 typedef struct c_##name * name; \
46 extern struct class * const _##name; \
49 #define EXTENDS(parent) \
50 const char _[sizeof(struct c_##parent)]
53 #define CREATE_CLASS(name,_parent,...) \
54 static struct class c_##name; \
55 static class_ptr _classInit##name##_(void) { \
56 c_##name.parent = _##_parent; \
57 c_##name.init = NULL; \
60 static struct class c_##name = { \
63 sizeof(struct c_##name), \
64 _classInit##name##_, \
65 INIT_IFACE_IMPL(__VA_ARGS__) \
66 }; struct class * const _##name = &c_##name; \
67 struct c_##name##_object { void * class; struct c_##name data; }
77 #define INSTANCE(class, name) \
78 struct c_##class##_object _##name; \
79 class name = &(_##name.data); \
80 struct c_##class##_object _##name = { \
83 #define INIT_CLASS(class) ((class)->init? (class)->init() : (class))
84 #define GET_CLASS(object) (INIT_CLASS(*(class_ptr *)((void*)(object) - sizeof(void*))))
85 #define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface)))
86 #define HAS_PARENT(class) (NULL != ((class)->parent) && INIT_CLASS((class)->parent))
88 #define IS_OBJECT(obj) ((GET_CLASS((obj)))->magic == CLASS_MAGIC)
89 #define INSTANCE_OF(class,obj) ((GET_CLASS((obj))) == _##class)
95 #define _CALL(_class,_iface,method,...) \
97 class_ptr class = _class; \
98 iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
99 while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \
100 class = class->parent; \
101 iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
103 assert(NULL != iface->method); \
106 #define CALL(object,_iface,method,...) \
108 struct i_##_iface * iface; \
109 _CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
110 iface->method(object, ##__VA_ARGS__); \
113 #define RETCALL(object,_iface,method,ret,...) \
115 struct i_##_iface * iface; \
116 _CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
117 ret = iface->method(object, ##__VA_ARGS__); \
120 #define PARENTCALL(object,_iface,method,...) \
122 struct i_##_iface * iface; \
123 class_ptr pc_class = GET_CLASS((object)); \
124 assert(HAS_PARENT(pc_class)); \
125 _CALL(pc_class->parent, _iface, method, ##__VA_ARGS__); \
126 iface->method(object, ##__VA_ARGS__); \
131 typedef struct class * class_ptr;
141 #endif // __CLASS_CLASS_H__
class_ptr(* fptr_classInit)(void)