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 "zfcast.h"
11#include "ZFSerializableData.h"
12
14
16
17// ============================================================
28
29#define ZFTOKEN_ZFCallbackTypeDummy "Dummy"
31#define ZFTOKEN_ZFCallbackTypeMethod "Method"
33#define ZFTOKEN_ZFCallbackTypeMemberMethod "MemeberMethod"
35#define ZFTOKEN_ZFCallbackTypeRawFunction "RawFunction"
37#define ZFTOKEN_ZFCallbackTypeLambda "Lambda"
38
39// ============================================================
40// callback invoker
41extern ZFLIB_ZFCore void _ZFP_ZFCallback_executeNullCallback(void);
45#define _ZFP_ZFCALLBACK_INVOKER(N) \
46 \
47 template<typename T_ReturnType ZFM_REPEAT(N, ZFM_REPEAT_TEMPLATE, ZFM_COMMA, ZFM_COMMA)> \
48 T_ReturnType executeExact(ZFM_REPEAT(N, ZFM_REPEAT_PARAM, ZFM_EMPTY, ZFM_COMMA)) const { \
49 switch(this->callbackType()) { \
50 case ZFCallbackTypeMethod: \
51 case ZFCallbackTypeMemberMethod: \
52 return this->callbackMethod()->executeExact<T_ReturnType ZFM_REPEAT(N, ZFM_REPEAT_TYPE, ZFM_COMMA, ZFM_COMMA)>( \
53 this->callbackOwnerObject() \
54 ZFM_REPEAT(N, ZFM_REPEAT_NAME, ZFM_COMMA, ZFM_COMMA) \
55 ); \
56 case ZFCallbackTypeRawFunction: \
57 return ((T_ReturnType (*)(ZFM_REPEAT(N, ZFM_REPEAT_TYPE, ZFM_EMPTY, ZFM_COMMA)))(this->callbackRawFunction())) \
58 (ZFM_REPEAT(N, ZFM_REPEAT_NAME, ZFM_EMPTY, ZFM_COMMA)); \
59 case ZFCallbackTypeLambda: { \
60 ZFCallback holder_(*this); \
61 return ((T_ReturnType (*)(void * ZFM_REPEAT(N, ZFM_REPEAT_TYPE, ZFM_COMMA, ZFM_COMMA)))(this->callbackLambdaInvoker()))( \
62 this->callbackLambdaImpl() \
63 ZFM_REPEAT(N, ZFM_REPEAT_NAME, ZFM_COMMA, ZFM_COMMA) \
64 ); \
65 } \
66 case ZFCallbackTypeDummy: \
67 default: \
68 _ZFP_ZFCallback_executeNullCallback(); \
69 return ((T_ReturnType (*)(void))(zfnull)) (); \
70 } \
71 }
72
73typedef void (*_ZFP_ZFCallbackLambdaDeleteCallback)(void *);
74
75// ============================================================
76// ZFCallback
78zfclassFwd _ZFP_ZFCallbackPrivate;
99public:
101 ZFCallback(void);
102 ZFCallback(ZF_IN const zfnullT &dummy);
103 ZFCallback(ZF_IN const ZFCallback &ref);
104 ZFCallback &operator = (ZF_IN const ZFCallback &ref);
105 ZFCallback &operator = (ZF_IN const zfnullT &dummy);
106 ~ZFCallback(void);
107 zfbool operator == (ZF_IN const ZFCallback &ref) const {return (this->objectCompare(ref) == ZFCompareEqual);}
108 zfbool operator != (ZF_IN const ZFCallback &ref) const {return (this->objectCompare(ref) != ZFCompareEqual);}
109 zfbool operator == (ZF_IN const zfnullT &dummy) const {return (d == zfnull);}
110 zfbool operator != (ZF_IN const zfnullT &dummy) const {return (d != zfnull);}
111 operator bool (void) const {return this->valid();}
112 inline void _ZFP_ZFCallback_d(ZF_IN _ZFP_ZFCallbackPrivate *d) {
113 this->d = d;
114 }
115 inline _ZFP_ZFCallbackPrivate *_ZFP_ZFCallback_d(void) const {
116 return d;
117 }
118 static ZFCallback _ZFP_ZFCallbackCreateMethod(ZF_IN const ZFMethod *callbackMethod);
119 static ZFCallback _ZFP_ZFCallbackCreateMemberMethod(
122 );
123 static ZFCallback _ZFP_ZFCallbackCreateRawFunction(ZF_IN ZFFuncAddrType callbackRawFunction);
124 static ZFCallback _ZFP_ZFCallbackCreateLambda(
126 , ZF_IN _ZFP_ZFCallbackLambdaDeleteCallback callbackLambdaImplDestroy
128 );
130
131 _ZFP_ZFCALLBACK_INVOKER(0)
132 _ZFP_ZFCALLBACK_INVOKER(1)
133 _ZFP_ZFCALLBACK_INVOKER(2)
134 _ZFP_ZFCALLBACK_INVOKER(3)
135 _ZFP_ZFCALLBACK_INVOKER(4)
136 _ZFP_ZFCALLBACK_INVOKER(5)
137 _ZFP_ZFCALLBACK_INVOKER(6)
138 _ZFP_ZFCALLBACK_INVOKER(7)
139 _ZFP_ZFCALLBACK_INVOKER(8)
140 /* ZFMETHOD_MAX_PARAM */
141
142public:
147
151 zffinal inline zfstring objectInfo(void) const {
152 zfstring ret;
153 this->objectInfoT(ret);
154 return ret;
155 }
156
173
174public:
182
190
198 ZF_IN const ZFListener &observer
199 , ZF_IN_OPT ZFLevel observerLevel = ZFLevelAppNormal
200 ) const;
203
216 zffinal const zfstring &callbackId(void) const;
217
222
235 ZF_IN const zfstring &key
236 , ZF_IN ZFObject *tag
237 );
244 ) const;
248 inline void callbackTagRemove(ZF_IN const zfstring &key) {
249 this->callbackTag(key, zfnull);
250 }
251
264
268 zffinal inline zfbool valid(void) const {
269 return (this->callbackType() != ZFCallbackTypeDummy);
270 }
271
276
282
286 zffinal const ZFMethod *callbackMethod(void) const;
287
292
296 zffinal void *callbackLambdaImpl(void) const;
301 zffinal void _ZFP_ZFCallback_callbackLambdaInvoker(ZF_IN ZFFuncAddrType v) const;
302
316
317 // ============================================================
318public:
323 this->callbackId(src.callbackId());
324 if(src.callbackSerializeDisable()) {
326 }
327 else {
328 this->callbackSerializeType(src.callbackSerializeType());
329 this->callbackSerializeData(src.callbackSerializeData());
330 }
331 this->pathInfo(src.pathInfo());
332 }
333
334 // ============================================================
335 // callback serialize logic
336public:
356
363
369
370 // ============================================================
371 // local path logic
372public:
379
380private:
381 _ZFP_ZFCallbackPrivate *d;
382};
384 return v0 == v1 ? ZFCompareEqual : ZFCompareUncomparable;
385})
386
387// ============================================================
388// ZFCallback create
395#define ZFCallbackForMethod(zfmethod) \
396 ZFCallback::_ZFP_ZFCallbackCreateMethod(zfmethod)
397
398
405#define ZFCallbackForMemberMethod(obj, zfmethod) \
406 ZFCallback::_ZFP_ZFCallbackCreateMemberMethod(obj, zfmethod)
407
413#define ZFCallbackForFunc(callbackRawFunction) \
414 ZFCallback::_ZFP_ZFCallbackCreateRawFunction( \
415 (ZFFuncAddrType)(void (*)(const ZFArgs &))(callbackRawFunction) \
416 )
417
418// ============================================================
419// child callback declare
420#define _ZFP_ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
421 zfclassLikePOD ZFLIB_ CallbackTypeName : zfextend ParentType { \
422 _ZFP_ZFCALLBACK_DECLARE_CONSTRUCTORS(CallbackTypeName, ParentType) \
423 public:
424#define _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
425 }; \
426 ZFOUTPUT_TYPE(CallbackTypeName, {v.objectInfoT(s);})
427#define _ZFP_ZFCALLBACK_DECLARE_CONSTRUCTORS(CallbackTypeName, ParentType) \
428 public: \
429 \
430 CallbackTypeName(void) \
431 : ParentType() \
432 { \
433 } \
434 CallbackTypeName(ZF_IN const zfnullT &dummy) \
435 : ParentType(dummy) \
436 { \
437 } \
438 CallbackTypeName(ZF_IN const ZFCallback &ref) \
439 : ParentType(ref) \
440 { \
441 } \
442 CallbackTypeName(ZF_IN const CallbackTypeName &ref) \
443 : ParentType((const ZFCallback &)ref) \
444 { \
445 } \
446 CallbackTypeName &operator = (ZF_IN const zfnullT &dummy) { \
447 ParentType::operator = (dummy); \
448 return *this; \
449 } \
450 CallbackTypeName &operator = (ZF_IN const ZFCallback &ref) { \
451 ParentType::operator = (ref); \
452 return *this; \
453 } \
454 CallbackTypeName &operator = (ZF_IN const CallbackTypeName &ref) { \
455 ParentType::operator = ((const ZFCallback &)ref); \
456 return *this; \
457 } \
458
462#define ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
463 _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
464 _ZFP_ZFCALLBACK_DECLARE_END_REG(ZFLIB_, CallbackTypeName, ParentType)
465#define _ZFP_ZFCALLBACK_DECLARE_END_REG(ZFLIB_, CallbackTypeName, ParentType) \
466 ZFTYPEID_ALIAS_DECLARE(ZFLIB_, ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName) \
467 ZFTYPEID_ALIAS_REG_CUSTOM(ZFLIB_, ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName, _ZFP_ZFTYPEID_ALIAS_VALUE_ACCESS_REINTERPRET_CAST)
468
488#define ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
489 _ZFP_ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType)
490
493#define ZFCALLBACK_DECLARE(ZFLIB_, CallbackTypeName, ParentType) \
494 ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
495 ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType)
496
500#define ZFCALLBACK_DEFINE(CallbackTypeName, ParentType) \
501 ZFTYPEID_ALIAS_DEFINE(ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName)
502
504#endif // #ifndef _ZFI_ZFCallback_h_
505
#define ZFCallbackForMethod(zfmethod)
create a callback from ZFMethod
Definition ZFCallback.h:395
ZFCallbackType
callback type of ZFCallback
Definition ZFCallback.h:21
@ ZFCallbackTypeLambda
lambda function that can capture other vars
Definition ZFCallback.h:26
@ ZFCallbackTypeDummy
dummy that must not be executed
Definition ZFCallback.h:22
@ ZFCallbackTypeMemberMethod
class memeber method that need a object instance to execute, described by ZFMethod
Definition ZFCallback.h:24
@ ZFCallbackTypeRawFunction
static function without ZFMethod
Definition ZFCallback.h:25
@ ZFCallbackTypeMethod
class static member method described by ZFMethod
Definition ZFCallback.h:23
#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:184
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:188
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:200
#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
ZFLevel
level for logic which depends on order
Definition ZFLevel.h:17
@ ZFLevelAppNormal
app
Definition ZFLevel.h:26
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
#define ZFSerializeDisable
special string to explicit disable serialization for impl
Definition ZFObjectClassTypeFwd.h:195
serializable data for ZFSerializable
callback used by ZFFramework
Definition ZFCallback.h:98
void callbackTagRemove(const zfstring &key)
remove tag, same as set tag to null
Definition ZFCallback.h:248
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:151
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:366
ZFCompareResult objectCompare(const ZFCallback &ref) const
compare with another callback
void callbackRelease(void)
explicitly clear reference of this 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:170
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 all reference of this callback
void callbackSerializeDisable(zfbool disable)
see ZFTypeId_ZFCallback
Definition ZFCallback.h:360
const zfstring & callbackId(void) const
see callbackId
void callbackInfoCopy(const ZFCallback &src)
util method to copy callback info from another callback
Definition ZFCallback.h:322
void callbackClearPrepareRemove(const ZFListener &observer) const
see callbackClear
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:268
ZFFuncAddrType callbackRawFunction(void) const
get static function, valid only if type is ZFCallbackTypeRawFunction
void callbackClearPrepareAdd(const ZFListener &observer, ZFLevel observerLevel=ZFLevelAppNormal) const
see callbackClear
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:342
listener as ZFCallback, mostly used by ZFObject::observerNotify
Definition ZFObjectObserver.h:30
reflectable method for ZFObject
Definition ZFMethod.h:252
base class of all objects
Definition ZFObjectCore.h:196
path related info storage for impl, actual meaning of the info depends on impl
Definition ZFPathInfo.h:34
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
zfcast declarations