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
Родитель cb215bc931
Коммит 9928e8bef1
10 изменённых файлов: 77 добавлений и 59 удалений

Просмотреть файл

@ -206,13 +206,13 @@ Sequence* Sequence::create(const Vector<FiniteTimeAction*>& 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<FiniteTimeAction*>& 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<Sprite*>(_target)->setDisplayFrame(frameToDisplay);

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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<Layer*>& 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

Просмотреть файл

@ -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

Просмотреть файл

@ -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
{

Просмотреть файл

@ -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<ParticleSystem*>(_children[pos-1]);
ParticleSystem* p = static_cast<ParticleSystem*>(_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<ParticleSystem*>(_children[i]);
ParticleSystem* node = static_cast<ParticleSystem*>(_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)

Просмотреть файл

@ -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");

Просмотреть файл

@ -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<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;
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

Просмотреть файл

@ -35,7 +35,6 @@ template<class T>
class CC_DLL Vector
{
public:
Vector<T>()
: _data()
{
@ -50,7 +49,8 @@ public:
setCapacity(capacity);
}
virtual ~Vector<T>() {
virtual ~Vector<T>()
{
CCLOG("In the destructor of Vector.");
removeAllObjects();
}
@ -58,34 +58,42 @@ public:
Vector<T>(const Vector<T>& other)
{
CCLOG("In the copy constructor!");
copy(other);
_data = other._data;
addRefForAllObjects();
}
/** Move constructor */
Vector<T>(Vector<T>&& other)
{
CCLOG("In the move constructor of Vector!");
_data = std::move(other._data);
_data = other._data;
}
Vector<T>& operator=(const Vector<T>& other)
{
CCLOG("In the copy assignment operator!");
copy(other);
removeAllObjects();
_data = other._data;
addRefForAllObjects();
return *this;
}
Vector<T>& operator=(Vector<T>&& 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<T>& 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<T> _data;
};
// end of data_structure group

Просмотреть файл

@ -677,7 +677,7 @@ Bone *Armature::getBoneAtPoint(float x, float y) const
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))
{
return bs;