ZFFramework
 
Loading...
Searching...
No Matches
ZFObjectObserver.h File Reference

observer types for ZFObject More...

#include "ZFCallback.h"
#include "ZFIdMap.h"
#include "ZFArgs.h"
#include "ZFListenerDeclare.h"

Go to the source code of this file.

Classes

class  ZFListener
 listener as ZFCallback, mostly used by ZFObject::observerNotify More...
 
class  ZFObserver
 holder object for observer logic, see ZFObject::observerNotify More...
 

Namespaces

namespace  ZFGlobalEvent
 

 

Macros

#define ZFEVENT(YourEvent)
 see ZFObject::observerNotify
 
#define ZFEVENT_INLINE(YourEvent)
 see ZFEVENT
 
#define ZFEVENT_REGISTER(YourClass, YourEvent)
 see ZFEVENT
 
#define ZFEVENT_GLOBAL(ZFLIB_, YourEvent)
 declare a observer event in global scope, see ZFEVENT
 
#define ZFEVENT_GLOBAL_REGISTER(YourEvent)
 see ZFEVENT_GLOBAL
 

Functions

ZFObserverZFGlobalObserver (void)
 all event notified by ZFObject::observerNotify would also be notified to the observers added by this method, see ZFObject::observerNotify
 
zfstring ZFEventNameForId (zfidentity idValue)
 get id name from id value, or null if no such id, see ZFEventIdForName
 
zfidentity ZFEventIdForName (const zfstring &idName)
 get id value from id name, or zfidentityInvalid if no such id name
 
zfidentity ZFEventDynamicRegister (const zfstring &idName)
 dynamically register your own id
 
void ZFEventDynamicUnregister (zfidentity idValue)
 unregister id that was registered by ZFEventDynamicRegister
 

Detailed Description

observer types for ZFObject

Macro Definition Documentation

◆ ZFEVENT

#define ZFEVENT ( YourEvent)

see ZFObject::observerNotify

usage:

zfclass YourClass ... {
ZFEVENT(YourEvent)
};
ZFEVENT_REGISTER(YourClass, YourEvent)
#define zfclass
same as class, shows that this class is a ZFObject type
Definition ZFObjectClassTypeFwd.h:38
#define ZFEVENT(YourEvent)
see ZFObject::observerNotify
Definition ZFObjectObserver.h:328
#define ZFEVENT_REGISTER(YourClass, YourEvent)
see ZFEVENT
Definition ZFObjectObserver.h:336

declare a event for ZFObject's observer logic, declared event name can be accessed by:

zfidentity eventId = YourClass::E_YourEvent();
zfstring eventName = ZFEventNameForId(eventId);
_zft_zfidentity zfidentity
identity type, ensured at least 32 bit, ensured unsigned
Definition ZFCoreTypeDef_CoreType.h:225
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
zfstring ZFEventNameForId(zfidentity idValue)
get id name from id value, or null if no such id, see ZFEventIdForName
Definition ZFObjectObserver.h:380

note that subclass may declare a event same as parent, while the final event name is different:
ParentClass::E_YourEvent() => "ParentClass.E_YourEvent"
ChildClass::E_YourEvent() => "ChildClass.E_YourEvent"

Note
we declare the event id as int types for performance, it's ensured each event has different event id, but it's only ensured while app is running, after relaunching the app, the event id is not ensured the same, you should use the name of the event to store or pass between apps, and you can use ZFEventIdForName or ZFEventNameForId to convert them easily
See also
ZFEVENT_GLOBAL

◆ ZFEVENT_GLOBAL

#define ZFEVENT_GLOBAL ( ZFLIB_,
YourEvent )

declare a observer event in global scope, see ZFEVENT

usage:

// in header files
ZF_NAMESPACE_BEGIN(YourNamespace)
/ ** @brief you can add doxygen docs here * /
ZF_NAMESPACE_END(YourNamespace)
ZFEVENT_GLOBAL_REGISTER(YourNamespace, YourEvent)
// use the event
zfidentity eventId = YourNamespace::E_YourEvent();
#define ZFLIB_APP
used to export symbols
Definition ZFCoreEnvDef.h:35
#define ZF_NAMESPACE_END(NameSpace)
end namespace
Definition ZFNamespace.h:60
#define ZF_NAMESPACE_BEGIN(NameSpace)
begin namespace
Definition ZFNamespace.h:40
#define ZFEVENT_GLOBAL(ZFLIB_, YourEvent)
declare a observer event in global scope, see ZFEVENT
Definition ZFObjectObserver.h:369
#define ZFEVENT_GLOBAL_REGISTER(YourEvent)
see ZFEVENT_GLOBAL
Definition ZFObjectObserver.h:373

unlike ZFEVENT, this macro would declare event outside of class scope, typically you should use ZFEVENT_GLOBAL which have "ZFGlobalEvent" as namespace

once declared, use ZFGlobalObserver to notify or add observer:

ZFGlobalObserver().observerAdd(YourNamespace::E_YourEvent(), xxx);
...
ZFGlobalObserver().observerNotify(YourNamespace::E_YourEvent(), xxx);
ZFObserver & ZFGlobalObserver(void)
all event notified by ZFObject::observerNotify would also be notified to the observers added by this ...
void observerAdd(zfidentity eventId, const ZFListener &observer, ZFLevel observerLevel=ZFLevelAppNormal)
see ZFObject::observerNotify
See also
ZFEVENT
ZFGlobalObserver

Function Documentation

◆ ZFGlobalObserver()

ZFObserver & ZFGlobalObserver ( void )
extern

all event notified by ZFObject::observerNotify would also be notified to the observers added by this method, see ZFObject::observerNotify

use only if necessary, which may cause performance issue

◆ ZFEventIdForName()

zfidentity ZFEventIdForName ( const zfstring & idName)
inline

get id value from id name, or zfidentityInvalid if no such id name

the id name should looks like YourClass.E_YourIdName or YourNamespace.E_YourIdName

note: can be found only if:

◆ ZFEventDynamicRegister()

zfidentity ZFEventDynamicRegister ( const zfstring & idName)
inline

dynamically register your own id

assert fail if already registered

◆ ZFEventDynamicUnregister()

void ZFEventDynamicUnregister ( zfidentity idValue)
inline

unregister id that was registered by ZFEventDynamicRegister

do nothing if no such id, assert fail if the id is not dynamically registered