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_ObjI_reg(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_ObjI_regCk(ZF_IN ZFClass *cls) { \
50 if(!cls->_ZFP_ZFClass_interfaceHasRegisterCk()) { \
51 cls->_ZFP_ZFClass_interfaceHasRegisterCk(zftrue); \
52 if(zfself::_ZFP_ObjI_reg != zfsuper::_ZFP_ObjI_reg) { \
53 zfself::_ZFP_ObjI_reg(cls); \
54 } \
55 } \
56 } \
57 public: \
58 \
59 static const ZFClass *ClassData(void) { \
60 static _ZFP_ZFClassRegisterHolder h( \
61 ZF_NAMESPACE_CURRENT() \
62 , zftext(#InterfaceName) \
63 , ParentInterface::ClassData() \
64 , OuterClass::ClassData() \
65 , zftrue \
66 , zfnull \
67 , zfnull \
68 , &zfself::_ZFP_ObjI_regCk \
69 , zftrue \
70 ); \
71 return h.cls; \
72 } \
73 protected: \
74 virtual void _ZFP_ObjI_ICk(void) { \
75 /* used to ensure inherit from ZFInterface */ \
76 _ZFP_ObjI_Base::_ZFP_ObjI_ICk(); \
77 } \
78 public: \
79 \
80 template<typename T_ZFObject> \
81 inline T_ZFObject to(void) { \
82 return zfcast(T_ZFObject, this); \
83 } \
84 \
85 public:
93#define ZFINTERFACE_DECLARE_WITH_CUSTOM_CTOR(InterfaceName, ParentInterface, ...) \
94 ZFM_VA_APPEND(_ZFP_ZFINTERFACE_DECLARE, _ZFP_ObjI_Base, InterfaceName, ParentInterface, ##__VA_ARGS__) \
95 public:
96
99#define ZFINTERFACE_DECLARE(InterfaceName, ParentInterface, ...) \
100 ZFM_VA_APPEND(_ZFP_ZFINTERFACE_DECLARE, _ZFP_ObjI_Base, InterfaceName, ParentInterface, ##__VA_ARGS__) \
101 protected: \
102 \
103 InterfaceName(void) {} \
104 virtual ~InterfaceName(void) {} \
105 \
106 public:
107
108template<typename T_FromZFObjectOrZFInterface, typename T_ToZFInterface, int isFromInterface>
109zfclassNotPOD _ZFP_ObjICW { // interface cast wrapper
110};
111template<typename T_FromZFObjectOrZFInterface, typename T_ToZFInterface>
112zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, T_ToZFInterface, 0> {
113public:
114 static ZFInterface *_ZFP_cast(ZF_IN ZFObject *obj) {
115 return (T_ToZFInterface *)zfcast(T_FromZFObjectOrZFInterface *, obj);
116 }
117};
118template<typename T_FromZFObjectOrZFInterface, typename T_ToZFInterface>
119zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, T_ToZFInterface, 1> {
120public:
121 template<typename T_ZFObject>
122 static inline ZFInterface *_ZFP_cast(ZF_IN T_ZFObject *obj) {
123 return zfcast(T_ToZFInterface *, obj->toObject());
124 }
125};
126template<typename T_FromZFObjectOrZFInterface>
127zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, ZFInterface, 0> {
128public:
129 template<typename T_ZFObject>
130 static inline ZFInterface *_ZFP_cast(ZF_IN T_ZFObject *obj) {
131 return zfnull;
132 }
133};
134template<typename T_FromZFObjectOrZFInterface>
135zfclassNotPOD _ZFP_ObjICW<T_FromZFObjectOrZFInterface, ZFInterface, 1> {
136public:
137 template<typename T_ZFObject>
138 static inline ZFInterface *_ZFP_cast(ZF_IN T_ZFObject *obj) {
139 return zfnull;
140 }
141};
142#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_PARAM(Interface) \
143 , Interface::ClassData(), &zfself::_ZFP_ObjICCb_##Interface
144#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_CAST_CALLBACK(Interface) \
145 static ZFInterface *_ZFP_ObjICCb_##Interface(ZF_IN ZFObject *obj) { \
146 return _ZFP_ObjICW<zfself, Interface, (zftIsTypeOf<zfself, ZFObject>::Value ? 0 : 1)>::_ZFP_cast(obj); \
147 }
148#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_INIT(Interface) \
149 Interface::_ZFP_ObjI_onInit();
150#define _ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_DEALLOC(Interface) \
151 Interface::_ZFP_ObjI_onDealloc();
152#define _ZFP_ZFIMPLEMENT_DECLARE(ImplementedInterfaces, ...) \
153 public: \
154 static void _ZFP_ObjI_reg(ZFClass *cls) { \
155 if(!cls->_ZFP_ZFClass_interfaceHasRegister()) { \
156 cls->_ZFP_ZFClass_interfaceRegister(0 \
157 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_PARAM, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
158 , (const ZFClass *)zfnull \
159 ); \
160 } \
161 } \
162 public: \
163 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_CAST_CALLBACK, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
164 public:
168#define ZFIMPLEMENT_DECLARE(ImplementedInterfaces, ...) \
169 _ZFP_ZFIMPLEMENT_DECLARE(ImplementedInterfaces, ##__VA_ARGS__) \
170 public: \
171 virtual inline void _ZFP_ObjI_ctor(void) { \
172 zfsuper::_ZFP_ObjI_ctor(); \
173 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_INIT, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
174 } \
175 virtual inline void _ZFP_ObjI_dtor(void) { \
176 ZFM_FIX_PARAM(_ZFP_ZFIMPLEMENT_DECLARE_EXPAND_INTERFACE_ON_DEALLOC, ZFM_EMPTY, ImplementedInterfaces, ##__VA_ARGS__) \
177 zfsuper::_ZFP_ObjI_dtor(); \
178 } \
179 public:
180
250 _ZFP_ZFINTERFACE_DECLARE(ZFInterface, _ZFP_ObjI_Base, _ZFP_ObjI_Base)
251
252protected:
254 ZFInterface(void) {}
255 virtual ~ZFInterface(void) {}
257
258public:
259 static void _ZFP_ObjI_reg(ZFClass *cls) {}
260 zffinal inline void _ZFP_ObjI_onInit(void) {}
261 zffinal inline void _ZFP_ObjI_onDealloc(void) {}
262
263public:
265 virtual const ZFClass *classData(void) zfpurevirtual;
266
271};
272
289#define ZFINTERFACE_ON_INIT_DECLARE() \
290 public: \
291 zffinal void _ZFP_ObjI_onInit(void)
292
293#define ZFINTERFACE_ON_INIT_DEFINE(YourInterface) \
294 void YourInterface::_ZFP_ObjI_onInit(void)
295
297#define ZFINTERFACE_ON_DEALLOC_DECLARE() \
298 public: \
299 zffinal void _ZFP_ObjI_onDealloc(void)
300
301#define ZFINTERFACE_ON_DEALLOC_DEFINE(YourInterface) \
302 void YourInterface::_ZFP_ObjI_onDealloc(void)
303
305
306#endif // #ifndef _ZFI_ZFObjectInterface_h_
307
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:196
#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 zfinterface
shows the type is an interface, see ZFInterface
Definition ZFObjectInterface.h:20
ZFObject's class info.
Definition ZFClass.h:63
dummy base of interface types
Definition ZFObjectInterface.h:249
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:195
util class to cast ZFObject types freely
#define zfcast(T_To, obj)
safely cast ZFObject types, return null if not desired type
Definition zfcast.h:24