ZFFramework
 
Loading...
Searching...
No Matches
ZFCallback.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCallback_h_
7#define _ZFI_ZFCallback_h_
8
9#include "ZFMethod.h"
10#include "ZFObjectCast.h"
11#include "ZFSerializableData.h"
12
14
15// ============================================================
26
27#define ZFTOKEN_ZFCallbackTypeDummy "Dummy"
29#define ZFTOKEN_ZFCallbackTypeMethod "Method"
31#define ZFTOKEN_ZFCallbackTypeMemberMethod "MemeberMethod"
33#define ZFTOKEN_ZFCallbackTypeRawFunction "RawFunction"
35#define ZFTOKEN_ZFCallbackTypeLambda "Lambda"
36
37// ============================================================
38// callback invoker
39extern ZFLIB_ZFCore void _ZFP_ZFCallback_executeNullCallback(void);
43#define _ZFP_ZFCALLBACK_INVOKER(N) \
44 \
45 template<typename T_ReturnType ZFM_REPEAT(N, ZFM_REPEAT_TEMPLATE, ZFM_COMMA, ZFM_COMMA)> \
46 T_ReturnType executeExact(ZFM_REPEAT(N, ZFM_REPEAT_PARAM, ZFM_EMPTY, ZFM_COMMA)) const { \
47 switch(this->callbackType()) { \
48 case ZFCallbackTypeMethod: \
49 case ZFCallbackTypeMemberMethod: \
50 return this->callbackMethod()->executeExact<T_ReturnType ZFM_REPEAT(N, ZFM_REPEAT_TYPE, ZFM_COMMA, ZFM_COMMA)>( \
51 this->callbackOwnerObject() \
52 ZFM_REPEAT(N, ZFM_REPEAT_NAME, ZFM_COMMA, ZFM_COMMA) \
53 ); \
54 case ZFCallbackTypeRawFunction: \
55 return ((T_ReturnType (*)(ZFM_REPEAT(N, ZFM_REPEAT_TYPE, ZFM_EMPTY, ZFM_COMMA)))(this->callbackRawFunction())) \
56 (ZFM_REPEAT(N, ZFM_REPEAT_NAME, ZFM_EMPTY, ZFM_COMMA)); \
57 case ZFCallbackTypeLambda: { \
58 ZFCallback holder_(*this); \
59 return ((T_ReturnType (*)(void * ZFM_REPEAT(N, ZFM_REPEAT_TYPE, ZFM_COMMA, ZFM_COMMA)))(this->callbackLambdaInvoker()))( \
60 this->callbackLambdaImpl() \
61 ZFM_REPEAT(N, ZFM_REPEAT_NAME, ZFM_COMMA, ZFM_COMMA) \
62 ); \
63 } \
64 case ZFCallbackTypeDummy: \
65 default: \
66 _ZFP_ZFCallback_executeNullCallback(); \
67 return ((T_ReturnType (*)(void))(zfnull)) (); \
68 } \
69 }
70
71typedef void (*_ZFP_ZFCallbackLambdaDeleteCallback)(void *);
72
73// ============================================================
74// ZFCallback
76zfclassFwd _ZFP_ZFCallbackPrivate;
97public:
99 ZFCallback(void);
100 ZFCallback(ZF_IN const zfnullT &dummy);
101 ZFCallback(ZF_IN const ZFCallback &ref);
102 ZFCallback &operator = (ZF_IN const ZFCallback &ref);
103 ZFCallback &operator = (ZF_IN const zfnullT &dummy);
104 ~ZFCallback(void);
105 zfbool operator == (ZF_IN const ZFCallback &ref) const {return (this->objectCompare(ref) == ZFCompareEqual);}
106 zfbool operator != (ZF_IN const ZFCallback &ref) const {return (this->objectCompare(ref) != ZFCompareEqual);}
107 zfbool operator == (ZF_IN const zfnullT &dummy) const {return (d == zfnull);}
108 zfbool operator != (ZF_IN const zfnullT &dummy) const {return (d != zfnull);}
109 operator bool (void) const {return this->valid();}
110 static ZFCallback _ZFP_ZFCallbackCreateMethod(ZF_IN const ZFMethod *callbackMethod);
111 static ZFCallback _ZFP_ZFCallbackCreateMemberMethod(
114 );
115 static ZFCallback _ZFP_ZFCallbackCreateRawFunction(ZF_IN ZFFuncAddrType callbackRawFunction);
116 static ZFCallback _ZFP_ZFCallbackCreateLambda(
118 , ZF_IN _ZFP_ZFCallbackLambdaDeleteCallback callbackLambdaImplDestroy
120 );
122
123 _ZFP_ZFCALLBACK_INVOKER(0)
124 _ZFP_ZFCALLBACK_INVOKER(1)
125 _ZFP_ZFCALLBACK_INVOKER(2)
126 _ZFP_ZFCALLBACK_INVOKER(3)
127 _ZFP_ZFCALLBACK_INVOKER(4)
128 _ZFP_ZFCALLBACK_INVOKER(5)
129 _ZFP_ZFCALLBACK_INVOKER(6)
130 _ZFP_ZFCALLBACK_INVOKER(7)
131 _ZFP_ZFCALLBACK_INVOKER(8)
132 /* ZFMETHOD_MAX_PARAM */
133
134public:
139
143 zffinal inline zfstring objectInfo(void) const {
144 zfstring ret;
145 this->objectInfoT(ret);
146 return ret;
147 }
148
165
166public:
173
186 zffinal const zfstring &callbackId(void) const;
187
192
205 ZF_IN const zfstring &key
206 , ZF_IN ZFObject *tag
207 );
214 ) const;
218 inline void callbackTagRemove(ZF_IN const zfstring &key) {
219 this->callbackTag(key, zfnull);
220 }
221
234
238 zffinal inline zfbool valid(void) const {
239 return (this->callbackType() != ZFCallbackTypeDummy);
240 }
241
246
252
256 zffinal const ZFMethod *callbackMethod(void) const;
257
262
266 zffinal void *callbackLambdaImpl(void) const;
271 zffinal void _ZFP_ZFCallback_callbackLambdaInvoker(ZF_IN ZFFuncAddrType v) const;
272
286
287 // ============================================================
288public:
293 this->callbackId(src.callbackId());
294 if(src.callbackSerializeDisable()) {
296 }
297 else {
298 this->callbackSerializeType(src.callbackSerializeType());
299 this->callbackSerializeData(src.callbackSerializeData());
300 }
301 this->pathInfo(src.pathInfo());
302 }
303
304 // ============================================================
305 // callback serialize logic
306public:
326
333
339
340 // ============================================================
341 // local path logic
342public:
349
350private:
351 _ZFP_ZFCallbackPrivate *d;
352};
354 return v0 == v1 ? ZFCompareEqual : ZFCompareUncomparable;
355})
356
357// ============================================================
358// ZFCallback create
365#define ZFCallbackForMethod(zfmethod) \
366 ZFCallback::_ZFP_ZFCallbackCreateMethod(zfmethod)
367
368
375#define ZFCallbackForMemberMethod(obj, zfmethod) \
376 ZFCallback::_ZFP_ZFCallbackCreateMemberMethod(obj, zfmethod)
377
383#define ZFCallbackForFunc(callbackRawFunction) \
384 ZFCallback::_ZFP_ZFCallbackCreateRawFunction( \
385 (ZFFuncAddrType)(void (*)(const ZFArgs &))(callbackRawFunction) \
386 )
387
388// ============================================================
389// child callback declare
390#define _ZFP_ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
391 zfclassLikePOD ZFLIB_ CallbackTypeName : zfextend ParentType { \
392 _ZFP_ZFCALLBACK_DECLARE_CONSTRUCTORS(CallbackTypeName, ParentType) \
393 public:
394#define _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
395 }; \
396 ZFOUTPUT_TYPE(CallbackTypeName, {v.objectInfoT(s);})
397#define _ZFP_ZFCALLBACK_DECLARE_CONSTRUCTORS(CallbackTypeName, ParentType) \
398 public: \
399 \
400 CallbackTypeName(void) \
401 : ParentType() \
402 { \
403 } \
404 CallbackTypeName(ZF_IN const zfnullT &dummy) \
405 : ParentType(dummy) \
406 { \
407 } \
408 CallbackTypeName(ZF_IN const ZFCallback &ref) \
409 : ParentType(ref) \
410 { \
411 } \
412 CallbackTypeName(ZF_IN const CallbackTypeName &ref) \
413 : ParentType((const ZFCallback &)ref) \
414 { \
415 } \
416 CallbackTypeName &operator = (ZF_IN const zfnullT &dummy) { \
417 ParentType::operator = (dummy); \
418 return *this; \
419 } \
420 CallbackTypeName &operator = (ZF_IN const ZFCallback &ref) { \
421 ParentType::operator = (ref); \
422 return *this; \
423 } \
424 CallbackTypeName &operator = (ZF_IN const CallbackTypeName &ref) { \
425 ParentType::operator = ((const ZFCallback &)ref); \
426 return *this; \
427 } \
428
432#define ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
433 _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
434 ZFTYPEID_ALIAS_DECLARE(ZFLIB_, ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName) \
435 ZFTYPEID_ALIAS_REG(ZFLIB_, ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName)
436#define _ZFP_ZFCALLBACK_DECLARE_END_NO_ALIAS(ZFLIB_, CallbackTypeName, ParentType) \
437 _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType)
438
458#define ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
459 _ZFP_ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType)
460
463#define ZFCALLBACK_DECLARE(ZFLIB_, CallbackTypeName, ParentType) \
464 ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
465 ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType)
466
470#define ZFCALLBACK_DEFINE(CallbackTypeName, ParentType) \
471 ZFTYPEID_ALIAS_DEFINE(ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName)
472
474#endif // #ifndef _ZFI_ZFCallback_h_
475
#define ZFCallbackForMethod(zfmethod)
create a callback from ZFMethod
Definition ZFCallback.h:365
ZFCallbackType
callback type of ZFCallback
Definition ZFCallback.h:19
@ ZFCallbackTypeLambda
lambda function that can capture other vars
Definition ZFCallback.h:24
@ ZFCallbackTypeDummy
dummy that must not be executed
Definition ZFCallback.h:20
@ ZFCallbackTypeMemberMethod
class memeber method that need a object instance to execute, described by ZFMethod
Definition ZFCallback.h:22
@ ZFCallbackTypeRawFunction
static function without ZFMethod
Definition ZFCallback.h:23
@ ZFCallbackTypeMethod
class static member method described by ZFMethod
Definition ZFCallback.h:21
#define ZFCOMPARER_DEFAULT_DECLARE(T_Comparable0, T_Comparable1, compareAction)
declare a template specialization for ZFComparerDefault
Definition ZFComparer.h:264
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
zfbool zfstringIsEqual(const zfchar *s1, const zfchar *s2)
util to compare two c strings, while null is regarded as equal to empty string
Definition ZFCoreStringUtil.h:148
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
#define zfnullT
type for zfnull, can be used for function overload
Definition ZFCoreTypeDef_CoreType.h:85
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
_zft_zfidentity zfidentity
identity type, ensured at least 32 bit, ensured unsigned
Definition ZFCoreTypeDef_CoreType.h:225
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
void(* ZFFuncAddrType)(void)
dummy function address type
Definition ZFCoreTypeDef_OtherType.h:277
ZFCompareResult
compare result of two ZFObjects
Definition ZFCoreTypeDef_OtherType.h:28
@ ZFCompareUncomparable
Definition ZFCoreTypeDef_OtherType.h:29
@ ZFCompareEqual
Definition ZFCoreTypeDef_OtherType.h:31
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
reflectable method definination for ZFFramework
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
zfcast declarations
#define ZFSerializeDisable
special string to explicit disable serialization for impl
Definition ZFObjectClassTypeFwd.h:194
serializable data for ZFSerializable
callback used by ZFFramework
Definition ZFCallback.h:96
void callbackTagRemove(const zfstring &key)
remove tag, same as set tag to null
Definition ZFCallback.h:218
ZFFuncAddrType callbackLambdaInvoker(void) const
impl for lambda type
zfany callbackTag(const zfstring &key) const
see callbackTag
void callbackId(const zfstring &callbackId)
an unique id for the callback, used for cache logic
void * callbackLambdaImpl(void) const
impl for lambda type
void callbackTagRemoveAll(void)
see callbackTag
zfstring objectInfo(void) const
return object info
Definition ZFCallback.h:143
void callbackSerializeData(const ZFSerializableData &customData)
see ZFTypeId_ZFCallback
void objectInfoT(zfstring &ret) const
see objectInfo
ZFCallbackType callbackType(void) const
get the type of callback
ZFPathInfo pathInfo(void) const
used for impl to store path related info, see ZFPathInfo
zfbool callbackSerializeDisable(void) const
see ZFTypeId_ZFCallback
Definition ZFCallback.h:336
ZFCompareResult objectCompare(const ZFCallback &ref) const
compare with another callback
zfauto callbackTagRemoveAndGet(const zfstring &key)
remove tag, return removed tag or null if not exist
void pathInfo(const ZFPathInfo &pathInfo)
see pathInfo
void callbackOwnerObjectRelease(void) const
manually release the owner, see callbackOwnerObjectRetain
void callbackOwnerObjectRetain(void) const
retain owner object and auto release it after callback's retain count reduced to 0
zfidentity callbackHash(void) const
hash of the callback
zfindex objectRetainCount(void) const
get current retain count
ZFCompareResult objectCompareByInstance(const ZFCallback &ref) const
compare callback by instance only (same callback contents not necessarily to be same instance)
Definition ZFCallback.h:162
ZFSerializableData callbackSerializeData(void) const
see ZFTypeId_ZFCallback
void callbackTag(const zfstring &key, ZFObject *tag)
retain and store a autoreleased object attached to this callback, or set null to remove
void callbackClear(void)
explicitly clear reference of this callback
void callbackSerializeDisable(zfbool disable)
see ZFTypeId_ZFCallback
Definition ZFCallback.h:330
const zfstring & callbackId(void) const
see callbackId
void callbackInfoCopy(const ZFCallback &src)
util method to copy callback info from another callback
Definition ZFCallback.h:292
void callbackSerializeType(const zfstring &customType)
see ZFTypeId_ZFCallback
const zfstring & callbackSerializeType(void) const
see ZFTypeId_ZFCallback
const ZFMethod * callbackMethod(void) const
get the method or null if not declared by ZFMethod
zfbool valid(void) const
return true if callback is valid
Definition ZFCallback.h:238
ZFFuncAddrType callbackRawFunction(void) const
get static function, valid only if type is ZFCallbackTypeRawFunction
zfany callbackOwnerObject(void) const
get the owner object, valid only if type is class member method
void callbackTagGetAllKeyValue(ZFCoreArray< zfstring > &allKey, ZFCoreArray< zfauto > &allValue) const
see callbackTag
light weight array
Definition ZFCoreArray.h:331
reflectable method for ZFObject
Definition ZFMethod.h:252
base class of all objects
Definition ZFObjectCore.h:209
path related info storage for impl, actual meaning of the info depends on impl
Definition ZFPathInfo.h:35
ZFSerializable's data container, see ZFSerializable.
Definition ZFSerializableData.h:74
util method to cast ZFObject types freely
Definition zfany.h:35
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34