1. Updatet the API documentation and parameter name from "object" to "sender" to reflect it's true purpose.
2. Fixed addObserver and observerExisted because it didn't allowed to add observers to notifications that only differs by sender
This commit is contained in:
Grzegorz Kościółek 2013-07-26 22:20:19 +02:00
parent 21da43a2ae
commit 3a1d4e7e28
2 changed files with 26 additions and 26 deletions

View File

@ -74,7 +74,7 @@ void NotificationCenter::purgeNotificationCenter(void)
//
// internal functions
//
bool NotificationCenter::observerExisted(Object *target,const char *name)
bool NotificationCenter::observerExisted(Object *target,const char *name, Object *sender)
{
Object* obj = NULL;
CCARRAY_FOREACH(_observers, obj)
@ -83,7 +83,7 @@ bool NotificationCenter::observerExisted(Object *target,const char *name)
if (!observer)
continue;
if (!strcmp(observer->getName(),name) && observer->getTarget() == target)
if (!strcmp(observer->getName(),name) && observer->getTarget() == target && observer->getSender() == sender)
return true;
}
return false;
@ -95,12 +95,12 @@ bool NotificationCenter::observerExisted(Object *target,const char *name)
void NotificationCenter::addObserver(Object *target,
SEL_CallFuncO selector,
const char *name,
Object *obj)
Object *sender)
{
if (this->observerExisted(target, name))
if (this->observerExisted(target, name, sender))
return;
NotificationObserver *observer = new NotificationObserver(target, selector, name, obj);
NotificationObserver *observer = new NotificationObserver(target, selector, name, sender);
if (!observer)
return;
@ -177,7 +177,7 @@ void NotificationCenter::unregisterScriptObserver(Object *target,const char* nam
}
}
void NotificationCenter::postNotification(const char *name, Object *object)
void NotificationCenter::postNotification(const char *name, Object *sender)
{
Array* ObserversCopy = Array::createWithCapacity(_observers->count());
ObserversCopy->addObjectsFromArray(_observers);
@ -188,7 +188,7 @@ void NotificationCenter::postNotification(const char *name, Object *object)
if (!observer)
continue;
if (!strcmp(name,observer->getName()) && (observer->getObject() == object || observer->getObject() == NULL || object == NULL))
if (!strcmp(name,observer->getName()) && (observer->getSender() == sender || observer->getSender() == NULL || sender == NULL))
{
if (0 != observer->getHandler())
{
@ -198,7 +198,7 @@ void NotificationCenter::postNotification(const char *name, Object *object)
}
else
{
observer->performSelector(object);
observer->performSelector(sender);
}
}
}
@ -241,11 +241,11 @@ int NotificationCenter::getObserverHandlerByName(const char* name)
NotificationObserver::NotificationObserver(Object *target,
SEL_CallFuncO selector,
const char *name,
Object *obj)
Object *sender)
{
_target = target;
_selector = selector;
_object = obj;
_sender = sender;
_name = name;
_handler = 0;
@ -256,14 +256,14 @@ NotificationObserver::~NotificationObserver()
}
void NotificationObserver::performSelector(Object *obj)
void NotificationObserver::performSelector(Object *sender)
{
if (_target)
{
if (obj) {
(_target->*_selector)(obj);
if (sender) {
(_target->*_selector)(sender);
} else {
(_target->*_selector)(_object);
(_target->*_selector)(_sender);
}
}
}
@ -283,9 +283,9 @@ const char* NotificationObserver::getName() const
return _name.c_str();
}
Object* NotificationObserver::getObject() const
Object* NotificationObserver::getSender() const
{
return _object;
return _sender;
}
int NotificationObserver::getHandler() const

View File

@ -58,12 +58,12 @@ public:
* @param target The target which wants to observe notification events.
* @param selector The callback function which will be invoked when the specified notification event was posted.
* @param name The name of this notification.
* @param obj The extra parameter which will be passed to the callback function.
* @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. NULL means that the sender is not used to decide whether to deliver the notification to target.
*/
void addObserver(Object *target,
SEL_CallFuncO selector,
const char *name,
Object *obj);
Object *sender);
/** @brief Removes the observer by the specified target and name.
* @param target The target of this notification.
@ -93,9 +93,9 @@ public:
/** @brief Posts one notification event by name.
* @param name The name of this notification.
* @param object The extra parameter.
* @param sender The object posting the notification. Can be NULL
*/
void postNotification(const char *name, Object *object);
void postNotification(const char *name, Object *sender);
/** @brief Gets script handler.
* @note Only supports Lua Binding now.
@ -112,7 +112,7 @@ private:
// internal functions
// Check whether the observer exists by the specified target and name.
bool observerExisted(Object *target,const char *name);
bool observerExisted(Object *target,const char *name, Object *sender);
// variables
//
@ -127,24 +127,24 @@ public:
* @param target The target which wants to observer notification events.
* @param selector The callback function which will be invoked when the specified notification event was posted.
* @param name The name of this notification.
* @param obj The extra parameter which will be passed to the callback function.
* @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. NULL means that the sender is not used to decide whether to deliver the notification to target.
*/
NotificationObserver(Object *target,
SEL_CallFuncO selector,
const char *name,
Object *obj);
Object *sender);
/** NotificationObserver destructor function */
~NotificationObserver();
/** Invokes the callback function of this observer */
void performSelector(Object *obj);
void performSelector(Object *sender);
// Getters / Setters
Object* getTarget() const;
SEL_CallFuncO getSelector() const;
const char* getName() const;
Object* getObject() const;
Object* getSender() const;
int getHandler() const;
void setHandler(int handler);
@ -152,7 +152,7 @@ private:
Object* _target;
SEL_CallFuncO _selector;
std::string _name;
Object* _object;
Object* _sender;
int _handler;
};