42 #ifndef _MGUI_NCSCTRL_OBJECT_H 43 #define _MGUI_NCSCTRL_OBJECT_H 54 #define MGNCS_INIT_CLASS(clss) \ 55 Class(clss).classConstructor((mObjectClass*)(void*)(&(Class(clss)))) 60 #define _c(ths) (ths)->_class 61 #define ClassType(className) className##Class 62 #define Class(className) g_st##className##Cls 63 #define ObjectType(className) className 66 #define _TYPEOF(x) void* //__typeof(x) 68 #define _TYPEOF(x) typeof(x) 71 #define BEGIN_MINI_CLASS(clss, superCls) \ 72 static ClassType(clss) * clss##ClassConstructor(ClassType(clss)* _class); \ 73 ClassType(clss) Class(clss) = { (PClassConstructor)clss##ClassConstructor }; \ 74 static const char* clss##_type_name = #clss; \ 75 static ClassType(clss) * clss##ClassConstructor(ClassType(clss)* _class) { \ 76 unsigned short * _pintfOffset = NULL; \ 77 _pintfOffset = (unsigned short *)((UINT_PTR)_pintfOffset ^ 0); \ 78 _class = (ClassType(clss)*)((PClassConstructor)(Class(superCls).classConstructor))((mObjectClass*)_class); \ 79 _class->super = (superCls##Class*)&Class(superCls); \ 80 _class->typeName = clss##_type_name; \ 81 _class->objSize = sizeof(clss); \ 82 _pintfOffset = &_class->intfOffset; 84 #define END_MINI_CLASS return _class; } 86 #define CLASS_METHOD_MAP(clss, name) \ 87 _class->name = (_TYPEOF(_class->name))(clss##_##name); 93 #define CHECK_METHOD(cls, mth) ((cls) && (cls)->mth) 95 #define CHECK_METHOD(cls, mth) (1) 104 #define _M(pobj, method, ...) _c(pobj)->method((pobj), ##__VA_ARGS__) 106 #define DECLARE_CLASSEX(EXPORT, clss, clssSuper) \ 107 typedef struct _##clss##Class clss##Class; \ 108 typedef struct _##clss clss; \ 109 struct _##clss##Class{ \ 110 clss##ClassHeader(clss, clssSuper) \ 115 EXPORT extern clss##Class g_st##clss##Cls; 117 #define DECLARE_CLASS(clss, clssSuper) DECLARE_CLASSEX(MGNCS_EXPORT, clss, clssSuper) 120 #define _SUPER(super,self,method, ...) Class(super).method((super*)(self), ##__VA_ARGS__) 122 #define DECLARE_OBJECT(Clss) \ 123 typedef struct _##Clss##Class Clss##Class; \ 124 typedef struct _##Clss Clss; \ 125 MGNCS_EXPORT extern Clss##Class g_st##Clss##Cls; 127 #define DEFINE_OBJECT(Clss, ClssSuper) \ 128 struct _##Clss##Class { Clss##ClassHeader(Clss, ClssSuper) }; \ 129 struct _##Clss { Clss##Header(Clss) }; 132 #define INTERFACE(Interface) \ 133 Interface##VTable * Interface##_; 135 #define DECLARE_INTERFACE(Interface) \ 136 typedef struct _##Interface##VTable Interface##VTable; \ 137 typedef struct _##Interface Interface; 139 #define DEFINE_INTERFACE(Interface) \ 140 struct _##Interface##VTable { \ 141 Interface##Header(Interface, Interface) \ 143 struct _##Interface { Interface##VTable *_vtable; }; 145 #define IMPLEMENT(Clss,Interface) \ 146 _class->_##Interface##_obj_offset = (UINT_PTR)(void*)&(((Clss*)0)->Interface##_); \ 147 _class->_##Interface##_next_offset = 0; \ 148 *_pintfOffset = (unsigned short)(UINT_PTR)(void*)&(((ClassType(Clss)*)0)->_##Interface##_obj_offset); \ 149 _pintfOffset = &_class->_##Interface##_next_offset; 151 #define INTERFACE_CAST(Interface, pobj) \ 152 ((Interface*)((unsigned char*)(pobj) + (_c(pobj)->_##Interface##_obj_offset))) 155 DECLARE_INTERFACE(IInterface)
156 struct _IInterfaceVTable{
157 unsigned short _obj_offset;
158 unsigned short _next_offset;
162 IInterfaceVTable * _vtable;
165 #define IInterfaceHeader(Interface, ClssImpl) \ 166 unsigned short _##Interface##_obj_offset; \ 167 unsigned short _##Interface##_next_offset; \ 173 #define INTEFACE_ADJUST(piobj) (_TYPEOF(piobj))((unsigned char*)(piobj) - \ 174 ((IInterfaceVTable*)((piobj))->_vtable)->_obj_offset) 176 #define INTERFACE2OBJECT(type, piobj) (type*)((unsigned char*)(piobj) - ((IInterfaceVTable*)((piobj))->_vtable)->_obj_offset) 182 #define _IM(piobj, method, ...) \ 183 (piobj)->_vtable->method(INTEFACE_ADJUST(piobj), ##__VA_ARGS__) 184 #define _I(piobj) (piobj)->_vtable 189 typedef struct _mObject
mObject;
213 typedef void (*PCONSTRUCT_VALIST)(
void*
self, va_list va);
215 #define mObjectClassHeader(clss, superCls) \ 216 PClassConstructor classConstructor; \ 217 ClassType(superCls) * super; \ 218 const char* typeName; \ 219 unsigned short objSize; \ 220 unsigned short intfOffset; \ 221 void (*construct)(clss *self, DWORD addData); \ 222 void (*destroy)(clss *self); \ 223 DWORD (*hash)(clss *self); \ 224 const char* (*toString)(clss *self, char* str, int max); \ 225 int (*addRef)(clss*); \ 226 int (*release)(clss*); 260 struct _mObjectClass {
272 NCSF_OBJECT_TODEL = 0x1000,
274 #define NCS_OBJ_TODEL(self) (self->objStatus & NCSF_OBJECT_TODEL) 276 #define mObjectHeader(clss) \ 277 ClassType(clss) * _class; \ 299 MGNCS_EXPORT
void ncsRefObjFreeSpecificData(DWORD key, DWORD value);
307 memset(pobj, 0, _class->objSize);
308 initObject(pobj, _class);
320 va_start(va, _class);
321 memset(pobj, 0, _class->objSize);
322 initObject(pobj, _class);
323 ((PCONSTRUCT_VALIST)(_class->construct))(pobj, va);
328 #define INIT_OBJEX(Clss, pobj, param) ((Clss* )_initObject((mObject*)(pobj), (mObjectClass*)&Class(Clss), (DWORD)(param))) 329 #define INIT_OBJ(Clss, pobj) INIT_OBJEX(Clss, pobj, 0) 330 #define INIT_OBJV(Clss, pobj, ...) ((Clss* )initObjectArgs((mObject*)(pobj), (mObjectClass*)&Class(Clss), ##__VA_ARGS__)) 331 #define UNIT_OBJ(pobj) (_c(pobj)->destroy(pobj)) 360 _class->construct(obj, addData);
364 #define NEWEX(classType, addData) (classType*)(void*)ncsNewObject((mObjectClass*)(void *)(&Class(classType)), (DWORD)(addData)) 365 #define NEW(classType) NEWEX(classType, 0) 370 va_start(va, _class);
373 ((PCONSTRUCT_VALIST)(_class->construct))(obj, va);
377 #define NEWV(Clss, ...) ((Clss*)_newobject_args((mObjectClass*)&(Class(Clss)), ##__VA_ARGS__)) 387 #define DELETE(obj) deleteObject((mObject*)(obj)) 393 #define TYPENAME(obj) ((obj)?(((obj)->_class)?((obj)->_class)->typeName:""):"") 416 #define INSTANCEOF(obj, clss) ncsInstanceOf((mObject*)(obj), (mObjectClass*)(void*)(&Class(clss))) 436 #define CHECKOBJ(obj) ncsIsValidObj((mObject*)obj) 455 #define TYPE_CAST(Type, obj) ((Type*)(obj)) 464 #define SAFE_CAST(Type, obj) TYPE_CAST(Type, ncsSafeCast((mObject*)obj,(mObjectClass*)(void*)(&(Class(Type))))) 482 #define SAFE_CAST_CLASS(Clss, ClssCast) ((ClassType(ClssCast)*)(ncsSafeCastClass((mObjectClass*)Clss, (mObjectClass*)(void*)(&Class(ClssCast))))) 484 int ncsParseConstructParams(va_list args,
const char* signature, ...);
501 #define NCS_CB_ONPIECEEVENT NCS_CB_ONOBJEVENT 592 MGNCS_EXPORT
void ncsRaiseEvent(mObject *sender,
int eventId, DWORD param);
MGNCS_EXPORT mObjectClass * ncsSafeCastClass(mObjectClass *clss, mObjectClass *castCls)
cast from a supper class to derived class type safely
MGNCS_EXPORT BOOL ncsAddEventListener(mObject *sender, mObject *listener, NCS_CB_ONOBJEVENT eventProc, int eventId)
connect sender object to listener object
MGNCS_EXPORT BOOL ncsRemoveEventSource(mObject *sender)
remove the connections when a sender is destroied
#define INSTANCEOF(obj, clss)
A wrapper of ncsInstanceOf, just like java's instanceof.
MGNCS_EXPORT BOOL ncsAddEventListeners(mObject *sender, mObject *listener, NCS_CB_ONOBJEVENT eventProc, int *eventIds)
connect a group of events from sender to listener
MGNCS_EXPORT BOOL ncsInstanceOf(mObject *object, mObjectClass *clss)
check an object is the class instance or not, same as instanceof operator in Java ...
MGNCS_EXPORT mObject * newObject(mObjectClass *_class)
new a object instance, like new operator in C++
void(* PCONSTRUCT_DWORD)(void *self, DWORD addData)
static mObject * ncsIsValidObj(mObject *obj)
Check a pointer is a valid mObject or not.
MGNCS_EXPORT BOOL ncsRemoveEventConnect(mObject *listener, mObject *source, int eventId)
remove a special event connect
MGNCS_EXPORT BOOL ncsRemoveEventListener(mObject *listener)
remove the connections which are listened by the object
MGNCS_EXPORT void deleteObject(mObject *obj)
delete a object intance, like delete operator in C++
static mObject * _initObject(mObject *pobj, mObjectClass *_class, DWORD param)
static mObject * initObjectArgs(mObject *pobj, mObjectClass *_class,...)
MGNCS_EXPORT void ncsRaiseEvent(mObject *sender, int eventId, DWORD param)
raise an event to listeners
the basic object class of NCS
MGNCS_EXPORT mObjectClass g_stmObjectCls
the singleton of mObjectClass, represent a class object
BOOL(* NCS_CB_ONOBJEVENT)(mObject *self, mObject *sender, int eventId, DWORD param)
The Callback of connection.
MGNCS_EXPORT mObject * ncsSafeCast(mObject *obj, mObjectClass *clss)
safe type cast function, like the dynamic_cast operator in C++