mGNCS API Reference  v1.2.0
A new control set and a new framework for MiniGUI apps
mobject.h
Go to the documentation of this file.
1 
42 #ifndef _MGUI_NCSCTRL_OBJECT_H
43 #define _MGUI_NCSCTRL_OBJECT_H
44 
45 #ifdef __cplusplus
46 extern "C"{
47 #endif
48 
54 #define MGNCS_INIT_CLASS(clss) \
55  Class(clss).classConstructor((mObjectClass*)(void*)(&(Class(clss))))
56 
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
64 
65 #ifdef WIN32
66 #define _TYPEOF(x) void* //__typeof(x)
67 #else
68 #define _TYPEOF(x) typeof(x)
69 #endif
70 
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); /* VW: prevent unused-but-set-variable warning */ \
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;
83 
84 #define END_MINI_CLASS return _class; }
85 
86 #define CLASS_METHOD_MAP(clss, name) \
87  _class->name = (_TYPEOF(_class->name))(clss##_##name);
88 
89 
90 #define NEED_CHECK
91 
92 #ifdef NEED_CHECK
93 #define CHECK_METHOD(cls, mth) ((cls) && (cls)->mth)
94 #else
95 #define CHECK_METHOD(cls, mth) (1)
96 #endif
97 
98 
99 #define PUBLIC
100 #define PRIVATE
101 #define PROTECTED
102 #define STATIC
103 
104 #define _M(pobj, method, ...) _c(pobj)->method((pobj), ##__VA_ARGS__)
105 
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) \
111  }; \
112  struct _##clss { \
113  clss##Header(clss) \
114  }; \
115  EXPORT extern clss##Class g_st##clss##Cls;
116 
117 #define DECLARE_CLASS(clss, clssSuper) DECLARE_CLASSEX(MGNCS_EXPORT, clss, clssSuper)
118 #define NONE_EXPORT
119 
120 #define _SUPER(super,self,method, ...) Class(super).method((super*)(self), ##__VA_ARGS__)
121 
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;
126 
127 #define DEFINE_OBJECT(Clss, ClssSuper) \
128  struct _##Clss##Class { Clss##ClassHeader(Clss, ClssSuper) }; \
129  struct _##Clss { Clss##Header(Clss) };
130 
132 #define INTERFACE(Interface) \
133  Interface##VTable * Interface##_; /*Interface vtable*/
134 
135 #define DECLARE_INTERFACE(Interface) \
136  typedef struct _##Interface##VTable Interface##VTable; \
137  typedef struct _##Interface Interface;
138 
139 #define DEFINE_INTERFACE(Interface) \
140  struct _##Interface##VTable { \
141  Interface##Header(Interface, Interface) \
142  }; \
143  struct _##Interface { Interface##VTable *_vtable; };
144 
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;
150 
151 #define INTERFACE_CAST(Interface, pobj) \
152  ((Interface*)((unsigned char*)(pobj) + (_c(pobj)->_##Interface##_obj_offset)))
153 
154 //the basic interface
155 DECLARE_INTERFACE(IInterface)
156 struct _IInterfaceVTable{
157  unsigned short _obj_offset;
158  unsigned short _next_offset;
159 };
160 
161 struct _IInterface {
162  IInterfaceVTable * _vtable;
163 };
164 
165 #define IInterfaceHeader(Interface, ClssImpl) \
166  unsigned short _##Interface##_obj_offset; \
167  unsigned short _##Interface##_next_offset; \
168 
169 
173 #define INTEFACE_ADJUST(piobj) (_TYPEOF(piobj))((unsigned char*)(piobj) - \
174  ((IInterfaceVTable*)((piobj))->_vtable)->_obj_offset)
175 
176 #define INTERFACE2OBJECT(type, piobj) (type*)((unsigned char*)(piobj) - ((IInterfaceVTable*)((piobj))->_vtable)->_obj_offset)
177 
182 #define _IM(piobj, method, ...) \
183  (piobj)->_vtable->method(INTEFACE_ADJUST(piobj), ##__VA_ARGS__)
184 #define _I(piobj) (piobj)->_vtable
185 
186 
187 
188 typedef struct _mObjectClass mObjectClass;
189 typedef struct _mObject mObject;
190 
199 typedef mObjectClass* (*PClassConstructor)(mObjectClass *);
200 
211 typedef void (*PCONSTRUCT_DWORD)(void* self, DWORD addData);
212 #include <stdarg.h>
213 typedef void (*PCONSTRUCT_VALIST)(void* self, va_list va);
214 
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*);
227 
228 
229 
230 
260 struct _mObjectClass {
261  mObjectClassHeader(mObject, mObject)
262 };
263 
268 MGNCS_EXPORT extern mObjectClass g_stmObjectCls; //Class(mObject);
269 
270 enum mObjectStatus
271 {
272  NCSF_OBJECT_TODEL = 0x1000,
273 };
274 #define NCS_OBJ_TODEL(self) (self->objStatus & NCSF_OBJECT_TODEL)
275 
276 #define mObjectHeader(clss) \
277  ClassType(clss) * _class; \
278  short objRefCount; \
279  short objStatus;
280 
291 struct _mObject {
292  mObjectHeader(mObject)
293 };
294 
295 
299 MGNCS_EXPORT void ncsRefObjFreeSpecificData(DWORD key, DWORD value);
300 
301 MGNCS_EXPORT mObject* initObject(mObject* pobj, mObjectClass* _class);
302 
306 static inline mObject* _initObject(mObject* pobj, mObjectClass* _class,DWORD param) {
307  memset(pobj, 0, _class->objSize);
308  initObject(pobj, _class);
309  ((PCONSTRUCT_DWORD)(_class->construct))(pobj, param);
310  return pobj;
311 }
312 
317 static inline mObject* initObjectArgs(mObject* pobj, mObjectClass* _class, ...)
318 {
319  va_list va;
320  va_start(va, _class);
321  memset(pobj, 0, _class->objSize);
322  initObject(pobj, _class);
323  ((PCONSTRUCT_VALIST)(_class->construct))(pobj, va);
324  va_end(va);
325  return pobj;
326 }
327 
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))
332 
354 MGNCS_EXPORT mObject * newObject(mObjectClass *_class);
355 static inline mObject * ncsNewObject(mObjectClass *_class,DWORD addData){
356  mObject * obj = newObject(_class);
357  if(!obj)
358  return NULL;
359 
360  _class->construct(obj, addData);
361  return obj;
362 }
363 
364 #define NEWEX(classType, addData) (classType*)(void*)ncsNewObject((mObjectClass*)(void *)(&Class(classType)), (DWORD)(addData))
365 #define NEW(classType) NEWEX(classType, 0)
366 static inline mObject * _newobject_args(mObjectClass* _class, ...)
367 {
368  va_list va;
369  mObject * obj;
370  va_start(va, _class);
371  obj = newObject(_class);
372  if(obj)
373  ((PCONSTRUCT_VALIST)(_class->construct))(obj, va);
374  va_end(va);
375  return obj;
376 }
377 #define NEWV(Clss, ...) ((Clss*)_newobject_args((mObjectClass*)&(Class(Clss)), ##__VA_ARGS__))
378 
386 MGNCS_EXPORT void deleteObject(mObject *obj);
387 #define DELETE(obj) deleteObject((mObject*)(obj))
388 
393 #define TYPENAME(obj) ((obj)?(((obj)->_class)?((obj)->_class)->typeName:""):"")
394 
396 
406 MGNCS_EXPORT BOOL ncsInstanceOf(mObject* object, mObjectClass* clss);
407 
416 #define INSTANCEOF(obj, clss) ncsInstanceOf((mObject*)(obj), (mObjectClass*)(void*)(&Class(clss)))
417 
426 static inline mObject* ncsIsValidObj(mObject* obj){
427  return (INSTANCEOF(obj, mObject)?(obj):NULL);
428 }
429 
436 #define CHECKOBJ(obj) ncsIsValidObj((mObject*)obj)
437 
449 MGNCS_EXPORT mObject* ncsSafeCast(mObject* obj, mObjectClass *clss);
450 
455 #define TYPE_CAST(Type, obj) ((Type*)(obj))
456 
464 #define SAFE_CAST(Type, obj) TYPE_CAST(Type, ncsSafeCast((mObject*)obj,(mObjectClass*)(void*)(&(Class(Type)))))
465 
477 MGNCS_EXPORT mObjectClass *ncsSafeCastClass(mObjectClass* clss, mObjectClass* castCls);
482 #define SAFE_CAST_CLASS(Clss, ClssCast) ((ClassType(ClssCast)*)(ncsSafeCastClass((mObjectClass*)Clss, (mObjectClass*)(void*)(&Class(ClssCast)))))
483 
484 int ncsParseConstructParams(va_list args, const char* signature, ...);
485 
499 typedef BOOL (*NCS_CB_ONOBJEVENT)(mObject* self, mObject *sender, int eventId, DWORD param);
500 
501 #define NCS_CB_ONPIECEEVENT NCS_CB_ONOBJEVENT
502 
519 MGNCS_EXPORT BOOL ncsAddEventListener(mObject *sender,
520  mObject* listener,
521  NCS_CB_ONOBJEVENT eventProc,
522  int eventId);
523 
540 MGNCS_EXPORT BOOL ncsAddEventListeners(mObject *sender,
541  mObject* listener,
542  NCS_CB_ONOBJEVENT eventProc,
543  int* eventIds);
544 
555 MGNCS_EXPORT BOOL ncsRemoveEventListener(mObject * listener);
556 
566 MGNCS_EXPORT BOOL ncsRemoveEventConnect(mObject* listener, mObject* source, int eventId);
567 
578 MGNCS_EXPORT BOOL ncsRemoveEventSource(mObject *sender);
579 
592 MGNCS_EXPORT void ncsRaiseEvent(mObject *sender, int eventId, DWORD param);
593 
594 
599 #ifdef __cplusplus
600 }
601 #endif
602 
603 
604 #endif
605 
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
the Object struct
#define INSTANCEOF(obj, clss)
A wrapper of ncsInstanceOf, just like java&#39;s instanceof.
Definition: mobject.h:416
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)
Definition: mobject.h:211
static mObject * ncsIsValidObj(mObject *obj)
Check a pointer is a valid mObject or not.
Definition: mobject.h:426
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)
Definition: mobject.h:306
static mObject * initObjectArgs(mObject *pobj, mObjectClass *_class,...)
Definition: mobject.h:317
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.
Definition: mobject.h:499
MGNCS_EXPORT mObject * ncsSafeCast(mObject *obj, mObjectClass *clss)
safe type cast function, like the dynamic_cast operator in C++