00001
00016 #ifndef _MGUI_NCSCTRL_OBJECT_H
00017 #define _MGUI_NCSCTRL_OBJECT_H
00018
00019 #ifdef __cplusplus
00020 extern "C"{
00021 #endif
00022
00028 #define MGNCS_INIT_CLASS(clss) \
00029 Class(clss).classConstructor((mObjectClass*)(void*)(&(Class(clss))))
00030
00034 #define _c(ths) (ths)->_class
00035 #define ClassType(className) className##Class
00036 #define Class(className) g_st##className##Cls
00037 #define ObjectType(className) className
00038
00039 #ifdef WIN32
00040 #define _TYPEOF(x) void* //__typeof(x)
00041 #else
00042 #define _TYPEOF(x) typeof(x)
00043 #endif
00044
00045 #define BEGIN_MINI_CLASS(clss, superCls) \
00046 static ClassType(clss) * clss##ClassConstructor(ClassType(clss)* _class); \
00047 ClassType(clss) Class(clss) = { (PClassConstructor)clss##ClassConstructor }; \
00048 static const char* clss##_type_name = #clss; \
00049 static ClassType(clss) * clss##ClassConstructor(ClassType(clss)* _class) { \
00050 unsigned short * _pintfOffset; \
00051 _class = (ClassType(clss)*)((PClassConstructor)(Class(superCls).classConstructor))((mObjectClass*)_class); \
00052 _class->super = &Class(superCls); \
00053 _class->typeName = clss##_type_name; \
00054 _class->objSize = sizeof(clss); \
00055 _pintfOffset = &_class->intfOffset;
00056
00057 #define END_MINI_CLASS return _class; }
00058
00059 #define CLASS_METHOD_MAP(clss, name) \
00060 _class->name = (_TYPEOF(_class->name))(clss##_##name);
00061
00062
00063 #define NEED_CHECK
00064
00065 #ifdef NEED_CHECK
00066 #define CHECK_METHOD(cls, mth) ((cls) && (cls)->mth)
00067 #else
00068 #define CHECK_METHOD(cls, mth) (1)
00069 #endif
00070
00071
00072 #define PUBLIC
00073 #define PRIVATE
00074 #define PROTECTED
00075 #define STATIC
00076
00077 #define _M(pobj, method, ...) _c(pobj)->method((pobj), ##__VA_ARGS__)
00078
00079 #define DECLARE_CLASSEX(EXPORT, clss, clssSuper) \
00080 typedef struct _##clss##Class clss##Class; \
00081 typedef struct _##clss clss; \
00082 struct _##clss##Class{ \
00083 clss##ClassHeader(clss, clssSuper) \
00084 }; \
00085 struct _##clss { \
00086 clss##Header(clss) \
00087 }; \
00088 EXPORT extern clss##Class g_st##clss##Cls;
00089
00090 #define DECLARE_CLASS(clss, clssSuper) DECLARE_CLASSEX(MGNCS_EXPORT, clss, clssSuper)
00091 #define NONE_EXPORT
00092
00093 #define _SUPER(super,self,method, ...) Class(super).method((super*)(self), ##__VA_ARGS__)
00094
00095 #define DECLARE_OBJECT(Clss) \
00096 typedef struct _##Clss##Class Clss##Class; \
00097 typedef struct _##Clss Clss; \
00098 MGNCS_EXPORT extern Clss##Class g_st##Clss##Cls;
00099
00100 #define DEFINE_OBJECT(Clss, ClssSuper) \
00101 struct _##Clss##Class { Clss##ClassHeader(Clss, ClssSuper) }; \
00102 struct _##Clss { Clss##Header(Clss) };
00103
00105 #define INTERFACE(Interface) \
00106 Interface##VTable * Interface##_;
00107
00108 #define DECLARE_INTERFACE(Interface) \
00109 typedef struct _##Interface##VTable Interface##VTable; \
00110 typedef struct _##Interface Interface;
00111
00112 #define DEFINE_INTERFACE(Interface) \
00113 struct _##Interface##VTable { \
00114 Interface##Header(Interface, Interface) \
00115 }; \
00116 struct _##Interface { Interface##VTable *_vtable; };
00117
00118 #define IMPLEMENT(Clss,Interface) \
00119 _class->_##Interface##_obj_offset = (unsigned int)(void*)&(((Clss*)0)->Interface##_); \
00120 _class->_##Interface##_next_offset = 0; \
00121 *_pintfOffset = (unsigned short)(unsigned int)(void*)&(((ClassType(Clss)*)0)->_##Interface##_obj_offset); \
00122 _pintfOffset = &_class->_##Interface##_next_offset;
00123
00124 #define INTERFACE_CAST(Interface, pobj) \
00125 ((Interface*)((unsigned char*)(pobj) + (_c(pobj)->_##Interface##_obj_offset)))
00126
00127
00128 DECLARE_INTERFACE(IInterface)
00129 struct _IInterfaceVTable{
00130 unsigned short _obj_offset;
00131 unsigned short _next_offset;
00132 };
00133
00134 struct _IInterface {
00135 IInterfaceVTable * _vtable;
00136 };
00137
00138 #define IInterfaceHeader(Interface, ClssImpl) \
00139 unsigned short _##Interface##_obj_offset; \
00140 unsigned short _##Interface##_next_offset; \
00141
00142
00146 #define INTEFACE_ADJUST(piobj) (_TYPEOF(piobj))((unsigned char*)(piobj) - \
00147 ((IInterfaceVTable*)((piobj))->_vtable)->_obj_offset)
00148
00149 #define INTERFACE2OBJECT(type, piobj) (type*)((unsigned char*)(piobj) - ((IInterfaceVTable*)((piobj))->_vtable)->_obj_offset)
00150
00155 #define _IM(piobj, method, ...) \
00156 (piobj)->_vtable->method(INTEFACE_ADJUST(piobj), ##__VA_ARGS__)
00157 #define _I(piobj) (piobj)->_vtable
00158
00159
00160
00161 typedef struct _mObjectClass mObjectClass;
00162 typedef struct _mObject mObject;
00163
00172 typedef mObjectClass* (*PClassConstructor)(mObjectClass *);
00173
00184 typedef void (*PCONSTRUCT_DWORD)(void* self, DWORD addData);
00185 #include <stdarg.h>
00186 typedef void (*PCONSTRUCT_VALIST)(void* self, va_list va);
00187
00188 #define mObjectClassHeader(clss, superCls) \
00189 PClassConstructor classConstructor; \
00190 ClassType(superCls) * super; \
00191 const char* typeName; \
00192 unsigned short objSize; \
00193 unsigned short intfOffset; \
00194 void (*construct)(clss *self, DWORD addData); \
00195 void (*destroy)(clss *self); \
00196 DWORD (*hash)(clss *self); \
00197 const char* (*toString)(clss *self, char* str, int max); \
00198 int (*addRef)(clss*); \
00199 int (*release)(clss*);
00200
00201
00202
00203
00233 struct _mObjectClass {
00234 mObjectClassHeader(mObject, mObject)
00235 };
00236
00241 MGNCS_EXPORT extern mObjectClass g_stmObjectCls;
00242
00243 enum mObjectStatus
00244 {
00245 NCSF_OBJECT_TODEL = 0x1000,
00246 };
00247 #define NCS_OBJ_TODEL(self) (self->objStatus & NCSF_OBJECT_TODEL)
00248
00249 #define mObjectHeader(clss) \
00250 ClassType(clss) * _class; \
00251 short objRefCount; \
00252 short objStatus;
00253
00264 struct _mObject {
00265 mObjectHeader(mObject)
00266 };
00267
00268
00272 MGNCS_EXPORT void ncsRefObjFreeSpecificData(DWORD key, DWORD value);
00273
00274 MGNCS_EXPORT mObject* initObject(mObject* pobj, mObjectClass* _class);
00275
00279 static inline mObject* _initObject(mObject* pobj, mObjectClass* _class,DWORD param) {
00280 memset(pobj, 0, _class->objSize);
00281 initObject(pobj, _class);
00282 ((PCONSTRUCT_DWORD)(_class->construct))(pobj, param);
00283 return pobj;
00284 }
00285
00290 static inline mObject* initObjectArgs(mObject* pobj, mObjectClass* _class, ...)
00291 {
00292 va_list va;
00293 va_start(va, _class);
00294 memset(pobj, 0, _class->objSize);
00295 initObject(pobj, _class);
00296 ((PCONSTRUCT_VALIST)(_class->construct))(pobj, va);
00297 va_end(va);
00298 return pobj;
00299 }
00300
00301 #define INIT_OBJEX(Clss, pobj, param) ((Clss* )_initObject((mObject*)(pobj), (mObjectClass*)&Class(Clss), (DWORD)(param)))
00302 #define INIT_OBJ(Clss, pobj) INIT_OBJEX(Clss, pobj, 0)
00303 #define INIT_OBJV(Clss, pobj, ...) ((Clss* )initObjectArgs((mObject*)(pobj), (mObjectClass*)&Class(Clss), ##__VA_ARGS__))
00304 static inline int GET_ARG_COUNT(va_list va){
00305 union {
00306 va_list va;
00307 DWORD dva;
00308 }_va;
00309 _va.va = va;
00310 if(_va.dva == 0)
00311 return 0;
00312 return 1;
00313 }
00314 #define UNIT_OBJ(pobj) (_c(pobj)->destroy(pobj))
00315
00316
00317
00318
00340 MGNCS_EXPORT mObject * newObject(mObjectClass *_class);
00341 static inline mObject * ncsNewObject(mObjectClass *_class,DWORD addData){
00342 mObject * obj = newObject(_class);
00343 if(!obj)
00344 return NULL;
00345
00346 _class->construct(obj, addData);
00347 return obj;
00348 }
00349
00350 #define NEWEX(classType, addData) (classType*)ncsNewObject((mObjectClass*)(void *)(&Class(classType)), addData)
00351 #define NEW(classType) NEWEX(classType, 0)
00352 static inline mObject * _newobject_args(mObjectClass* _class, ...)
00353 {
00354 va_list va;
00355 mObject * obj;
00356 va_start(va, _class);
00357 obj = newObject(_class);
00358 if(obj)
00359 ((PCONSTRUCT_VALIST)(_class->construct))(obj, va);
00360 va_end(va);
00361 return obj;
00362 }
00363 #define NEWV(Clss, ...) ((Clss*)_newobject_args((mObjectClass*)&(Class(Clss)), ##__VA_ARGS__))
00364
00372 MGNCS_EXPORT void deleteObject(mObject *obj);
00373 #define DELETE(obj) deleteObject((mObject*)(obj))
00374
00379 #define TYPENAME(obj) ((obj)?(((obj)->_class)?((obj)->_class)->typeName:""):"")
00380
00382
00392 MGNCS_EXPORT BOOL ncsInstanceOf(mObject* object, mObjectClass* clss);
00393
00402 #define INSTANCEOF(obj, clss) ncsInstanceOf((mObject*)(obj), (mObjectClass*)(void*)(&Class(clss)))
00403
00412 static inline mObject* ncsIsValidObj(mObject* obj){
00413 return (INSTANCEOF(obj, mObject)?(obj):NULL);
00414 }
00415
00422 #define CHECKOBJ(obj) ncsIsValidObj((mObject*)obj)
00423
00435 MGNCS_EXPORT mObject* ncsSafeCast(mObject* obj, mObjectClass *clss);
00436
00441 #define TYPE_CAST(Type, obj) ((Type*)(obj))
00442
00450 #define SAFE_CAST(Type, obj) TYPE_CAST(Type, ncsSafeCast((mObject*)obj,(mObjectClass*)(void*)(&(Class(Type)))))
00451
00463 MGNCS_EXPORT mObjectClass *ncsSafeCastClass(mObjectClass* clss, mObjectClass* castCls);
00468 #define SAFE_CAST_CLASS(Clss, ClssCast) ((ClassType(ClssCast)*)(ncsSafeCastClass((mObjectClass*)Clss, (mObjectClass*)(void*)(&Class(ClssCast)))))
00469
00470 int ncsParseConstructParams(va_list args, const char* signature, ...);
00471
00485 typedef BOOL (*NCS_CB_ONOBJEVENT)(mObject* self, mObject *sender, int eventId, DWORD param);
00486
00487 #define NCS_CB_ONPIECEEVENT NCS_CB_ONOBJEVENT
00488
00505 MGNCS_EXPORT BOOL ncsAddEventListener(mObject *sender,
00506 mObject* listener,
00507 NCS_CB_ONOBJEVENT eventProc,
00508 int eventId);
00509
00526 MGNCS_EXPORT BOOL ncsAddEventListeners(mObject *sender,
00527 mObject* listener,
00528 NCS_CB_ONOBJEVENT eventProc,
00529 int* eventIds);
00530
00541 MGNCS_EXPORT BOOL ncsRemoveEventListener(mObject * listener);
00542
00552 MGNCS_EXPORT BOOL ncsRemoveEventConnect(mObject* listener, mObject* source, int eventId);
00553
00564 MGNCS_EXPORT BOOL ncsRemoveEventSource(mObject *sender);
00565
00578 MGNCS_EXPORT void ncsRaiseEvent(mObject *sender, int eventId, DWORD param);
00579
00580
00585 #ifdef __cplusplus
00586 }
00587 #endif
00588
00589
00590 #endif
00591