issue #2790: Uses Vector<T>::getObjectAtIndex instead of Vector<T>[] operator to avoid some mistakes.

This commit is contained in:
James Chen 2013-11-29 16:33:15 +08:00
parent cb215bc931
commit 9928e8bef1
10 changed files with 77 additions and 59 deletions

View File

@ -206,13 +206,13 @@ Sequence* Sequence::create(const Vector<FiniteTimeAction*>& arrayOfActions)
long count = arrayOfActions.count(); long count = arrayOfActions.count();
CC_BREAK_IF(count == 0); CC_BREAK_IF(count == 0);
auto prev = arrayOfActions[0]; auto prev = arrayOfActions.getObjectAtIndex(0);
if (count > 1) if (count > 1)
{ {
for (long i = 1; i < count; ++i) for (long i = 1; i < count; ++i)
{ {
prev = createWithTwoActions(prev, arrayOfActions[i]); prev = createWithTwoActions(prev, arrayOfActions.getObjectAtIndex(i));
} }
} }
else else
@ -578,12 +578,12 @@ Spawn* Spawn::create(const Vector<FiniteTimeAction*>& arrayOfActions)
{ {
long count = arrayOfActions.count(); long count = arrayOfActions.count();
CC_BREAK_IF(count == 0); CC_BREAK_IF(count == 0);
auto prev = arrayOfActions[0]; auto prev = arrayOfActions.getObjectAtIndex(0);
if (count > 1) if (count > 1)
{ {
for (int i = 1; i < arrayOfActions.count(); ++i) for (int i = 1; i < arrayOfActions.count(); ++i)
{ {
prev = createWithTwoActions(prev, arrayOfActions[i]); prev = createWithTwoActions(prev, arrayOfActions.getObjectAtIndex(i));
} }
} }
else else
@ -2104,7 +2104,7 @@ void Animate::update(float t)
float splitTime = _splitTimes->at(i); float splitTime = _splitTimes->at(i);
if( splitTime <= t ) { if( splitTime <= t ) {
AnimationFrame* frame = frames[i]; AnimationFrame* frame = frames.getObjectAtIndex(i);
frameToDisplay = frame->getSpriteFrame(); frameToDisplay = frame->getSpriteFrame();
static_cast<Sprite*>(_target)->setDisplayFrame(frameToDisplay); static_cast<Sprite*>(_target)->setDisplayFrame(frameToDisplay);

View File

@ -200,7 +200,7 @@ void EventDispatcher::visitTarget(Node* node)
// visit children zOrder < 0 // visit children zOrder < 0
for( ; i < childrenCount; i++ ) for( ; i < childrenCount; i++ )
{ {
child = children[i]; child = children.getObjectAtIndex(i);
if ( child && child->getZOrder() < 0 ) if ( child && child->getZOrder() < 0 )
visitTarget(child); visitTarget(child);
@ -212,7 +212,7 @@ void EventDispatcher::visitTarget(Node* node)
for( ; i < childrenCount; i++ ) for( ; i < childrenCount; i++ )
{ {
child = children[i]; child = children.getObjectAtIndex(i);
if (child) if (child)
visitTarget(child); visitTarget(child);
} }

View File

@ -1005,7 +1005,7 @@ bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
} }
_enabledLayer = 0; _enabledLayer = 0;
this->addChild(_layers[_enabledLayer]); this->addChild(_layers.getObjectAtIndex(_enabledLayer));
return true; return true;
} }
@ -1020,7 +1020,7 @@ bool LayerMultiplex::initWithArray(const Vector<Layer*>& arrayOfLayers)
_layers.addObjectsFromArray(arrayOfLayers); _layers.addObjectsFromArray(arrayOfLayers);
_enabledLayer = 0; _enabledLayer = 0;
this->addChild(_layers[_enabledLayer]); this->addChild(_layers.getObjectAtIndex(_enabledLayer));
return true; return true;
} }
return false; return false;
@ -1030,24 +1030,24 @@ void LayerMultiplex::switchTo(int n)
{ {
CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" ); CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" );
this->removeChild(_layers[_enabledLayer], true); this->removeChild(_layers.getObjectAtIndex(_enabledLayer), true);
_enabledLayer = n; _enabledLayer = n;
this->addChild(_layers[n]); this->addChild(_layers.getObjectAtIndex(n));
} }
void LayerMultiplex::switchToAndReleaseMe(int n) void LayerMultiplex::switchToAndReleaseMe(int n)
{ {
CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" ); 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); _layers.replaceObjectAtIndex(_enabledLayer, nullptr);
_enabledLayer = n; _enabledLayer = n;
this->addChild(_layers[n]); this->addChild(_layers.getObjectAtIndex(n));
} }
NS_CC_END NS_CC_END

View File

@ -942,7 +942,7 @@ void MenuItemToggle::setSelectedIndex(unsigned int index)
currentItem->removeFromParentAndCleanup(false); currentItem->removeFromParentAndCleanup(false);
} }
MenuItem* item = _subItems[_selectedIndex]; MenuItem* item = _subItems.getObjectAtIndex(_selectedIndex);
this->addChild(item, 0, kCurrentItem); this->addChild(item, 0, kCurrentItem);
Size s = item->getContentSize(); Size s = item->getContentSize();
this->setContentSize(s); this->setContentSize(s);
@ -953,13 +953,13 @@ void MenuItemToggle::setSelectedIndex(unsigned int index)
void MenuItemToggle::selected() void MenuItemToggle::selected()
{ {
MenuItem::selected(); MenuItem::selected();
_subItems[_selectedIndex]->selected(); _subItems.getObjectAtIndex(_selectedIndex)->selected();
} }
void MenuItemToggle::unselected() void MenuItemToggle::unselected()
{ {
MenuItem::unselected(); MenuItem::unselected();
_subItems[_selectedIndex]->unselected(); _subItems.getObjectAtIndex(_selectedIndex)->unselected();
} }
void MenuItemToggle::activate() void MenuItemToggle::activate()
@ -986,7 +986,7 @@ void MenuItemToggle::setEnabled(bool enabled)
MenuItem* MenuItemToggle::getSelectedItem() MenuItem* MenuItemToggle::getSelectedItem()
{ {
return _subItems[_selectedIndex]; return _subItems.getObjectAtIndex(_selectedIndex);
} }
NS_CC_END NS_CC_END

View File

@ -852,7 +852,7 @@ void Node::visit()
} }
this->transform(); this->transform();
int i = 0; long i = 0;
if(!_children.empty()) if(!_children.empty())
{ {
@ -860,7 +860,7 @@ void Node::visit()
// draw children zOrder < 0 // draw children zOrder < 0
for( ; i < _children.count(); i++ ) for( ; i < _children.count(); i++ )
{ {
auto node = _children[i]; auto node = _children.getObjectAtIndex(i);
if ( node && node->_ZOrder < 0 ) if ( node && node->_ZOrder < 0 )
node->visit(); node->visit();
@ -870,12 +870,17 @@ void Node::visit()
// self draw // self draw
this->draw(); this->draw();
for( ; i < _children.count(); i++ ) // Uses std::for_each to improve performance.
{ std::for_each(_children.cbegin()+i, _children.cend(), [](Node* node){
auto node = _children[i];
if (node)
node->visit(); node->visit();
} });
// for( ; i < _children.count(); i++ )
// {
// auto node = _children[i];
// if (node)
// node->visit();
// }
} }
else else
{ {

View File

@ -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"); 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 //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 //get new atlasIndex
int atlasIndex = 0; int atlasIndex = 0;
if (pos != 0) if (pos != 0)
{ {
ParticleSystem* p = static_cast<ParticleSystem*>(_children[pos-1]); ParticleSystem* p = static_cast<ParticleSystem*>(_children.getObjectAtIndex(pos-1));
atlasIndex = p->getAtlasIndex() + p->getTotalParticles(); atlasIndex = p->getAtlasIndex() + p->getTotalParticles();
} }
else else
@ -267,7 +267,7 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder)
int newAtlasIndex = 0; int newAtlasIndex = 0;
for( int i=0;i < _children.count();i++) for( int i=0;i < _children.count();i++)
{ {
ParticleSystem* node = static_cast<ParticleSystem*>(_children[i]); ParticleSystem* node = static_cast<ParticleSystem*>(_children.getObjectAtIndex(i));
if( node == child ) if( node == child )
{ {
newAtlasIndex = child->getAtlasIndex(); newAtlasIndex = child->getAtlasIndex();
@ -295,7 +295,7 @@ void ParticleBatchNode::getCurrentIndex(long* oldIndex, long* newIndex, Node* ch
for( long i=0; i < count; i++ ) for( long i=0; i < count; i++ )
{ {
Node* pNode = _children[i]; Node* pNode = _children.getObjectAtIndex(i);
// new index // new index
if( pNode->getZOrder() > z && ! foundNewIdx ) if( pNode->getZOrder() > z && ! foundNewIdx )
@ -342,7 +342,7 @@ long ParticleBatchNode::searchNewPositionInChildrenForZ(int z)
for( long i=0; i < count; i++ ) for( long i=0; i < count; i++ )
{ {
Node *child = _children[i]; Node *child = _children.getObjectAtIndex(i);
if (child->getZOrder() > z) if (child->getZOrder() > z)
{ {
return i; return i;
@ -378,7 +378,7 @@ void ParticleBatchNode::removeChild(Node* aChild, bool cleanup)
void ParticleBatchNode::removeChildAtIndex(unsigned int index, bool doCleanup) void ParticleBatchNode::removeChildAtIndex(unsigned int index, bool doCleanup)
{ {
removeChild(_children[index],doCleanup); removeChild(_children.getObjectAtIndex(index), doCleanup);
} }
void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup) void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup)

View File

@ -1060,7 +1060,7 @@ void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName,
CCASSERT(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found"); CCASSERT(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found");
AnimationFrame* frame = a->getFrames()[frameIndex]; AnimationFrame* frame = a->getFrames().getObjectAtIndex(frameIndex);
CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame"); CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame");

View File

@ -218,7 +218,7 @@ void SpriteBatchNode::removeChild(Node *child, bool cleanup)
void SpriteBatchNode::removeChildAtIndex(int index, bool doCleanup) void SpriteBatchNode::removeChildAtIndex(int index, bool doCleanup)
{ {
CCASSERT(index>=0 && index < _children.count(), "Invalid index"); CCASSERT(index>=0 && index < _children.count(), "Invalid index");
removeChild(_children[index], doCleanup); removeChild(_children.getObjectAtIndex(index), doCleanup);
} }
void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup) void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
@ -309,7 +309,7 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex)
{ {
bool needNewIndex=true; bool needNewIndex=true;
if (array[0]->getZOrder() >= 0) if (array.getObjectAtIndex(0)->getZOrder() >= 0)
{ {
//all children are in front of the parent //all children are in front of the parent
oldIndex = sprite->getAtlasIndex(); oldIndex = sprite->getAtlasIndex();
@ -476,7 +476,7 @@ int SpriteBatchNode::lowestAtlasIndexInChild(Sprite *sprite)
} }
else else
{ {
return lowestAtlasIndexInChild(static_cast<Sprite*>(children[0])); return lowestAtlasIndexInChild(static_cast<Sprite*>(children.getObjectAtIndex(0)));
} }
} }
@ -490,7 +490,7 @@ int SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ)
Sprite *prev = NULL; Sprite *prev = NULL;
if (childIndex > 0 && childIndex != -1) if (childIndex > 0 && childIndex != -1)
{ {
prev = static_cast<Sprite*>(siblings[childIndex - 1]); prev = static_cast<Sprite*>(siblings.getObjectAtIndex(childIndex - 1));
} }
// first child of the sprite sheet // first child of the sprite sheet

View File

@ -35,7 +35,6 @@ template<class T>
class CC_DLL Vector class CC_DLL Vector
{ {
public: public:
Vector<T>() Vector<T>()
: _data() : _data()
{ {
@ -50,7 +49,8 @@ public:
setCapacity(capacity); setCapacity(capacity);
} }
virtual ~Vector<T>() { virtual ~Vector<T>()
{
CCLOG("In the destructor of Vector."); CCLOG("In the destructor of Vector.");
removeAllObjects(); removeAllObjects();
} }
@ -58,34 +58,42 @@ public:
Vector<T>(const Vector<T>& other) Vector<T>(const Vector<T>& other)
{ {
CCLOG("In the copy constructor!"); CCLOG("In the copy constructor!");
copy(other); _data = other._data;
addRefForAllObjects();
} }
/** Move constructor */ /** Move constructor */
Vector<T>(Vector<T>&& other) Vector<T>(Vector<T>&& other)
{ {
CCLOG("In the move constructor of Vector!"); CCLOG("In the move constructor of Vector!");
_data = std::move(other._data); _data = other._data;
} }
Vector<T>& operator=(const Vector<T>& other) Vector<T>& operator=(const Vector<T>& other)
{ {
CCLOG("In the copy assignment operator!"); CCLOG("In the copy assignment operator!");
copy(other); removeAllObjects();
_data = other._data;
addRefForAllObjects();
return *this; return *this;
} }
Vector<T>& operator=(Vector<T>&& other) Vector<T>& operator=(Vector<T>&& other)
{ {
CCLOG("In the move assignment operator!"); CCLOG("In the move assignment operator!");
_data = std::move(other._data); _data = other._data;
return *this; return *this;
} }
T operator[](long index) const // T& operator[](long index)
{ // {
return getObjectAtIndex(index); // return _data[index];
} // }
//
// const T& operator[](long index) const
// {
// return _data[index];
// }
/** Sets capacity of current array */ /** Sets capacity of current array */
void setCapacity(long capacity) void setCapacity(long capacity)
@ -99,16 +107,6 @@ public:
return _data.capacity(); return _data.capacity();
} }
void copy(const Vector<T>& other)
{
if (this == &other)
return;
removeAllObjects();
setCapacity(other.count());
addObjectsFromArray(other);
}
// Querying an Array // Querying an Array
/** Returns element count of the array */ /** Returns element count of the array */
@ -153,7 +151,12 @@ public:
/** Returns a random element */ /** Returns a random element */
T getRandomObject() const 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. */ /** 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 */ /** sets a certain object at a certain index */
void setObject(T object, long index) void setObject(T object, long index)
{ {
CCASSERT(index >= 0 && index < count(), "Invalid index!");
_data[index]->release();
_data[index] = object; _data[index] = object;
object->retain(); object->retain();
} }
@ -348,9 +353,17 @@ public:
const_reverse_iterator crbegin() const { return _data.crbegin(); } const_reverse_iterator crbegin() const { return _data.crbegin(); }
const_reverse_iterator crend() const { return _data.crend(); } const_reverse_iterator crend() const { return _data.crend(); }
protected:
std::vector<T> _data;
protected:
void addRefForAllObjects()
{
std::for_each(_data.begin(), _data.end(), [](T obj){
obj->retain();
});
}
std::vector<T> _data;
}; };
// end of data_structure group // end of data_structure group

View File

@ -677,7 +677,7 @@ Bone *Armature::getBoneAtPoint(float x, float y) const
for(long i = length - 1; i >= 0; i--) for(long i = length - 1; i >= 0; i--)
{ {
bs = static_cast<Bone*>( _children[i] ); bs = static_cast<Bone*>( _children.getObjectAtIndex(i) );
if(bs->getDisplayManager()->containPoint(x, y)) if(bs->getDisplayManager()->containPoint(x, y))
{ {
return bs; return bs;