ZFFramework
Loading...
Searching...
No Matches
ZFObjectObserver.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFObjectObserver_h_
7#define _ZFI_ZFObjectObserver_h_
8
9#include "ZFCallback.h"
10#include "ZFIdMap.h"
11#include "ZFArgs.h"
12#include "ZFListenerDeclare.h"
13
14#if ZF_ENV_LAMBDA
15#include <functional>
16#endif
17
19
20// ============================================================
31public:
33 inline zfbool execute(void) const {
34 if(this->valid()) {
35 ZFArgs zfargs;
37 return zftrue;
38 }
39 else {
40 return zffalse;
41 }
42 }
43
44 inline zfbool execute(ZF_IN const ZFArgs &zfargs) const {
45 if(this->valid()) {
47 return zftrue;
48 }
49 else {
50 return zffalse;
51 }
52 }
53
54 inline zfbool operator () (void) const {
55 if(this->valid()) {
56 ZFArgs zfargs;
58 return zftrue;
59 }
60 else {
61 return zffalse;
62 }
63 }
64
65 inline zfbool operator () (ZF_IN const ZFArgs &zfargs) const {
66 if(this->valid()) {
68 return zftrue;
69 }
70 else {
71 return zffalse;
72 }
73 }
74
76public:
77 typedef void (*FUNC_TYPE)(ZF_IN const ZFArgs &zfargs);
78 ZFListener(ZF_IN FUNC_TYPE f) {
79 FUNC_TYPE fTmp = f;
80 ZFLISTENER_1(wrapper
81 , FUNC_TYPE, fTmp
82 ) {
83 fTmp(zfargs);
85 ZFCallback::operator = ((const ZFCallback &)wrapper);
86 }
87 ZFListener &operator = (ZF_IN FUNC_TYPE f) {
88 FUNC_TYPE fTmp = f;
89 ZFLISTENER_1(wrapper
90 , FUNC_TYPE, fTmp
91 ) {
92 fTmp(zfargs);
94 ZFCallback::operator = ((const ZFCallback &)wrapper);
95 return *this;
96 }
98
100#if ZF_ENV_LAMBDA
101public:
102 ZFListener(ZF_IN std::function<void(const ZFArgs &)> const &f) {
103 ZFLISTENER_1(wrapper
104 , std::function<void(const ZFArgs &)>, f
105 ) {
106 f(zfargs);
108 ZFCallback::operator = ((const ZFCallback &)wrapper);
109 }
110 ZFListener &operator = (ZF_IN std::function<void(const ZFArgs &)> const &f) {
111 ZFLISTENER_1(wrapper
112 , std::function<void(const ZFArgs &)>, f
113 ) {
114 f(zfargs);
116 ZFCallback::operator = ((const ZFCallback &)wrapper);
117 return *this;
118 }
119#endif
121_ZFP_ZFCALLBACK_DECLARE_END(ZFLIB_ZFCore, ZFListener, ZFCallback)
122
123// ============================================================
124// ZFObserver
125zfclassFwd _ZFP_ZFObserverPrivate;
130public:
132 ZFObserver(void);
133 ZFObserver(ZF_IN ZFObserver const &ref);
134 ~ZFObserver(void);
135 ZFObserver &operator = (ZF_IN ZFObserver const &ref);
136 zfbool operator == (ZF_IN ZFObserver const &ref) const;
137 inline zfbool operator != (ZF_IN ZFObserver const &ref) const {return !(this->operator == (ref));}
139
140public:
143 ZF_IN zfidentity eventId
144 , ZF_IN const ZFListener &observer
145 , ZF_IN_OPT ZFLevel observerLevel = ZFLevelAppNormal
146 );
149 ZF_IN zfidentity eventId
150 , ZF_IN const ZFListener &observer
151 , ZF_IN_OPT ZFLevel observerLevel = ZFLevelAppNormal
152 );
155 ZF_IN zfidentity eventId
156 , ZF_IN const ZFListener &callback
157 );
168 ZF_IN zfidentity eventId
169 , ZF_IN_OPT const ZFArgs &zfargs = ZFArgs()
170 ) const {
171 this->observerNotifyWithSender(this->observerOwner(), eventId, zfargs);
172 }
173
175 ZF_IN ZFObject *customSender
176 , ZF_IN zfidentity eventId
177 , ZF_IN_OPT const ZFArgs &zfargs = ZFArgs()
178 ) const;
181 ZF_IN zfidentity eventId
182 , ZF_IN_OPT const ZFArgs &zfargs = ZFArgs()
183 ) const {
184 this->observerNotifyReverselyWithSender(this->observerOwner(), eventId, zfargs);
185 }
186
188 ZF_IN ZFObject *customSender
189 , ZF_IN zfidentity eventId
190 , ZF_IN_OPT const ZFArgs &zfargs = ZFArgs()
191 ) const;
192
193public:
196 ZF_IN const zfstring &eventName
197 , ZF_IN const ZFListener &observer
198 , ZF_IN_OPT ZFLevel observerLevel = ZFLevelAppNormal
199 );
201 zffinal inline void on(
202 ZF_IN zfidentity eventId
203 , ZF_IN const ZFListener &observer
204 , ZF_IN_OPT ZFLevel observerLevel = ZFLevelAppNormal
205 ) {
206 this->observerAdd(eventId, observer, observerLevel);
207 }
208
209public:
234 ZF_IN zfidentity eventId
235 , ZF_IN_OUT zfuint *flag
236 , ZF_IN_OUT zfuint flagBit
237 );
242 ZF_IN zfidentity eventId
243 , ZF_IN_OUT zfuint *flag
244 , ZF_IN_OUT zfuint flagBit
245 );
246
247public:
249 void objectInfoT(ZF_OUT zfstring &ret) const;
253 zfstring objectInfo(void) const {
254 zfstring ret;
255 this->objectInfoT(ret);
256 return ret;
257 }
258
259public:
262 zffinal void _ZFP_ZFObserver_observerOwner(ZF_IN ZFObject *obj);
263 zffinal void _ZFP_ZFObserver_ZFImplementDynamicAttach(ZF_IN ZFObject *holder);
264
265private:
266 _ZFP_ZFObserverPrivate *d;
267};
268
269// ============================================================
278
279// ============================================================
280// observer
313#define ZFEVENT(YourEvent) \
314 ZFIDMAP_DETAIL(E_, YourEvent)
315
317#define ZFEVENT_INLINE(YourEvent) \
318 ZFIDMAP_DETAIL_INLINE(E_, YourEvent)
319
321#define ZFEVENT_REGISTER(YourClass, YourEvent) \
322 ZFIDMAP_REGISTER_DETAIL(YourClass, E_, YourEvent)
323
324// ============================================================
354#define ZFEVENT_GLOBAL(ZFLIB_, YourEvent) \
355 ZFIDMAP_GLOBAL_DETAIL(ZFLIB_, E_, YourEvent)
356
358#define ZFEVENT_GLOBAL_REGISTER(YourEvent) \
359 ZFIDMAP_GLOBAL_REGISTER_DETAIL(E_, YourEvent)
360
361// ============================================================
366 return ZFIdMapNameForId(idValue);
367}
368
379 return ZFIdMapIdForName(idName);
380}
381
388 return ZFIdMapDynamicRegister(idName);
389}
390
398}
399
402
403// ============================================================
418 ZF_IN const zfstring &eventName
419 , ZF_IN const ZFClass *cls
420 );
421
422// ============================================================
431 , ZF_IN_OUT ZFCoreArray<zfstring> &idNames
432 , ZF_IN_OPT const zfstring &ns = zfnull
433 , ZF_IN_OPT zfbool ignoreParent = zftrue
434 );
438 , ZF_IN_OPT const zfstring &ns = zfnull
439 , ZF_IN_OPT zfbool ignoreParent = zftrue
440 );
443 ZF_IN_OPT const zfstring &ns = zfnull
444 , ZF_IN_OPT zfbool ignoreParent = zftrue
445 ) {
447 ZFEventGetAllIdT(ret, ns, ignoreParent);
448 return ret;
449}
450
453 , ZF_IN_OPT const zfstring &ns = zfnull
454 , ZF_IN_OPT zfbool ignoreParent = zftrue
455 );
458 ZF_IN_OPT const zfstring &ns = zfnull
459 , ZF_IN_OPT zfbool ignoreParent = zftrue
460 ) {
462 ZFEventGetAllNameT(ret, ns, ignoreParent);
463 return ret;
464}
465
467#endif // #ifndef _ZFI_ZFObjectObserver_h_
468
params for ZFListener
header for ZFCallback
#define ZFCALLBACK_DECLARE_BEGIN(ZFLIB_, CallbackTypeName, ParentType)
util macro to declare a child type of ZFCallback
Definition ZFCallback.h:488
#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 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:198
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:202
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:206
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:214
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
#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 zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
id to string map util, useful to define extendable string enum
zfidentity ZFIdMapIdForName(const zfstring &idName)
get id value from id name, or zfidentityInvalid if no such id name, see ZFIDMAP
zfidentity ZFIdMapDynamicRegister(const zfstring &idName)
dynamically register your own id
void ZFIdMapDynamicUnregister(zfidentity idValue)
unregister id that was registered by ZFIdMapDynamicRegister
zfstring ZFIdMapNameForId(zfidentity idValue)
get id name from id value, or null if no such id, see ZFIdMapIdForName
ZFLevel
level for logic which depends on order
Definition ZFLevel.h:17
@ ZFLevelAppNormal
app
Definition ZFLevel.h:26
util macros to declare ZFListener as ZFCallback
#define ZFLISTENER_END()
see ZFLISTENER
Definition ZFListenerDeclare.h:17
#define ZFLISTENER_1(name, CaptureParam0, capture0)
see ZFLISTENER
Definition ZFListenerDeclare.h:69
#define ZF_NAMESPACE_BEGIN_REGISTER(NameSpace, ParentNameSpace)
begin and register namespace
Definition ZFNamespace.h:48
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_END(NameSpace)
end namespace
Definition ZFNamespace.h:60
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define ZF_NAMESPACE_GLOBAL
namespace of ZFFramework
Definition ZFNamespace.h:96
zfidentity ZFEventIdForName(const zfstring &idName)
get id value from id name, or zfidentityInvalid if no such id name
Definition ZFObjectObserver.h:378
zfidentity ZFEventIdForEventName(const zfstring &eventName, const ZFClass *cls)
recursively find evnet id by simple event name
void ZFEventDynamicUnregister(zfidentity idValue)
unregister id that was registered by ZFEventDynamicRegister
Definition ZFObjectObserver.h:396
void ZFEventGetAllNameT(ZFCoreArray< zfstring > &idNames, const zfstring &ns=zft_zfnull, zfbool ignoreParent=_ZFT_t_zftrue)
see ZFEventGetAll
ZFCoreArray< zfidentity > ZFEventGetAllId(const zfstring &ns=zft_zfnull, zfbool ignoreParent=_ZFT_t_zftrue)
see ZFEventGetAll
Definition ZFObjectObserver.h:442
zfstring ZFEventNameForId(zfidentity idValue)
get id name from id value, or null if no such id, see ZFEventIdForName
Definition ZFObjectObserver.h:365
void ZFEventGetAllIdT(ZFCoreArray< zfidentity > &idValues, const zfstring &ns=zft_zfnull, zfbool ignoreParent=_ZFT_t_zftrue)
see ZFEventGetAll
zfidentity ZFEventDynamicRegister(const zfstring &idName)
dynamically register your own id
Definition ZFObjectObserver.h:387
ZFCoreArray< zfstring > ZFEventGetAllName(const zfstring &ns=zft_zfnull, zfbool ignoreParent=_ZFT_t_zftrue)
see ZFEventGetAll
Definition ZFObjectObserver.h:457
void ZFEventGetAll(ZFCoreArray< zfidentity > &idValues, ZFCoreArray< zfstring > &idNames, const zfstring &ns=zft_zfnull, zfbool ignoreParent=_ZFT_t_zftrue)
see ZFEVENT
ZFObserver & ZFGlobalObserver(void)
all event notified by ZFObject::observerNotify would also be notified to the observers added by this ...
listener data used by ZFListener
Definition ZFArgs.h:20
ZFArgs(void)
main constructor
callback used by ZFFramework
Definition ZFCallback.h:98
T_ReturnType executeExact() const
see ZFCallback, you must assign the exact return type and param types for safe
Definition ZFCallback.h:131
zfbool valid(void) const
return true if callback is valid
Definition ZFCallback.h:268
ZFObject's class info.
Definition ZFClass.h:63
light weight array
Definition ZFCoreArray.h:297
listener as ZFCallback, mostly used by ZFObject::observerNotify
Definition ZFObjectObserver.h:30
zfbool execute(void) const
see ZFListener
Definition ZFObjectObserver.h:33
zfbool execute(const ZFArgs &zfargs) const
see ZFListener
Definition ZFObjectObserver.h:44
base class of all objects
Definition ZFObjectCore.h:195
holder object for observer logic, see ZFObject::observerNotify
Definition ZFObjectObserver.h:129
void observerAddForOnce(zfidentity eventId, const ZFListener &observer, ZFLevel observerLevel=ZFLevelAppNormal)
see ZFObject::observerNotify
void on(zfidentity eventId, const ZFListener &observer, ZFLevel observerLevel=ZFLevelAppNormal)
util to observerAdd
Definition ZFObjectObserver.h:201
void observerHasAddStateDetach(zfidentity eventId, zfuint *flag, zfuint flagBit)
see observerHasAddStateAttach
void observerNotify(zfidentity eventId, const ZFArgs &zfargs=ZFArgs()) const
see ZFObject::observerNotify
Definition ZFObjectObserver.h:167
void observerNotifyReversely(zfidentity eventId, const ZFArgs &zfargs=ZFArgs()) const
see ZFObject::observerNotify
Definition ZFObjectObserver.h:180
void observerNotifyWithSender(ZFObject *customSender, zfidentity eventId, const ZFArgs &zfargs=ZFArgs()) const
see ZFObject::observerNotify
void observerRemove(zfidentity eventId, const ZFListener &callback)
see ZFObject::observerNotify
void observerRemoveAll(void)
see ZFObject::observerNotify
void objectInfoT(zfstring &ret) const
see objectInfo
void on(const zfstring &eventName, const ZFListener &observer, ZFLevel observerLevel=ZFLevelAppNormal)
util to observerAdd
zfany observerOwner(void) const
owner object of this observer holder, or null if none
zfstring objectInfo(void) const
return a short string describe the object
Definition ZFObjectObserver.h:253
void observerRemoveAll(zfidentity eventId)
see ZFObject::observerNotify
void observerAdd(zfidentity eventId, const ZFListener &observer, ZFLevel observerLevel=ZFLevelAppNormal)
see ZFObject::observerNotify
void observerNotifyReverselyWithSender(ZFObject *customSender, zfidentity eventId, const ZFArgs &zfargs=ZFArgs()) const
see ZFObject::observerNotify
zfbool observerHasAdd(void) const
see ZFObject::observerNotify
void observerHasAddStateAttach(zfidentity eventId, zfuint *flag, zfuint flagBit)
attach a state that indicate whether the observer has add, for performance usage
zfbool observerHasAdd(zfidentity eventId) const
see ZFObject::observerNotify
util method to cast ZFObject types freely
Definition zfany.h:35
Definition ZFApp.h:13