From 9928e8bef1527251f64399e0c2ff461f524dd4da Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 29 Nov 2013 16:33:15 +0800 Subject: [PATCH] issue #2790: Uses Vector::getObjectAtIndex instead of Vector[] operator to avoid some mistakes. --- cocos/2d/CCActionInterval.cpp | 10 ++-- cocos/2d/CCEventDispatcher.cpp | 4 +- cocos/2d/CCLayer.cpp | 12 ++-- cocos/2d/CCMenuItem.cpp | 8 +-- cocos/2d/CCNode.cpp | 21 ++++--- cocos/2d/CCParticleBatchNode.cpp | 12 ++-- cocos/2d/CCSprite.cpp | 2 +- cocos/2d/CCSpriteBatchNode.cpp | 8 +-- cocos/base/CCVector.h | 57 ++++++++++++------- .../editor-support/cocostudio/CCArmature.cpp | 2 +- 10 files changed, 77 insertions(+), 59 deletions(-) diff --git a/cocos/2d/CCActionInterval.cpp b/cocos/2d/CCActionInterval.cpp index 936c94823a..56c4c1a5f9 100644 --- a/cocos/2d/CCActionInterval.cpp +++ b/cocos/2d/CCActionInterval.cpp @@ -206,13 +206,13 @@ Sequence* Sequence::create(const Vector& arrayOfActions) long count = arrayOfActions.count(); CC_BREAK_IF(count == 0); - auto prev = arrayOfActions[0]; + auto prev = arrayOfActions.getObjectAtIndex(0); if (count > 1) { for (long i = 1; i < count; ++i) { - prev = createWithTwoActions(prev, arrayOfActions[i]); + prev = createWithTwoActions(prev, arrayOfActions.getObjectAtIndex(i)); } } else @@ -578,12 +578,12 @@ Spawn* Spawn::create(const Vector& arrayOfActions) { long count = arrayOfActions.count(); CC_BREAK_IF(count == 0); - auto prev = arrayOfActions[0]; + auto prev = arrayOfActions.getObjectAtIndex(0); if (count > 1) { for (int i = 1; i < arrayOfActions.count(); ++i) { - prev = createWithTwoActions(prev, arrayOfActions[i]); + prev = createWithTwoActions(prev, arrayOfActions.getObjectAtIndex(i)); } } else @@ -2104,7 +2104,7 @@ void Animate::update(float t) float splitTime = _splitTimes->at(i); if( splitTime <= t ) { - AnimationFrame* frame = frames[i]; + AnimationFrame* frame = frames.getObjectAtIndex(i); frameToDisplay = frame->getSpriteFrame(); static_cast(_target)->setDisplayFrame(frameToDisplay); diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 285ead66ea..a0831ce9ab 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -200,7 +200,7 @@ void EventDispatcher::visitTarget(Node* node) // visit children zOrder < 0 for( ; i < childrenCount; i++ ) { - child = children[i]; + child = children.getObjectAtIndex(i); if ( child && child->getZOrder() < 0 ) visitTarget(child); @@ -212,7 +212,7 @@ void EventDispatcher::visitTarget(Node* node) for( ; i < childrenCount; i++ ) { - child = children[i]; + child = children.getObjectAtIndex(i); if (child) visitTarget(child); } diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index bd04800a57..c9b9d0c38d 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -1005,7 +1005,7 @@ bool LayerMultiplex::initWithLayers(Layer *layer, va_list params) } _enabledLayer = 0; - this->addChild(_layers[_enabledLayer]); + this->addChild(_layers.getObjectAtIndex(_enabledLayer)); return true; } @@ -1020,7 +1020,7 @@ bool LayerMultiplex::initWithArray(const Vector& arrayOfLayers) _layers.addObjectsFromArray(arrayOfLayers); _enabledLayer = 0; - this->addChild(_layers[_enabledLayer]); + this->addChild(_layers.getObjectAtIndex(_enabledLayer)); return true; } return false; @@ -1030,24 +1030,24 @@ void LayerMultiplex::switchTo(int n) { CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" ); - this->removeChild(_layers[_enabledLayer], true); + this->removeChild(_layers.getObjectAtIndex(_enabledLayer), true); _enabledLayer = n; - this->addChild(_layers[n]); + this->addChild(_layers.getObjectAtIndex(n)); } void LayerMultiplex::switchToAndReleaseMe(int n) { CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" ); - this->removeChild(_layers[_enabledLayer], true); + this->removeChild(_layers.getObjectAtIndex(_enabledLayer), true); _layers.replaceObjectAtIndex(_enabledLayer, nullptr); _enabledLayer = n; - this->addChild(_layers[n]); + this->addChild(_layers.getObjectAtIndex(n)); } NS_CC_END diff --git a/cocos/2d/CCMenuItem.cpp b/cocos/2d/CCMenuItem.cpp index 6e0b507e85..b6f98ea424 100644 --- a/cocos/2d/CCMenuItem.cpp +++ b/cocos/2d/CCMenuItem.cpp @@ -942,7 +942,7 @@ void MenuItemToggle::setSelectedIndex(unsigned int index) currentItem->removeFromParentAndCleanup(false); } - MenuItem* item = _subItems[_selectedIndex]; + MenuItem* item = _subItems.getObjectAtIndex(_selectedIndex); this->addChild(item, 0, kCurrentItem); Size s = item->getContentSize(); this->setContentSize(s); @@ -953,13 +953,13 @@ void MenuItemToggle::setSelectedIndex(unsigned int index) void MenuItemToggle::selected() { MenuItem::selected(); - _subItems[_selectedIndex]->selected(); + _subItems.getObjectAtIndex(_selectedIndex)->selected(); } void MenuItemToggle::unselected() { MenuItem::unselected(); - _subItems[_selectedIndex]->unselected(); + _subItems.getObjectAtIndex(_selectedIndex)->unselected(); } void MenuItemToggle::activate() @@ -986,7 +986,7 @@ void MenuItemToggle::setEnabled(bool enabled) MenuItem* MenuItemToggle::getSelectedItem() { - return _subItems[_selectedIndex]; + return _subItems.getObjectAtIndex(_selectedIndex); } NS_CC_END diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index fbba0e5df7..43cc219118 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -852,7 +852,7 @@ void Node::visit() } this->transform(); - int i = 0; + long i = 0; if(!_children.empty()) { @@ -860,7 +860,7 @@ void Node::visit() // draw children zOrder < 0 for( ; i < _children.count(); i++ ) { - auto node = _children[i]; + auto node = _children.getObjectAtIndex(i); if ( node && node->_ZOrder < 0 ) node->visit(); @@ -870,12 +870,17 @@ void Node::visit() // self draw this->draw(); - for( ; i < _children.count(); i++ ) - { - auto node = _children[i]; - if (node) - node->visit(); - } + // Uses std::for_each to improve performance. + std::for_each(_children.cbegin()+i, _children.cend(), [](Node* node){ + node->visit(); + }); + +// for( ; i < _children.count(); i++ ) +// { +// auto node = _children[i]; +// if (node) +// node->visit(); +// } } else { diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index 2d5b54c248..b3ecc28fa1 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -177,14 +177,14 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) CCASSERT( _blendFunc.src == child->getBlendFunc().src && _blendFunc.dst == child->getBlendFunc().dst, "Can't add a ParticleSystem that uses a different blending function"); //no lazy sorting, so don't call super addChild, call helper instead - unsigned int pos = addChildHelper(child,zOrder,tag); + long pos = addChildHelper(child,zOrder,tag); //get new atlasIndex int atlasIndex = 0; if (pos != 0) { - ParticleSystem* p = static_cast(_children[pos-1]); + ParticleSystem* p = static_cast(_children.getObjectAtIndex(pos-1)); atlasIndex = p->getAtlasIndex() + p->getTotalParticles(); } else @@ -267,7 +267,7 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) int newAtlasIndex = 0; for( int i=0;i < _children.count();i++) { - ParticleSystem* node = static_cast(_children[i]); + ParticleSystem* node = static_cast(_children.getObjectAtIndex(i)); if( node == child ) { newAtlasIndex = child->getAtlasIndex(); @@ -295,7 +295,7 @@ void ParticleBatchNode::getCurrentIndex(long* oldIndex, long* newIndex, Node* ch for( long i=0; i < count; i++ ) { - Node* pNode = _children[i]; + Node* pNode = _children.getObjectAtIndex(i); // new index if( pNode->getZOrder() > z && ! foundNewIdx ) @@ -342,7 +342,7 @@ long ParticleBatchNode::searchNewPositionInChildrenForZ(int z) for( long i=0; i < count; i++ ) { - Node *child = _children[i]; + Node *child = _children.getObjectAtIndex(i); if (child->getZOrder() > z) { return i; @@ -378,7 +378,7 @@ void ParticleBatchNode::removeChild(Node* aChild, bool cleanup) void ParticleBatchNode::removeChildAtIndex(unsigned int index, bool doCleanup) { - removeChild(_children[index],doCleanup); + removeChild(_children.getObjectAtIndex(index), doCleanup); } void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup) diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 867cc07d71..c8b328dcb0 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -1060,7 +1060,7 @@ void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, CCASSERT(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found"); - AnimationFrame* frame = a->getFrames()[frameIndex]; + AnimationFrame* frame = a->getFrames().getObjectAtIndex(frameIndex); CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame"); diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index b7bbab495f..c6ca01d03c 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -218,7 +218,7 @@ void SpriteBatchNode::removeChild(Node *child, bool cleanup) void SpriteBatchNode::removeChildAtIndex(int index, bool doCleanup) { CCASSERT(index>=0 && index < _children.count(), "Invalid index"); - removeChild(_children[index], doCleanup); + removeChild(_children.getObjectAtIndex(index), doCleanup); } void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup) @@ -309,7 +309,7 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex) { bool needNewIndex=true; - if (array[0]->getZOrder() >= 0) + if (array.getObjectAtIndex(0)->getZOrder() >= 0) { //all children are in front of the parent oldIndex = sprite->getAtlasIndex(); @@ -476,7 +476,7 @@ int SpriteBatchNode::lowestAtlasIndexInChild(Sprite *sprite) } else { - return lowestAtlasIndexInChild(static_cast(children[0])); + return lowestAtlasIndexInChild(static_cast(children.getObjectAtIndex(0))); } } @@ -490,7 +490,7 @@ int SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ) Sprite *prev = NULL; if (childIndex > 0 && childIndex != -1) { - prev = static_cast(siblings[childIndex - 1]); + prev = static_cast(siblings.getObjectAtIndex(childIndex - 1)); } // first child of the sprite sheet diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 3beb5b19e0..31554a3c8c 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -35,7 +35,6 @@ template class CC_DLL Vector { public: - Vector() : _data() { @@ -50,7 +49,8 @@ public: setCapacity(capacity); } - virtual ~Vector() { + virtual ~Vector() + { CCLOG("In the destructor of Vector."); removeAllObjects(); } @@ -58,34 +58,42 @@ public: Vector(const Vector& other) { CCLOG("In the copy constructor!"); - copy(other); + _data = other._data; + addRefForAllObjects(); } /** Move constructor */ Vector(Vector&& other) { CCLOG("In the move constructor of Vector!"); - _data = std::move(other._data); + _data = other._data; } Vector& operator=(const Vector& other) { CCLOG("In the copy assignment operator!"); - copy(other); + removeAllObjects(); + _data = other._data; + addRefForAllObjects(); return *this; } Vector& operator=(Vector&& other) { CCLOG("In the move assignment operator!"); - _data = std::move(other._data); + _data = other._data; return *this; } - T operator[](long index) const - { - return getObjectAtIndex(index); - } +// T& operator[](long index) +// { +// return _data[index]; +// } +// +// const T& operator[](long index) const +// { +// return _data[index]; +// } /** Sets capacity of current array */ void setCapacity(long capacity) @@ -99,16 +107,6 @@ public: return _data.capacity(); } - void copy(const Vector& other) - { - if (this == &other) - return; - - removeAllObjects(); - setCapacity(other.count()); - addObjectsFromArray(other); - } - // Querying an Array /** Returns element count of the array */ @@ -153,7 +151,12 @@ public: /** Returns a random element */ T getRandomObject() const { - return *_data.begin(); + if (!_data.empty()) + { + int randIdx = rand() % _data.size(); + return *(_data.begin() + randIdx); + } + return nullptr; } /** Returns a Boolean value that indicates whether object is present in array. */ @@ -204,6 +207,8 @@ public: /** sets a certain object at a certain index */ void setObject(T object, long index) { + CCASSERT(index >= 0 && index < count(), "Invalid index!"); + _data[index]->release(); _data[index] = object; object->retain(); } @@ -348,9 +353,17 @@ public: const_reverse_iterator crbegin() const { return _data.crbegin(); } const_reverse_iterator crend() const { return _data.crend(); } + protected: + + void addRefForAllObjects() + { + std::for_each(_data.begin(), _data.end(), [](T obj){ + obj->retain(); + }); + } + std::vector _data; - }; // end of data_structure group diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index 65c965276f..10dffd52b2 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -677,7 +677,7 @@ Bone *Armature::getBoneAtPoint(float x, float y) const for(long i = length - 1; i >= 0; i--) { - bs = static_cast( _children[i] ); + bs = static_cast( _children.getObjectAtIndex(i) ); if(bs->getDisplayManager()->containPoint(x, y)) { return bs;