server 0.0.1
basicserverinfrastructure

include/class.h

Go to the documentation of this file.
00001 #ifndef __CLASS_H__
00002 #define __CLASS_H__
00003 
00004 #include <stdarg.h>
00005 #include <sys/types.h>
00006 #include <string.h>
00007 #include <assert.h>
00008 
00009 #include "interface.h"
00010 
00011 #define _ISOC99_SOURCE
00012 
00013 #define CLASS_MAGIC    0xFEFE
00014 
00015 #define CLASS(name)                    \
00016         struct c_##name;                   \
00017         typedef struct c_##name * name;    \
00018         extern struct class * const _##name; \
00019         struct c_##name
00020 
00021 #define EXTENDS(parent) \
00022         const char _[sizeof(struct c_##parent)]
00023 
00024 #define _NULL   NULL
00025 #define CREATE_CLASS(name,_parent,...) \
00026         static struct class c_##name;      \
00027         static void _classInit_(void) {    \
00028                 c_##name.parent = _##_parent;  \
00029                 c_##name.init   = NULL;        \
00030         }                                  \
00031         static struct class c_##name = {   \
00032                 CLASS_MAGIC,                   \
00033                 NULL,                          \
00034                 sizeof(struct c_##name),       \
00035                 _classInit_,                   \
00036                 INIT_IMPL(__VA_ARGS__)         \
00037         }; struct class * const _##name = &c_##name
00038 
00043 #define _CALL(object,_iface,method,...)                                       \
00044         class_ptr           class = class_getClass((object));                     \
00045         struct i_##_iface * iface;                                                \
00046         if (class->init)        class->init();                                        \
00047         iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface);     \
00048         while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) {   \
00049                 class = class->parent;                                                \
00050                 if (class->init)        class->init();                                    \
00051                 iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \
00052         };                                                                        \
00053         assert(NULL != iface->method);
00054 
00055 #define CALL(object,_iface,method,...)               \
00056         do {                                             \
00057                 _CALL(object, _iface, method, ##__VA_ARGS__); \
00058                 iface->method(object, ##__VA_ARGS__);        \
00059         } while(0)
00060 
00061 #define RETCALL(object,_iface,method,ret,...)        \
00062         do {                                             \
00063                 _CALL(object, _iface, method, ##__VA_ARGS__); \
00064                 ret = iface->method(object, ##__VA_ARGS__);  \
00065         } while(0)
00066 
00067 
00068 #define IFACE_GET(class,iface)          (interfaceGet(&((class)->impl),(iface)))
00069 #define IFACE_EXISTS(class,iface)       (NULL != IFACE_GET((class),(iface)))
00070 
00071 #define HAS_PARENT(class)                       (NULL != ((class)->parent))
00072 
00073 typedef void (* fptr_classInit)(void);
00074 
00075 struct class;
00076 typedef struct class * class_ptr;
00077 struct class {
00078         const int         magic;
00079         class_ptr         parent;
00080         size_t            object_size;
00081         fptr_classInit    init;
00082         struct iface_impl impl;
00083 };
00084 
00085 extern void *    class_getInterface(class_ptr *, iface_ptr);
00086 extern class_ptr class_getClass(void *);
00087 
00088 #endif // __CLASS_H__
00089 
00090 // vim: set ts=4 sw=4:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines