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:
181
189 ZF_IN const ZFListener &observer
190 , ZF_IN_OPT ZFLevel observerLevel = ZFLevelAppNormal
191 ) const;
194
207 zffinal const zfstring &callbackId(void) const;
208
213
226 ZF_IN const zfstring &key
227 , ZF_IN ZFObject *tag
228 );
235 ) const;
239 inline void callbackTagRemove(ZF_IN const zfstring &key) {
240 this->callbackTag(key, zfnull);
241 }
242
255
259 zffinal inline zfbool valid(void) const {
260 return (this->callbackType() != ZFCallbackTypeDummy);
261 }
262
267
273
277 zffinal const ZFMethod *callbackMethod(void) const;
278
283
287 zffinal void *callbackLambdaImpl(void) const;
292 zffinal void _ZFP_ZFCallback_callbackLambdaInvoker(ZF_IN ZFFuncAddrType v) const;
293
307
308 // ============================================================
309public:
314 this->callbackId(src.callbackId());
315 if(src.callbackSerializeDisable()) {
317 }
318 else {
319 this->callbackSerializeType(src.callbackSerializeType());
320 this->callbackSerializeData(src.callbackSerializeData());
321 }
322 this->pathInfo(src.pathInfo());
323 }
324
325 // ============================================================
326 // callback serialize logic
327public:
347
354
360
361 // ============================================================
362 // local path logic
363public:
370
371private:
372 _ZFP_ZFCallbackPrivate *d;
373};
375 return v0 == v1 ? ZFCompareEqual : ZFCompareUncomparable;
376})
377
378// ============================================================
379// ZFCallback create
386#define ZFCallbackForMethod(zfmethod) \
387 ZFCallback::_ZFP_ZFCallbackCreateMethod(zfmethod)
388
389
396#define ZFCallbackForMemberMethod(obj, zfmethod) \
397 ZFCallback::_ZFP_ZFCallbackCreateMemberMethod(obj, zfmethod)
398
404#define ZFCallbackForFunc(callbackRawFunction) \
405 ZFCallback::_ZFP_ZFCallbackCreateRawFunction( \
406 (ZFFuncAddrType)(void (*)(const ZFArgs &))(callbackRawFunction) \
407 )
408
409// ============================================================
410// child callback declare
411#define _ZFP_ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
412 zfclassLikePOD ZFLIB_ CallbackTypeName : zfextend ParentType { \
413 _ZFP_ZFCALLBACK_DECLARE_CONSTRUCTORS(CallbackTypeName, ParentType) \
414 public:
415#define _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
416 }; \
417 ZFOUTPUT_TYPE(CallbackTypeName, {v.objectInfoT(s);})
418#define _ZFP_ZFCALLBACK_DECLARE_CONSTRUCTORS(CallbackTypeName, ParentType) \
419 public: \
420 \
421 CallbackTypeName(void) \
422 : ParentType() \
423 { \
424 } \
425 CallbackTypeName(ZF_IN const zfnullT &dummy) \
426 : ParentType(dummy) \
427 { \
428 } \
429 CallbackTypeName(ZF_IN const ZFCallback &ref) \
430 : ParentType(ref) \
431 { \
432 } \
433 CallbackTypeName(ZF_IN const CallbackTypeName &ref) \
434 : ParentType((const ZFCallback &)ref) \
435 { \
436 } \
437 CallbackTypeName &operator = (ZF_IN const zfnullT &dummy) { \
438 ParentType::operator = (dummy); \
439 return *this; \
440 } \
441 CallbackTypeName &operator = (ZF_IN const ZFCallback &ref) { \
442 ParentType::operator = (ref); \
443 return *this; \
444 } \
445 CallbackTypeName &operator = (ZF_IN const CallbackTypeName &ref) { \
446 ParentType::operator = ((const ZFCallback &)ref); \
447 return *this; \
448 } \
449
453#define ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
454 _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType) \
455 ZFTYPEID_ALIAS_DECLARE(ZFLIB_, ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName) \
456 ZFTYPEID_ALIAS_REG(ZFLIB_, ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName)
457#define _ZFP_ZFCALLBACK_DECLARE_END_NO_ALIAS(ZFLIB_, CallbackTypeName, ParentType) \
458 _ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType)
459
479#define ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
480 _ZFP_ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType)
481
484#define ZFCALLBACK_DECLARE(ZFLIB_, CallbackTypeName, ParentType) \
485 ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType) \
486 ZFCALLBACK_DECLARE_END(ZFLIB_, CallbackTypeName, ParentType)
487
491#define ZFCALLBACK_DEFINE(CallbackTypeName, ParentType) \
492 ZFTYPEID_ALIAS_DEFINE(ZFCallback, ZFCallback, CallbackTypeName, CallbackTypeName)
493
495#endif // #ifndef _ZFI_ZFCallback_h_
496
#define ZFCallbackForMethod(zfmethod)
create a callback from ZFMethod
Definition ZFCallback.h:386
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:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
#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
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:194
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:239
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:357
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: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:351
const zfstring & callbackId(void) const
see callbackId
void callbackInfoCopy(const ZFCallback &src)
util method to copy callback info from another callback
Definition ZFCallback.h:313
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:259
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:331
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:205
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