ZFFramework
 
Loading...
Searching...
No Matches
ZFObjectInterface.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFObjectInterface_h_
7#define _ZFI_ZFObjectInterface_h_
8
9#include "zfany.h"
10#include "ZFClass.h"
11
13
16
20#define zfinterface class
24#define zfimplement public
25
26// ============================================================
27zfclassNotPOD ZFLIB_ZFCore _ZFP_ObjI_Base {
28protected:
29 virtual ~_ZFP_ObjI_Base(void) {}
30
31public:
32 static const ZFClass *ClassData(void) {return zfnull;}
33public:
34 static void _ZFP_Obj_initImpl(ZFClass *cls) {}
35public:
36 static void _ZFP_ObjI_ICk(void) {}
37};
38#define _ZFP_ZFINTERFACE_DECLARE(InterfaceName, ParentInterface, OuterClass, ...) \
39 ZFCLASS_DISALLOW_COPY_CONSTRUCTOR(InterfaceName) \
40 public: \
41 static void _ZFP_zftIsZFObject(void) {} \
42 static void _ZFP_zftIsZFInterface(void) {} \
43 protected: \
44 \
45 typedef ParentInterface zfsuper; \
46 \
47 typedef InterfaceName zfself; \
48 private: \
49 static void _ZFP_Obj_initImplCk(ZF_IN ZFClass *cls) { \
50 if(cls->_ZFP_ZFClass_implListNeedInit) { \
51 cls->_ZFP_ZFClass_implListNeedInit = zffalse; \
52 if(zfself::_ZFP_Obj_initImpl != zfsuper::_ZFP_Obj_initImpl) { \
53 zfself::_ZFP_Obj_initImpl(cls); \
54 } \
55 } \
56 } \
57 public: \
58 \
59 static const ZFClass *ClassData(void) { \
60 static _ZFP_ZFClassRegisterHolder _holder( \
61 ZF_NAMESPACE_CURRENT(), \
62 zftext(#InterfaceName), \
63 ParentInterface::ClassData(), \
64 OuterClass::ClassData(), \
65 zftrue, \
66 zfnull, \
67 zfnull, \
68 zfnull, \
69 &zfself::_ZFP_Obj_initImplCk, \
70 zftrue \
71 ); \
72 return _holder.cls; \
73 } \
74 protected: \
75 virtual void _ZFP_ObjI_ICk(void) { \
76 /* used to ensure inherit from ZFInterface */ \
77 _ZFP_ObjI_Base::_ZFP_ObjI_ICk(); \
78 } \
79 public: \
80 \
81 template<typename T_ZFObject> \
82 inline T_ZFObject to(void) { \
83 return zfcast(T_ZFObject, this); \
84 } \
85 \
86 public:
94#define ZFINTERFACE_DECLARE_WITH_CUSTOM_CTOR(InterfaceName, ParentInterface, ...) \
95 ZFM_VA_APPEND(_ZFP_ZFINTERFACE_DECLARE, _ZFP_ObjI_Base, InterfaceName, ParentInterface, ##__VA_ARGS__) \
96 public:
97
100#define ZFINTERFACE_DECLARE(InterfaceName, ParentInterface, ...) \
101 ZFM_VA_APPEND(_ZFP_ZFINTERFACE_DECLARE, _ZFP_ObjI_Base, InterfaceName, ParentInterface, ##__VA_ARGS__) \
102 protected: \
103 \
104 InterfaceName(void) {} \
105 virtual ~InterfaceName(void) {} \
106 \
107 public:
108
109template<typename T_FromZFObjectOrZFInterface, typename T_ToZFInterface, int isFromInterface>
110zfclassNotPOD _ZFP_ObjICW { // interface cast wrapper
111};
112template<typename T_FromZFObjectOrZFInterface, typename T_ToZFInterface>
113zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, T_ToZFInterface, 0> {
114public:
115 static ZFInterface *_ZFP_cast(ZF_IN ZFObject *obj) {
116 return (T_ToZFInterface *)zfcast(T_FromZFObjectOrZFInterface *, obj);
117 }
118};
119template<typename T_FromZFObjectOrZFInterface, typename T_ToZFInterface>
120zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, T_ToZFInterface, 1> {
121public:
122 template<typename T_ZFObject>
123 static inline ZFInterface *_ZFP_cast(ZF_IN T_ZFObject *obj) {
124 return zfcast(T_ToZFInterface *, obj->toObject());
125 }
126};
127template<typename T_FromZFObjectOrZFInterface>
128zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, ZFInterface, 0> {
129public:
130 template<typename T_ZFObject>
131 static inline ZFInterface *_ZFP_cast(ZF_IN T_ZFObject *obj) {
132 return zfnull;
133 }
134};
135template<typename T_FromZFObjectOrZFInterface>
136zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, ZFInterface, 1> {
137public:
138 template<typename T_ZFObject>
139 static inline ZFInterface *_ZFP_cast(ZF_IN T_ZFObject *obj) {
140 return zfnull;
141 }
142};
143#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_PARAM(Interface) \
144 , Interface::ClassData(), &zfself::_ZFP_ObjICCb_##Interface
145#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_CAST_CALLBACK(Interface) \
146 static ZFInterface *_ZFP_ObjICCb_##Interface(ZF_IN ZFObject *obj) { \
147 return _ZFP_ObjICW<zfself, Interface, (zftIsTypeOf<zfself, ZFObject>::Value ? 0 : 1)>::_ZFP_cast(obj); \
148 }
149#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_INIT(Interface) \
150 Interface::_ZFP_ObjI_onInit();
151#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_DEALLOC(Interface) \
152 Interface::_ZFP_ObjI_onDealloc();
153#define _ZFP_ZFIMPLEMENT_DECLARE(ImplementedInterfaces, ...) \
154 public: \
155 static void _ZFP_Obj_initImpl(ZFClass *cls) { \
156 if(cls->_ZFP_ZFClass_interfaceNeedRegister()) { \
157 cls->_ZFP_ZFClass_interfaceRegister(0 \
158 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_PARAM, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
159 , (const ZFClass *)zfnull \
160 ); \
161 } \
162 } \
163 public: \
164 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_CAST_CALLBACK, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
165 public:
169#define ZFIMPLEMENT_DECLARE(ImplementedInterfaces, ...) \
170 _ZFP_ZFIMPLEMENT_DECLARE(ImplementedInterfaces, ##__VA_ARGS__) \
171 public: \
172 virtual inline void _ZFP_ObjI_onInitIvk(void) { \
173 zfsuper::_ZFP_ObjI_onInitIvk(); \
174 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_INIT, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
175 } \
176 virtual inline void _ZFP_ObjI_onDeallocIvk(void) { \
177 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_DEALLOC, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
178 zfsuper::_ZFP_ObjI_onDeallocIvk(); \
179 } \
180 public:
181
251 _ZFP_ZFINTERFACE_DECLARE(ZFInterface, _ZFP_ObjI_Base, _ZFP_ObjI_Base)
252
253protected:
255 ZFInterface(void) {}
256 virtual ~ZFInterface(void) {}
258
259public:
260 static void _ZFP_Obj_initImpl(ZFClass *cls) {}
261 zffinal inline void _ZFP_ObjI_onInit(void) {}
262 zffinal inline void _ZFP_ObjI_onDealloc(void) {}
263
264public:
266 virtual const ZFClass *classData(void) zfpurevirtual;
267
272};
273
290#define ZFINTERFACE_ON_INIT_DECLARE() \
291 public: \
292 zffinal void _ZFP_ObjI_onInit(void)
293
294#define ZFINTERFACE_ON_INIT_DEFINE(YourInterface) \
295 void YourInterface::_ZFP_ObjI_onInit(void)
296
298#define ZFINTERFACE_ON_DEALLOC_DECLARE() \
299 public: \
300 zffinal void _ZFP_ObjI_onDealloc(void)
301
302#define ZFINTERFACE_ON_DEALLOC_DEFINE(YourInterface) \
303 void YourInterface::_ZFP_ObjI_onDealloc(void)
304
306
307#endif // #ifndef _ZFI_ZFObjectInterface_h_
308
meta data for ZFObject
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#define zfpurevirtual
dummy macro shows that a method is pure virtual method
Definition ZFCoreTypeDef_ClassType.h:68
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define zfclassNotPOD
shows the class is not a POD type, you should not memset it or declare it in stack or copy value by c...
Definition ZFCoreTypeDef_ClassType.h:48
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define zfcast(T_To, obj)
safely cast ZFObject types, return null if not desired type
Definition ZFObjectCast.h:28
#define zfinterface
shows the type is an interface, see ZFInterface
Definition ZFObjectInterface.h:20
ZFObject's class info.
Definition ZFClass.h:67
dummy base of interface types
Definition ZFObjectInterface.h:250
virtual ZFObject * toObject(void)=0
convert to ZFObject type
virtual const ZFClass * classData(void)=0
get instance's class info
base class of all objects
Definition ZFObjectCore.h:209
util class to cast ZFObject types freely