Merge pull request #3285 from gkosciolek/fix_for_CCNotificationCenter

closed 2611: Fixing the bug that observers with the same target and selector but different sender are the same observer in NotificationCenter.
This commit is contained in:
James Chen 2013-08-19 23:23:18 -07:00
commit c215a1d5a2
2 changed files with 27 additions and 27 deletions

View File

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

View File

@ -58,12 +58,12 @@ public:
* @param target The target which wants to observe notification events. * @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 selector The callback function which will be invoked when the specified notification event was posted.
* @param name The name of this notification. * @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, void addObserver(Object *target,
SEL_CallFuncO selector, SEL_CallFuncO selector,
const char *name, const char *name,
Object *obj); Object *sender);
/** @brief Removes the observer by the specified target and name. /** @brief Removes the observer by the specified target and name.
* @param target The target of this notification. * @param target The target of this notification.
@ -93,9 +93,9 @@ public:
/** @brief Posts one notification event by name. /** @brief Posts one notification event by name.
* @param name The name of this notification. * @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. /** @brief Gets script handler.
* @note Only supports Lua Binding now. * @note Only supports Lua Binding now.
@ -112,7 +112,7 @@ private:
// internal functions // internal functions
// Check whether the observer exists by the specified target and name. // 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 // variables
// //
@ -127,24 +127,24 @@ public:
* @param target The target which wants to observer notification events. * @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 selector The callback function which will be invoked when the specified notification event was posted.
* @param name The name of this notification. * @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, NotificationObserver(Object *target,
SEL_CallFuncO selector, SEL_CallFuncO selector,
const char *name, const char *name,
Object *obj); Object *sender);
/** NotificationObserver destructor function */ /** NotificationObserver destructor function */
~NotificationObserver(); ~NotificationObserver();
/** Invokes the callback function of this observer */ /** Invokes the callback function of this observer */
void performSelector(Object *obj); void performSelector(Object *sender);
// Getters / Setters // Getters / Setters
Object* getTarget() const; Object* getTarget() const;
SEL_CallFuncO getSelector() const; SEL_CallFuncO getSelector() const;
const char* getName() const; const char* getName() const;
Object* getObject() const; Object* getSender() const;
int getHandler() const; int getHandler() const;
void setHandler(int handler); void setHandler(int handler);
@ -152,7 +152,7 @@ private:
Object* _target; Object* _target;
SEL_CallFuncO _selector; SEL_CallFuncO _selector;
std::string _name; std::string _name;
Object* _object; Object* _sender;
int _handler; int _handler;
}; };