📄 ooc.h
字号:
----------------------------------------------- macros for object identification, cast & delete -----------------------------------------------*/#define super(obj, super) \ (&((obj)->m.super))#define className(obj) \ (typeid(obj)->name)#define isA(obj, class) \ ((obj)->__vptr == (void*)(class).__vptr)#define offsetOf(obj) \ ((obj)->__iptr->offset)#define methodAddr(obj, msg) \ ((obj)->__vptr->msg)#define typeid(obj) \ (((t_object*)base_cast(obj))->__iptr->info)#define base_cast(obj) \ ((void*const)((char*)(obj) - offsetOf(obj)))#define super_cast(obj, super) \ super(obj, super)#define static_cast(obj, objclass, subclass) \ ((_concat_(t_,subclass)*const)((char*)(obj) - \ offsetof(_concat_(t_,subclass),m.objclass)))#define dynamic_cast(obj, class) \ ((_concat_(t_,class)*)_ooc_castDynamic_((void*)(obj), (void*)&(class)))#define delete(obj) \ ((obj) ? \ (typeid(obj)->class->_object(base_cast(obj)), \ free(base_cast(obj))): \ (void)0)/* ----------------------------- macros for method declaration -----------------------------*/#ifdef IMPLEMENTATION# undef classMethod# undef classMethod_# undef method# undef method_# undef constMethod# undef constMethod_# define classMethod(fct) \ (*fct)(void)# define classMethod_(fct) \ (*fct)(# define method(fct) \ (*fct) (t_OBJECT *const)# define method_(fct) \ (*fct) (t_OBJECT *const,# define constMethod(fct) \ (*fct) (t_OBJECT const*const)# define constMethod_(fct) \ (*fct) (t_OBJECT const*const,# define initClassDecl() \ void classMethodDecl(initClass)# define dtorDecl() \ void methodDecl(_concat_(_,OBJECT))# define classMethodDecl(fct) \ static methodName(fct)(void)# define classMethodDecl_(fct) \ static methodName(fct)(# define methodDecl(fct) \ static methodName(fct)(t_OBJECT *const this)# define methodDecl_(fct) \ static methodName(fct)(t_OBJECT *const this,# define constMethodDecl(fct) \ static methodName(fct)(t_OBJECT const*const this)# define constMethodDecl_(fct) \ static methodName(fct)(t_OBJECT const*const this,# define methodOvldDecl(fct, obj) \ static methodOvldName(fct,obj)(_concat_(t_,obj) *const this)# define methodOvldDecl_(fct, obj) \ static methodOvldName(fct,obj)(_concat_(t_,obj) *const this,# define constMethodOvldDecl(fct, obj) \ static methodOvldName(fct,obj)(_concat_(t_,obj) const*const this)# define constMethodOvldDecl_(fct, obj) \ static methodOvldName(fct,obj)(_concat_(t_,obj) const*const this,# define methodName(fct) \ _concat_(_concat_(fct,_m_),OBJECT)# define methodOvldName(fct, obj) \ _concat_(_concat_(fct,_m_),_concat_(_concat_(obj,_),OBJECT))#else# define classMethod(fct) \ (*const fct)(void)# define classMethod_(fct) \ (*const fct)(# define method(fct) \ (*const fct) (t_OBJECT *const)# define method_(fct) \ (*const fct) (t_OBJECT *const,# define constMethod(fct) \ (*const fct) (t_OBJECT const*const)# define constMethod_(fct) \ (*const fct) (t_OBJECT const*const,#endif/* -------------------------- macros for message passing --------------------------*//* object messages */#define sendMsg(obj, msg) \ ((obj)->__vptr->msg)(obj)#define sendMsg_(obj, msg) \ ((obj)->__vptr->msg)(obj,/* class messages */#define sendCMsg(obj, class, msg) \ ((class).__vptr->msg)(obj)#define sendCMsg_(obj, class, msg) \ ((class).__vptr->msg)(obj,/* arguments closure #undef __ or use _concat_(_,_concat_(_,token)) if you want to build token with __*/#define __ )/* -------------------------- macros for generic objects --------------------------*/#define GENERIC(obj) _concat_(gTypePrefix, obj)#define GENERIC_DTOR(obj) _concat_(_,GENERIC(obj))/* ----------- base object -----------*//* maximum number of super classes */#define _OBJECT_MAXEXTRASUPER_ 4/* disable _CONST__ if requested by user */#ifdef ALLOW_OBJCOPY# define _CONST__# define objCopy(a,b) ((a) = (b))#else# define _CONST__ const# define objCopy(a,b) memcpy(&(a), &(b), sizeof(a))#endif/* disable _CONST_ in implementation */#ifdef IMPLEMENTATION# undef _CONST_# define _CONST_#else# undef _CONST_# define _CONST_ _CONST__#endif#ifndef _OOC_INCLUDE_ONCE_#define _OOC_INCLUDE_ONCE_/* object type (public) */typedef union { struct _ooc_vtbl_object const *_CONST__ __vptr; struct _ooc_vtbl_object _CONST_*_CONST_ __iptr;} t_object;/* object virtual table type (private) */struct _ooc_vtbl_object { struct _ooc_type_info const*_CONST_ info; size_t offset;};/* object class type (private) */struct _ooc_class_object { struct _ooc_vtbl_object const*const __vptr; t_object (*const object) (void); void (*const _object) (t_object *const);};/* object RTTI (private) */struct _ooc_type_info { char const*const name; struct _ooc_class_object const*_CONST_ class; t_object const*_CONST_ obj; struct _ooc_type_info const*_CONST_ super; size_t _CONST_ extraSuper; size_t _CONST_ extraOffset[_OBJECT_MAXEXTRASUPER_];};#endif/* ------------ debug info ------------*/#if defined(DEBUG_CALL) || defined(DEBUG_MEM) || defined(DEBUG_OBJ)# include <oodebug.h>#endif#ifndef DEBUG_MEM/* for compatibility with oodebug.h */extern char *strdup(const char*); /* not ISO C, but in libc or ooc.c */#endif/* ------------ declarations ------------*//* Extern declarations*/extern void _ooc_updateRef_ (t_object *const, struct _ooc_type_info const*const);extern void*const _ooc_castDynamic_ (t_object const*const, struct _ooc_class_object const*const);extern void ooc_delete (void *const);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -