From d16fa076614b4b92828bc5b864960ac670ffc8bb Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Dec 2013 17:45:29 +0800 Subject: [PATCH] Removes Vector::forEach, please use std::for_each instead. The same as Vector::sort. --- cocos/2d/CCActionManager.cpp | 2 +- cocos/2d/CCClippingNode.cpp | 3 +- cocos/2d/CCLabel.cpp | 10 +-- cocos/2d/CCLabelBMFont.cpp | 8 +-- cocos/2d/CCLayer.cpp | 6 +- cocos/2d/CCMenu.cpp | 16 ++--- cocos/2d/CCMenuItem.cpp | 4 +- cocos/2d/CCNode.cpp | 16 ++--- cocos/2d/CCParticleBatchNode.cpp | 4 +- cocos/2d/CCRenderTexture.cpp | 2 +- cocos/2d/CCScene.cpp | 3 +- cocos/2d/CCScheduler.cpp | 2 +- cocos/2d/CCSprite.cpp | 6 +- cocos/2d/CCSpriteBatchNode.cpp | 17 ++--- cocos/2d/CCTMXLayer.cpp | 4 +- cocos/2d/CCTMXTiledMap.cpp | 3 +- cocos/base/CCAutoreleasePool.cpp | 4 +- cocos/base/CCVector.h | 63 +------------------ .../editor-support/cocosbuilder/CCBReader.cpp | 3 +- cocos/editor-support/cocostudio/CCBone.cpp | 2 +- .../GUI/CCControlExtension/CCControl.cpp | 16 +++-- .../GUI/CCControlExtension/CCScale9Sprite.cpp | 15 +++-- extensions/GUI/CCScrollView/CCScrollView.cpp | 6 +- extensions/GUI/CCScrollView/CCTableView.cpp | 4 +- .../Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp | 3 +- .../TestCpp/Classes/LayerTest/LayerTest.cpp | 3 +- .../Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp | 4 +- .../Classes/ParticleTest/ParticleTest.cpp | 11 ++-- .../PerformanceTest/PerformanceLabelTest.cpp | 8 ++- .../TestCpp/Classes/ShaderTest/ShaderTest.cpp | 3 +- .../SpriteTest/SpriteTest.cpp.REMOVED.git-id | 2 +- .../Classes/TileMapTest/TileMapTest.cpp | 31 +++++---- 32 files changed, 132 insertions(+), 152 deletions(-) diff --git a/cocos/2d/CCActionManager.cpp b/cocos/2d/CCActionManager.cpp index 2fb08fd0bd..7419311790 100644 --- a/cocos/2d/CCActionManager.cpp +++ b/cocos/2d/CCActionManager.cpp @@ -158,7 +158,7 @@ Vector ActionManager::pauseAllRunningActions() void ActionManager::resumeTargets(const Vector& targetsToResume) { - targetsToResume.forEach([this](Node* node){ + std::for_each(targetsToResume.begin(), targetsToResume.end(), [this](Node* node){ this->resumeTarget(node); }); } diff --git a/cocos/2d/CCClippingNode.cpp b/cocos/2d/CCClippingNode.cpp index f39131a4ac..61baa02aaa 100644 --- a/cocos/2d/CCClippingNode.cpp +++ b/cocos/2d/CCClippingNode.cpp @@ -40,7 +40,8 @@ static void setProgram(Node *n, GLProgram *p) { n->setShaderProgram(p); - n->getChildren().forEach([p](Node* child){ + auto& children = n->getChildren(); + std::for_each(children.begin(), children.end(), [p](Node* child){ setProgram(child, p); }); } diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index ba6847e7c4..e3c3ffe49c 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -305,7 +305,7 @@ void Label::alignText() LabelTextFormatter::alignText(this); int strLen = cc_wcslen(_currentUTF16String); - _children.forEach([this,&strLen](Node* child){ + std::for_each(_children.begin(), _children.end(), [this,&strLen](Node* child){ if (child) { int tag = child->getTag(); @@ -522,7 +522,7 @@ void Label::draw() _shaderProgram->setUniformLocationWith3f(_uniformEffectColor, _effectColor.r/255.0f,_effectColor.g/255.0f,_effectColor.b/255.0f); } - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->updateTransform(); }); @@ -710,7 +710,7 @@ void Label::setOpacityModifyRGB(bool isOpacityModifyRGB) { _isOpacityModifyRGB = isOpacityModifyRGB; - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ if (child) { RGBAProtocol *pRGBAProtocol = dynamic_cast(child); @@ -752,7 +752,7 @@ void Label::updateDisplayedOpacity(GLubyte parentOpacity) { _displayedOpacity = _realOpacity * parentOpacity/255.0; - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ Sprite *item = static_cast( child ); item->updateDisplayedOpacity(_displayedOpacity); }); @@ -818,7 +818,7 @@ void Label::updateDisplayedColor(const Color3B& parentColor) _displayedColor.g = _realColor.g * parentColor.g/255.0; _displayedColor.b = _realColor.b * parentColor.b/255.0; - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ Sprite *item = static_cast( child ); item->updateDisplayedColor(_displayedColor); }); diff --git a/cocos/2d/CCLabelBMFont.cpp b/cocos/2d/CCLabelBMFont.cpp index 23877d3adb..5d1716ada8 100644 --- a/cocos/2d/CCLabelBMFont.cpp +++ b/cocos/2d/CCLabelBMFont.cpp @@ -747,7 +747,7 @@ void LabelBMFont::setString(unsigned short *newString, bool needUpdateLabel) CC_SAFE_DELETE_ARRAY(tmp); } - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->setVisible(false); }); @@ -823,7 +823,7 @@ void LabelBMFont::setOpacity(GLubyte opacity) void LabelBMFont::setOpacityModifyRGB(bool var) { _isOpacityModifyRGB = var; - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ if (child) { RGBAProtocol *pRGBAProtocol = dynamic_cast(child); @@ -843,7 +843,7 @@ void LabelBMFont::updateDisplayedOpacity(GLubyte parentOpacity) { _displayedOpacity = _realOpacity * parentOpacity/255.0f; - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(),[this](Node* child){ Sprite *item = static_cast( child ); item->updateDisplayedOpacity(_displayedOpacity); }); @@ -855,7 +855,7 @@ void LabelBMFont::updateDisplayedColor(const Color3B& parentColor) _displayedColor.g = _realColor.g * parentColor.g/255.0f; _displayedColor.b = _realColor.b * parentColor.b/255.0f; - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(),[this](Node* child){ Sprite *item = static_cast( child ); item->updateDisplayedColor(_displayedColor); }); diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index ced8a179eb..f905e84312 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -507,7 +507,7 @@ void LayerRGBA::updateDisplayedOpacity(GLubyte parentOpacity) if (_cascadeOpacityEnabled) { - _children.forEach([this](Node* obj){ + std::for_each(_children.begin(), _children.end(),[this](Node* obj){ RGBAProtocol *item = dynamic_cast(obj); if (item) { @@ -525,7 +525,7 @@ void LayerRGBA::updateDisplayedColor(const Color3B& parentColor) if (_cascadeColorEnabled) { - _children.forEach([this](Node* obj){ + std::for_each(_children.begin(), _children.end(),[this](Node* obj){ RGBAProtocol *item = dynamic_cast(obj); if (item) { @@ -952,7 +952,7 @@ LayerMultiplex::LayerMultiplex() LayerMultiplex::~LayerMultiplex() { - _layers.forEach([](Layer* layer){ + std::for_each(_layers.begin(), _layers.end(), [](Layer* layer){ layer->cleanup(); }); } diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index a5edb05ae9..f233bea012 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -291,7 +291,7 @@ void Menu::alignItemsVerticallyWithPadding(float padding) { float height = -padding; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { height += child->getContentSize().height * child->getScaleY() + padding; @@ -300,7 +300,7 @@ void Menu::alignItemsVerticallyWithPadding(float padding) float y = height / 2.0f; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { child->setPosition(Point(0, y - child->getContentSize().height * child->getScaleY() / 2.0f)); @@ -317,7 +317,7 @@ void Menu::alignItemsHorizontally(void) void Menu::alignItemsHorizontallyWithPadding(float padding) { float width = -padding; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { width += child->getContentSize().width * child->getScaleX() + padding; @@ -326,7 +326,7 @@ void Menu::alignItemsHorizontallyWithPadding(float padding) float x = -width / 2.0f; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { child->setPosition(Point(x + child->getContentSize().width * child->getScaleX() / 2.0f, 0)); @@ -365,7 +365,7 @@ void Menu::alignItemsInColumnsWithArray(const ValueVector& rows) int columnsOccupied = 0; int rowColumns = 0; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { CCASSERT(row < rows.size(), ""); @@ -401,7 +401,7 @@ void Menu::alignItemsInColumnsWithArray(const ValueVector& rows) float x = 0.0; float y = (float)(height / 2); - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { if (child) @@ -469,7 +469,7 @@ void Menu::alignItemsInRowsWithArray(const ValueVector& columns) int rowsOccupied = 0; int columnRows; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { // check if too many menu items for the amount of rows/columns @@ -511,7 +511,7 @@ void Menu::alignItemsInRowsWithArray(const ValueVector& columns) float x = (float)(-width / 2); float y = 0.0; - _children.forEach([&](Node* child){ + std::for_each(_children.begin(), _children.end(), [&](Node* child){ if (child) { if (columnRows == 0) diff --git a/cocos/2d/CCMenuItem.cpp b/cocos/2d/CCMenuItem.cpp index 09cb00ee99..48e67b1a4e 100644 --- a/cocos/2d/CCMenuItem.cpp +++ b/cocos/2d/CCMenuItem.cpp @@ -918,7 +918,7 @@ void MenuItemToggle::addSubItem(MenuItem *item) MenuItemToggle::~MenuItemToggle() { - _subItems.forEach([](MenuItem* item){ + std::for_each(_subItems.begin(), _subItems.end(), [](MenuItem* item){ item->cleanup(); }); } @@ -970,7 +970,7 @@ void MenuItemToggle::setEnabled(bool enabled) { MenuItem::setEnabled(enabled); - _subItems.forEach([&enabled](MenuItem* item){ + std::for_each(_subItems.begin(), _subItems.end(), [&enabled](MenuItem* item){ item->setEnabled(enabled); }); } diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 233ee8f286..de1a4c7161 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -576,7 +576,7 @@ void Node::cleanup() } // timers - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->cleanup(); }); } @@ -939,7 +939,7 @@ void Node::onEnter() { _isTransitionFinished = false; - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->onEnter(); }); @@ -960,7 +960,7 @@ void Node::onEnterTransitionDidFinish() { _isTransitionFinished = true; - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->onEnterTransitionDidFinish(); }); @@ -975,7 +975,7 @@ void Node::onEnterTransitionDidFinish() void Node::onExitTransitionDidStart() { - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->onExitTransitionDidStart(); }); @@ -1001,7 +1001,7 @@ void Node::onExit() ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); } - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->onExit(); }); } @@ -1413,7 +1413,7 @@ bool Node::updatePhysicsTransform() void Node::updateTransform() { // Recursively iterate over children - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->updateTransform(); }); } @@ -1524,7 +1524,7 @@ void NodeRGBA::updateDisplayedOpacity(GLubyte parentOpacity) if (_cascadeOpacityEnabled) { - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ RGBAProtocol* item = dynamic_cast(child); if (item) { @@ -1579,7 +1579,7 @@ void NodeRGBA::updateDisplayedColor(const Color3B& parentColor) if (_cascadeColorEnabled) { - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ RGBAProtocol *item = dynamic_cast(child); if (item) { diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index 85c7315617..7b8d700082 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -385,7 +385,7 @@ void ParticleBatchNode::removeChildAtIndex(int index, bool doCleanup) void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup) { - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ static_cast(child)->setBatchNode(nullptr); }); @@ -481,7 +481,7 @@ void ParticleBatchNode::updateAllAtlasIndexes() { int index = 0; - _children.forEach([&index](Node* child){ + std::for_each(_children.begin(), _children.end(), [&index](Node* child){ ParticleSystem* partiSys = static_cast(child); partiSys->setAtlasIndex(index); index += partiSys->getTotalParticles(); diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index 6075c4eab3..db1a28e58a 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -529,7 +529,7 @@ void RenderTexture::draw() //! make sure all children are drawn sortAllChildren(); - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ if (child != _sprite) { child->visit(); diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index e37f52d0ef..cb57fe35d4 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -145,7 +145,8 @@ void Scene::addChildToPhysicsWorld(Node* child) _physicsWorld->addBody(node->getPhysicsBody()); } - node->getChildren().forEach([addToPhysicsWorldFunc](Node* n){ + auto& children = node->getChildren(); + std::for_each(children.begin(), children.end(), [addToPhysicsWorldFunc](Node* n){ addToPhysicsWorldFunc(n); }); }; diff --git a/cocos/2d/CCScheduler.cpp b/cocos/2d/CCScheduler.cpp index 064e13b058..09d0a9da6c 100644 --- a/cocos/2d/CCScheduler.cpp +++ b/cocos/2d/CCScheduler.cpp @@ -809,7 +809,7 @@ Vector Scheduler::pauseAllTargetsWithMinPriority(int minPriority) void Scheduler::resumeTargets(const Vector& targetsToResume) { - targetsToResume.forEach([this](Object* obj){ + std::for_each(targetsToResume.begin(), targetsToResume.end(), [this](Object* obj){ this->resumeTarget(obj); }); } diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 4aa5a744f1..5af035faea 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -817,7 +817,7 @@ void Sprite::removeAllChildrenWithCleanup(bool cleanup) { if (_batchNode) { - _children.forEach([this](Node* child){ + std::for_each(_children.begin(), _children.end(), [this](Node* child){ Sprite* sprite = dynamic_cast(child); if (sprite) { @@ -863,7 +863,7 @@ void Sprite::sortAllChildren() if ( _batchNode) { - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->sortAllChildren(); }); } @@ -900,7 +900,7 @@ void Sprite::setDirtyRecursively(bool bValue) // recursively set dirty if (_hasChildren) { - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ Sprite* sp = dynamic_cast(child); if (sp) { diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index 2194ea7a2c..267984c169 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -270,7 +270,7 @@ void SpriteBatchNode::sortAllChildren() if (!_children.empty()) { //first sort all children recursively based on zOrder - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->sortAllChildren(); }); @@ -278,7 +278,7 @@ void SpriteBatchNode::sortAllChildren() //fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact) // and at the same time reorder descendants and the quads to the right index - _children.forEach([this, &index](Node* child){ + std::for_each(_children.begin(), _children.end(), [this, &index](Node* child){ Sprite* sp = static_cast(child); updateAtlasIndex(sp, &index); }); @@ -324,7 +324,7 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, ssize_t* curIndex) needNewIndex = false; } - array.forEach([&](Node* child){ + std::for_each(array.begin(), array.end(), [&](Node* child){ Sprite* sp = static_cast(child); if (needNewIndex && sp->getZOrder() >= 0) { @@ -390,7 +390,7 @@ void SpriteBatchNode::draw(void) CC_NODE_DRAW_SETUP(); - _children.forEach([](Node* child){ + std::for_each(_children.begin(), _children.end(), [](Node* child){ child->updateTransform(); }); @@ -426,7 +426,7 @@ ssize_t SpriteBatchNode::rebuildIndexInOrder(Sprite *parent, ssize_t index) auto& children = parent->getChildren(); - children.forEach([this, &index](Node* child){ + std::for_each(children.begin(), children.end(), [this, &index](Node* child){ Sprite* sp = static_cast(child); if (sp && (sp->getZOrder() < 0)) { @@ -441,7 +441,7 @@ ssize_t SpriteBatchNode::rebuildIndexInOrder(Sprite *parent, ssize_t index) index++; } - children.forEach([this, &index](Node* child){ + std::for_each(children.begin(), children.end(), [this, &index](Node* child){ Sprite* sp = static_cast(child); if (sp && (sp->getZOrder() >= 0)) { @@ -559,7 +559,8 @@ void SpriteBatchNode::appendChild(Sprite* sprite) _textureAtlas->insertQuad(&quad, index); // add children recursively - sprite->getChildren().forEach([this](Node* child){ + auto& children = sprite->getChildren(); + std::for_each(children.begin(), children.end(), [this](Node* child){ appendChild(static_cast(child)); }); } @@ -586,7 +587,7 @@ void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite) // remove children recursively auto& children = sprite->getChildren(); - children.forEach([this](Node* obj){ + std::for_each(children.begin(), children.end(), [this](Node* obj){ Sprite* child = static_cast(obj); if (child) { diff --git a/cocos/2d/CCTMXLayer.cpp b/cocos/2d/CCTMXLayer.cpp index fa8bb8fba4..335e6294aa 100644 --- a/cocos/2d/CCTMXLayer.cpp +++ b/cocos/2d/CCTMXLayer.cpp @@ -389,7 +389,7 @@ Sprite * TMXLayer::insertTileForGID(unsigned int gid, const Point& pos) // update possible children - _children.forEach([&indexForZ](Node* child){ + std::for_each(_children.begin(), _children.end(), [&indexForZ](Node* child){ Sprite* sp = static_cast(child); if (child) { @@ -594,7 +594,7 @@ void TMXLayer::removeTileAt(const Point& pos) _textureAtlas->removeQuadAtIndex(atlasIndex); // update possible children - _children.forEach([&atlasIndex](Node* obj){ + std::for_each(_children.begin(), _children.end(), [&atlasIndex](Node* obj){ Sprite* child = static_cast(obj); if (child) { diff --git a/cocos/2d/CCTMXTiledMap.cpp b/cocos/2d/CCTMXTiledMap.cpp index e7847b7644..cc71bdce58 100644 --- a/cocos/2d/CCTMXTiledMap.cpp +++ b/cocos/2d/CCTMXTiledMap.cpp @@ -168,7 +168,8 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo) int idx=0; - mapInfo->getLayers().forEach([&idx, this, &mapInfo](TMXLayerInfo* layerInfo){ + auto& layers = mapInfo->getLayers(); + std::for_each(layers.begin(), layers.end(), [&idx, this, &mapInfo](TMXLayerInfo* layerInfo){ if (layerInfo && layerInfo->_visible) { TMXLayer *child = parseLayer(layerInfo, mapInfo); diff --git a/cocos/base/CCAutoreleasePool.cpp b/cocos/base/CCAutoreleasePool.cpp index d829c7bb10..f90753f43d 100644 --- a/cocos/base/CCAutoreleasePool.cpp +++ b/cocos/base/CCAutoreleasePool.cpp @@ -64,7 +64,7 @@ void AutoreleasePool::clear() int nIndex = _managedObjectArray.size() - 1; #endif - _managedObjectArray.forEachReverse([&](Object* obj){ + std::for_each(_managedObjectArray.rbegin(), _managedObjectArray.rend(), [&](Object* obj){ --(obj->_autoReleaseCount); //(*it)->release(); //delete (*it); @@ -118,7 +118,7 @@ void PoolManager::finalize() { if (!_releasePoolStack.empty()) { - _releasePoolStack.forEach([](AutoreleasePool* pool){ + std::for_each(_releasePoolStack.begin(), _releasePoolStack.end(), [](AutoreleasePool* pool){ pool->clear(); }); } diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 92077e29f6..0112d1a2eb 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -303,8 +303,10 @@ public: /** @brief Remove a certain object. * @param object The object to be removed. * @param toRelease Whether to decrease the referece count of the deleted object. + * @return An iterator pointing to the new location of the element that followed the last element erased by the function call. + * This is the container end if the operation erased the last element in the sequence. */ - void eraseObject(T object, bool toRelease = true) + iterator eraseObject(T object, bool toRelease = true) { CCASSERT(object != nullptr, "The object should not be nullptr"); auto iter = std::find(_data.begin(), _data.end(), object); @@ -410,65 +412,6 @@ public: _data.shrink_to_fit(); } - /** Traverses through the vector and applys the callback function for each element */ - void forEach(const std::function& callback) - { - if (empty()) - return; - - std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ - callback(obj); - }); - } - - /** Traverses through the vector and applys the callback function for each element */ - void forEach(const std::function& callback) const - { - if (empty()) - return; - - std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ - callback(obj); - }); - } - - /** Traverses through the vector in reversed order and applys the callback function for each element */ - void forEachReverse(const std::function& callback) - { - if (empty()) - return; - - std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){ - callback(obj); - }); - } - - /** Traverses through the vector in reversed order and applys the callback function for each element */ - void forEachReverse(const std::function& callback) const - { - if (empty()) - return; - - std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){ - callback(obj); - }); - } - - /** @brief Sorts all elements in the vector according the binary callback function. - * @param callback Binary function that accepts two elements in the vector as arguments, - * and returns a value convertible to bool. - * The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. - */ - void sort(const std::function& callback) - { - if (empty()) - return; - - std::sort(_data.begin(), _data.end(), [&callback](T a, T b) -> bool{ - return callback(a, b); - }); - } - protected: /** Retains all the objects in the vector */ diff --git a/cocos/editor-support/cocosbuilder/CCBReader.cpp b/cocos/editor-support/cocosbuilder/CCBReader.cpp index db5733a19a..ea743318db 100644 --- a/cocos/editor-support/cocosbuilder/CCBReader.cpp +++ b/cocos/editor-support/cocosbuilder/CCBReader.cpp @@ -293,7 +293,8 @@ void CCBReader::cleanUpNodeGraph(Node *node) { node->setUserObject(nullptr); - node->getChildren().forEach([this](Node* obj){ + auto& children = node->getChildren(); + std::for_each(children.begin(), children.end(), [this](Node* obj){ cleanUpNodeGraph(obj); }); } diff --git a/cocos/editor-support/cocostudio/CCBone.cpp b/cocos/editor-support/cocostudio/CCBone.cpp index 42b9316287..12d627ce45 100644 --- a/cocos/editor-support/cocostudio/CCBone.cpp +++ b/cocos/editor-support/cocostudio/CCBone.cpp @@ -228,7 +228,7 @@ void Bone::update(float delta) DisplayFactory::updateDisplay(this, delta, _boneTransformDirty || _armature->getArmatureTransformDirty()); - _children.forEach([&delta](Node* obj){ + std::for_each(_children.begin(), _children.end(), [&delta](Node* obj){ Bone *childBone = static_cast(obj); childBone->update(delta); }); diff --git a/extensions/GUI/CCControlExtension/CCControl.cpp b/extensions/GUI/CCControlExtension/CCControl.cpp index ab6244b783..56a88970a8 100644 --- a/extensions/GUI/CCControlExtension/CCControl.cpp +++ b/extensions/GUI/CCControlExtension/CCControl.cpp @@ -111,7 +111,7 @@ void Control::sendActionsForControlEvents(EventType controlEvents) // Call invocations const auto& invocationList = this->dispatchListforControlEvent((Control::EventType)(1<invoke(this); }); @@ -192,8 +192,10 @@ void Control::removeTargetWithActionForControlEvent(Object* target, Handler acti } else { - //normally we would use a predicate, but this won't work here. Have to do it manually - eventInvocationList.forEach([&](Invocation* invocation){ + std::vector tobeRemovedInvocations; + + //normally we would use a predicate, but this won't work here. Have to do it manually + std::for_each(eventInvocationList.begin(), eventInvocationList.end(), [&](Invocation* invocation){ bool shouldBeRemoved=true; if (target) { @@ -206,9 +208,13 @@ void Control::removeTargetWithActionForControlEvent(Object* target, Handler acti // Remove the corresponding invocation object if (shouldBeRemoved) { - eventInvocationList.eraseObject(invocation, bDeleteObjects); + tobeRemovedInvocations.push_back(invocation); } }); + + std::for_each(tobeRemovedInvocations.begin(), tobeRemovedInvocations.end(), [&](Invocation* invocation){ + eventInvocationList.eraseObject(invocation, bDeleteObjects); + }); } } @@ -218,7 +224,7 @@ void Control::setOpacityModifyRGB(bool bOpacityModifyRGB) { _isOpacityModifyRGB=bOpacityModifyRGB; - _children.forEach([&](Node* obj){ + std::for_each(_children.begin(), _children.end(), [&](Node* obj){ RGBAProtocol* rgba = dynamic_cast(obj); if (rgba) { diff --git a/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp b/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp index d46726ee07..36fb6123fe 100644 --- a/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp +++ b/extensions/GUI/CCControlExtension/CCScale9Sprite.cpp @@ -700,7 +700,8 @@ void Scale9Sprite::setOpacityModifyRGB(bool var) } _opacityModifyRGB = var; - _scale9Image->getChildren().forEach([this](Node* child){ + auto& children = _scale9Image->getChildren(); + std::for_each(children.begin(), children.end(), [this](Node* child){ RGBAProtocol* rgba = dynamic_cast(child); if (rgba) { @@ -789,7 +790,8 @@ void Scale9Sprite::setColor(const Color3B& color) NodeRGBA::setColor(color); - _scale9Image->getChildren().forEach([&color](Node* child){ + auto& children = _scale9Image->getChildren(); + std::for_each(children.begin(), children.end(), [&color](Node* child){ RGBAProtocol* rgba = dynamic_cast(child); if (rgba) { @@ -811,7 +813,8 @@ void Scale9Sprite::setOpacity(GLubyte opacity) } NodeRGBA::setOpacity(opacity); - _scale9Image->getChildren().forEach([&opacity](Node* child){ + auto& children = _scale9Image->getChildren(); + std::for_each(children.begin(), children.end(), [&opacity](Node* child){ RGBAProtocol* rgba = dynamic_cast(child); if (rgba) { @@ -833,7 +836,8 @@ void Scale9Sprite::updateDisplayedColor(const cocos2d::Color3B &parentColor) } NodeRGBA::updateDisplayedColor(parentColor); - _scale9Image->getChildren().forEach([&parentColor](Node* child){ + auto& children = _scale9Image->getChildren(); + std::for_each(children.begin(), children.end(), [&parentColor](Node* child){ RGBAProtocol* rgba = dynamic_cast(child); if (rgba) { @@ -850,7 +854,8 @@ void Scale9Sprite::updateDisplayedOpacity(GLubyte parentOpacity) } NodeRGBA::updateDisplayedOpacity(parentOpacity); - _scale9Image->getChildren().forEach([&parentOpacity](Node* child){ + auto& children = _scale9Image->getChildren(); + std::for_each(children.begin(), children.end(), [&parentOpacity](Node* child){ RGBAProtocol* rgba = dynamic_cast(child); if (rgba) { diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index 2a61d5223e..5418d0ed0c 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -153,14 +153,16 @@ void ScrollView::pause(Object* sender) { _container->pause(); - _container->getChildren().forEach([](Node* child){ + auto& children = _container->getChildren(); + std::for_each(children.begin(), children.end(), [](Node* child){ child->pause(); }); } void ScrollView::resume(Object* sender) { - _container->getChildren().forEach([](Node* child){ + auto& children = _container->getChildren(); + std::for_each(children.begin(), children.end(), [](Node* child){ child->resume(); }); diff --git a/extensions/GUI/CCScrollView/CCTableView.cpp b/extensions/GUI/CCScrollView/CCTableView.cpp index 46d38f4615..073c793399 100644 --- a/extensions/GUI/CCScrollView/CCTableView.cpp +++ b/extensions/GUI/CCScrollView/CCTableView.cpp @@ -98,7 +98,7 @@ void TableView::reloadData() { _oldDirection = Direction::NONE; - _cellsUsed.forEach([this](TableViewCell* cell){ + std::for_each(_cellsUsed.begin(), _cellsUsed.end(), [this](TableViewCell* cell){ if(_tableViewDelegate != NULL) { _tableViewDelegate->tableCellWillRecycle(this, cell); } @@ -455,7 +455,7 @@ void TableView::scrollViewDidScroll(ScrollView* view) if (_isUsedCellsDirty) { _isUsedCellsDirty = false; - _cellsUsed.sort([](TableViewCell *a, TableViewCell *b) -> bool{ + std::sort(_cellsUsed.begin(), _cellsUsed.end(), [](TableViewCell *a, TableViewCell *b) -> bool{ return a->getIdx() < b->getIdx(); }); } diff --git a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp index 07c0aef320..3020e851e9 100644 --- a/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp +++ b/samples/Cpp/TestCpp/Classes/BugsTest/Bug-422.cpp @@ -47,7 +47,8 @@ void Bug422Layer::reset() void Bug422Layer::check(Node* t) { - t->getChildren().forEach([this](Node* child){ + auto& children = t->getChildren(); + std::for_each(children.begin(), children.end(), [this](Node* child){ log("%p, rc: %d", child, child->retainCount()); check(child); }); diff --git a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp index dace8cbd86..d9149fed6d 100644 --- a/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LayerTest/LayerTest.cpp @@ -119,7 +119,8 @@ static void setEnableRecursiveCascading(Node* node, bool enable) rgba->setCascadeOpacityEnabled(enable); } - node->getChildren().forEach([enable](Node* child){ + auto& children = node->getChildren(); + std::for_each(children.begin(), children.end(), [enable](Node* child){ setEnableRecursiveCascading(child, enable); }); } diff --git a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp index eb8aaa9448..3b2af08048 100644 --- a/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp +++ b/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp @@ -88,7 +88,9 @@ MenuLayerMainMenu::MenuLayerMainMenu() auto s = Director::getInstance()->getWinSize(); int i=0; - menu->getChildren().forEach([&i, &s](Node* child){ + auto& children = menu->getChildren(); + + std::for_each(children.begin(), children.end(), [&i, &s](Node* child){ auto dstPoint = child->getPosition(); int offset = (int) (s.width/2 + 50); if( i % 2 == 0) diff --git a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp index 4425ef3974..b3158654df 100644 --- a/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParticleTest/ParticleTest.cpp @@ -1530,7 +1530,7 @@ void MultipleParticleSystems::update(float dt) unsigned int count = 0; - getChildren().forEach([&count](Node* child){ + std::for_each(_children.begin(), _children.end(), [&count](Node* child){ auto item = dynamic_cast(child); if (item != NULL) { @@ -1582,7 +1582,8 @@ void MultipleParticleSystemsBatched::update(float dt) auto batchNode = getChildByTag(2); - batchNode->getChildren().forEach([&count](Node* child){ + auto& children = batchNode->getChildren(); + std::for_each(children.begin(), children.end(), [&count](Node* child){ auto item = dynamic_cast(child); if (item != NULL) { @@ -1671,7 +1672,8 @@ void AddAndDeleteParticleSystems::update(float dt) auto batchNode = getChildByTag(2); - batchNode->getChildren().forEach([&count](Node* child){ + auto& children = batchNode->getChildren(); + std::for_each(children.begin(), children.end(), [&count](Node* child){ auto item = dynamic_cast(child); if (item != NULL) { @@ -1807,7 +1809,8 @@ void ReorderParticleSystems::update(float dt) auto batchNode = getChildByTag(2); - batchNode->getChildren().forEach([&count](Node* child){ + auto& children = batchNode->getChildren(); + std::for_each(children.begin(), children.end(), [&count](Node* child){ auto item = dynamic_cast(child); if (item != nullptr) { diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceLabelTest.cpp b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceLabelTest.cpp index f5c91270a8..1cbcbf900c 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceLabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceLabelTest.cpp @@ -311,10 +311,12 @@ void LabelMainScene::updateText(float dt) char text[20]; sprintf(text,"%.2f",_accumulativeTime); + auto& children = _labelContainer->getChildren(); + switch (_s_labelCurCase) { case kCaseLabelTTFUpdate: - _labelContainer->getChildren().forEach([&text](Node* child){ + std::for_each(children.begin(), children.end(), [&text](Node* child){ if (child) { LabelTTF* label = (LabelTTF*)child; @@ -323,7 +325,7 @@ void LabelMainScene::updateText(float dt) }); break; case kCaseLabelBMFontUpdate: - _labelContainer->getChildren().forEach([&text](Node* child){ + std::for_each(children.begin(), children.end(), [&text](Node* child){ if (child) { LabelBMFont* label = (LabelBMFont*)child; @@ -332,7 +334,7 @@ void LabelMainScene::updateText(float dt) }); break; case kCaseLabelUpdate: - _labelContainer->getChildren().forEach([&text](Node* child){ + std::for_each(children.begin(), children.end(), [&text](Node* child){ if (child) { Label* label = (Label*)child; diff --git a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp index f2a6008766..b9e297a8fd 100644 --- a/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest.cpp @@ -679,7 +679,8 @@ void ShaderRetroEffect::update(float dt) _accum += dt; int i=0; - _label->getChildren().forEach([&i, this](Node* sprite){ + auto& children = _label->getChildren(); + std::for_each(children.begin(), children.end(), [&i, this](Node* sprite){ i++; auto oldPosition = sprite->getPosition(); sprite->setPosition(Point( oldPosition.x, sinf( _accum * 2 + i/2.0) * 20 )); diff --git a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id index 73c4962d89..afb5fb8eae 100644 --- a/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id +++ b/samples/Cpp/TestCpp/Classes/SpriteTest/SpriteTest.cpp.REMOVED.git-id @@ -1 +1 @@ -e4cdac1cca25440803dbf150fc336b61c44edb93 \ No newline at end of file +de445fcbdc65bcfb39c12f4e5c05f48b7ce64ab7 \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp index b1d455c7ea..5c9d0c6905 100644 --- a/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp +++ b/samples/Cpp/TestCpp/Classes/TileMapTest/TileMapTest.cpp @@ -128,9 +128,9 @@ TMXOrthoTest::TMXOrthoTest() Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); - auto& pChildrenArray = map->getChildren(); + auto& children = map->getChildren(); - pChildrenArray.forEach([](Node* obj){ + std::for_each(children.begin(), children.end(), [](Node* obj){ auto child = static_cast(obj); child->getTexture()->setAntiAliasTexParameters(); }); @@ -171,10 +171,10 @@ TMXOrthoTest2::TMXOrthoTest2() Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); - auto& pChildrenArray = map->getChildren(); + auto& children = map->getChildren(); SpriteBatchNode* child = NULL; - pChildrenArray.forEach([&child](Node* obj){ + std::for_each(children.begin(), children.end(), [&child](Node* obj){ child = static_cast(obj); child->getTexture()->setAntiAliasTexParameters(); }); @@ -203,7 +203,7 @@ TMXOrthoTest3::TMXOrthoTest3() auto& children = map->getChildren(); SpriteBatchNode* child = NULL; - children.forEach([&child](Node* node){ + std::for_each(children.begin(), children.end(), [&child](Node* node){ child = static_cast(node); child->getTexture()->setAntiAliasTexParameters(); }); @@ -231,7 +231,10 @@ TMXOrthoTest4::TMXOrthoTest4() CCLOG("ContentSize: %f, %f", s1.width,s1.height); SpriteBatchNode* child = nullptr; - map->getChildren().forEach([&child](Node* node){ + + auto& children = map->getChildren(); + + std::for_each(children.begin(), children.end(), [&child](Node* node){ child = static_cast(node); child->getTexture()->setAntiAliasTexParameters(); }); @@ -529,7 +532,9 @@ TMXUncompressedTest::TMXUncompressedTest() // testing release map TMXLayer* layer; - map->getChildren().forEach([&layer](Node* node){ + + auto& children = map->getChildren(); + std::for_each(children.begin(), children.end(), [&layer](Node* node){ layer= static_cast(node); layer->releaseMap(); }); @@ -1097,7 +1102,8 @@ TMXOrthoFlipTest::TMXOrthoFlipTest() Size CC_UNUSED s = map->getContentSize(); log("ContentSize: %f, %f", s.width,s.height); - map->getChildren().forEach([](Node* node){ + auto& children = map->getChildren(); + std::for_each(children.begin(), children.end(), [](Node* node){ auto child = static_cast(node); child->getTexture()->setAntiAliasTexParameters(); }); @@ -1125,7 +1131,8 @@ TMXOrthoFlipRunTimeTest::TMXOrthoFlipRunTimeTest() auto s = map->getContentSize(); log("ContentSize: %f, %f", s.width,s.height); - map->getChildren().forEach([](Node* node){ + auto& children = map->getChildren(); + std::for_each(children.begin(), children.end(), [](Node* node){ auto child = static_cast(node); child->getTexture()->setAntiAliasTexParameters(); }); @@ -1202,7 +1209,8 @@ TMXOrthoFromXMLTest::TMXOrthoFromXMLTest() auto s = map->getContentSize(); log("ContentSize: %f, %f", s.width,s.height); - map->getChildren().forEach([](Node* node){ + auto& children = map->getChildren(); + std::for_each(children.begin(), children.end(), [](Node* node){ auto child = static_cast(node); child->getTexture()->setAntiAliasTexParameters(); }); @@ -1229,7 +1237,8 @@ TMXBug987::TMXBug987() Size CC_UNUSED s1 = map->getContentSize(); CCLOG("ContentSize: %f, %f", s1.width,s1.height); - map->getChildren().forEach([](Node* child){ + auto& children = map->getChildren(); + std::for_each(children.begin(), children.end(), [](Node* child){ auto node = static_cast(child); node->getTexture()->setAntiAliasTexParameters(); });