From 3e01dd5edfedab873a777e8c8ff84560ac248a86 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sun, 15 Sep 2013 21:58:54 +0800 Subject: [PATCH] [dispatcher] Using std::vector instead of std::list for EventListenerItem container. --- cocos2dx/event_dispatcher/CCEventDispatcher.cpp | 14 +++++++------- cocos2dx/event_dispatcher/CCEventDispatcher.h | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index a0a6880c67..37759b155b 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -77,19 +77,19 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) { if (!_listeners) { - _listeners = new std::map*>(); + _listeners = new std::map*>(); } auto itr = _listeners->find(item->listener->type); if (itr == _listeners->end()) { - _listeners->insert(std::make_pair(item->listener->type, new std::list())); + _listeners->insert(std::make_pair(item->listener->type, new std::vector())); itr = _listeners->find(item->listener->type); } auto listenerList = itr->second; - listenerList->push_front(item); + listenerList->insert(listenerList->begin(), item); } void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) @@ -144,7 +144,7 @@ void EventDispatcher::removeEventListener(EventListener* listener) { (*itemIter)->listener->release(); delete (*itemIter); - iter->second->remove(*itemIter); + iter->second->erase(itemIter); } else { @@ -268,7 +268,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) for (auto iter = touchListeners->begin(); iter != touchListeners->end(); ++iter) { - TouchEventListener* touchEventListener = static_cast((*iter)->listener); + auto touchEventListener = static_cast((*iter)->listener); if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE) { @@ -488,7 +488,7 @@ void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventT auto listenerList = getListenerItemsForType(eventType); // After sort: priority < 0, = 0, scene graph, > 0 - listenerList->sort([](const EventListenerItem* item1, const EventListenerItem* item2) { + std::sort(listenerList->begin(), listenerList->end(), [](const EventListenerItem* item1, const EventListenerItem* item2) { // item1 and item2 are both using fixed priority. if (nullptr == item1->node && nullptr == item2->node) { @@ -515,7 +515,7 @@ void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventT }); } -std::list* EventDispatcher::getListenerItemsForType(const std::string &eventType) +std::vector* EventDispatcher::getListenerItemsForType(const std::string &eventType) { if (_listeners != nullptr) { diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.h b/cocos2dx/event_dispatcher/CCEventDispatcher.h index cb0cf75684..64d8f8c499 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.h +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.h @@ -32,6 +32,7 @@ #include #include #include +#include NS_CC_BEGIN @@ -115,7 +116,7 @@ private: void dispatchTouchEvent(TouchEvent* event); /** Gets event the listener list for the event type. */ - std::list* getListenerItemsForType(const std::string& eventType); + std::vector* getListenerItemsForType(const std::string& eventType); /** Sorts the listeners of specified type by priority */ void sortAllEventListenerItemsForType(const std::string& eventType); @@ -127,7 +128,7 @@ private: /** * Listeners map. */ - std::map*>* _listeners; + std::map*>* _listeners; int _inDispatch; std::list _eventNodes;