From C++ best practices

Uses static_cast<> instead of C casting.
This commit is contained in:
Ricardo Quesada 2013-07-08 14:38:14 -07:00
parent cc545f1441
commit 9fe9341646
17 changed files with 65 additions and 68 deletions

View File

@ -276,7 +276,7 @@ bool FlipX::initWithFlipX(bool x) {
void FlipX::update(float time) {
CC_UNUSED_PARAM(time);
((Sprite*) (_target))->setFlipX(_flipX);
static_cast<Sprite*>(_target)->setFlipX(_flipX);
}
FlipX* FlipX::reverse() const
@ -334,7 +334,7 @@ bool FlipY::initWithFlipY(bool y) {
void FlipY::update(float time) {
CC_UNUSED_PARAM(time);
((Sprite*) (_target))->setFlipY(_flipY);
static_cast<Sprite*>(_target)->setFlipY(_flipY);
}
FlipY* FlipY::reverse() const

View File

@ -216,13 +216,13 @@ Sequence* Sequence::create(Array* arrayOfActions)
unsigned int count = arrayOfActions->count();
CC_BREAK_IF(count == 0);
FiniteTimeAction* prev = (FiniteTimeAction*)arrayOfActions->objectAtIndex(0);
FiniteTimeAction* prev = static_cast<FiniteTimeAction*>( arrayOfActions->objectAtIndex(0) );
if (count > 1)
{
for (unsigned int i = 1; i < count; ++i)
{
prev = createWithTwoActions(prev, (FiniteTimeAction*)arrayOfActions->objectAtIndex(i));
prev = createWithTwoActions(prev, static_cast<FiniteTimeAction*>( arrayOfActions->objectAtIndex(i)) );
}
}
else
@ -256,9 +256,7 @@ Sequence* Sequence::clone(void) const
{
// no copy constructor
auto a = new Sequence();
a->initWithTwoActions((FiniteTimeAction*)(_actions[0]->clone()),
(FiniteTimeAction*)(_actions[1]->clone())
);
a->initWithTwoActions(_actions[0]->clone(), _actions[1]->clone() );
a->autorelease();
return a;
}
@ -418,7 +416,7 @@ Repeat* Repeat::clone(void) const
{
// no copy constructor
auto a = new Repeat();
a->initWithAction((FiniteTimeAction*)_innerAction->clone(), _times );
a->initWithAction( _innerAction->clone(), _times );
a->autorelease();
return a;
}
@ -553,7 +551,7 @@ RepeatForever *RepeatForever::clone(void) const
{
// no copy constructor
auto a = new RepeatForever();
a->initWithAction((ActionInterval*)_innerAction->clone());
a->initWithAction(_innerAction->clone());
a->autorelease();
return a;
}
@ -658,12 +656,12 @@ Spawn* Spawn::create(Array *arrayOfActions)
{
unsigned int count = arrayOfActions->count();
CC_BREAK_IF(count == 0);
FiniteTimeAction* prev = (FiniteTimeAction*)arrayOfActions->objectAtIndex(0);
FiniteTimeAction* prev = static_cast<FiniteTimeAction*>( arrayOfActions->objectAtIndex(0) );
if (count > 1)
{
for (unsigned int i = 1; i < arrayOfActions->count(); ++i)
{
prev = createWithTwoActions(prev, (FiniteTimeAction*)arrayOfActions->objectAtIndex(i));
prev = createWithTwoActions(prev, static_cast<FiniteTimeAction*>( arrayOfActions->objectAtIndex(i)) );
}
}
else
@ -2506,6 +2504,7 @@ void ReverseTime::update(float time)
ReverseTime* ReverseTime::reverse() const
{
// XXX: This looks like a bug
return (ReverseTime*)_other->clone();
}
@ -2624,7 +2623,7 @@ void Animate::stop(void)
{
if (_animation->getRestoreOriginalFrame() && _target)
{
((Sprite*)(_target))->setDisplayFrame(_origFrame);
static_cast<Sprite*>(_target)->setDisplayFrame(_origFrame);
}
ActionInterval::stop();
@ -2655,9 +2654,9 @@ void Animate::update(float t)
float splitTime = _splitTimes->at(i);
if( splitTime <= t ) {
AnimationFrame* frame = (AnimationFrame*)frames->objectAtIndex(i);
AnimationFrame* frame = static_cast<AnimationFrame*>(frames->objectAtIndex(i));
frameToDisplay = frame->getSpriteFrame();
((Sprite*)_target)->setDisplayFrame(frameToDisplay);
static_cast<Sprite*>(_target)->setDisplayFrame(frameToDisplay);
Dictionary* dict = frame->getUserInfo();
if( dict )
@ -2685,7 +2684,7 @@ Animate* Animate::reverse() const
Object* pObj = NULL;
CCARRAY_FOREACH_REVERSE(pOldArray, pObj)
{
AnimationFrame* pElement = (AnimationFrame*)pObj;
AnimationFrame* pElement = static_cast<AnimationFrame*>(pObj);
if (! pElement)
{
break;

View File

@ -306,7 +306,7 @@ void Dictionary::removeObjectsForKeys(Array* pKeyArray)
Object* pObj = NULL;
CCARRAY_FOREACH(pKeyArray, pObj)
{
String* pStr = (String*)pObj;
String* pStr = static_cast<String*>(pObj);
removeObjectForKey(pStr->getCString());
}
}
@ -374,11 +374,11 @@ Object* Dictionary::randomObject()
if (_dictType == kDictInt)
{
return objectForKey(((Integer*)key)->getValue());
return objectForKey( static_cast<Integer*>(key)->getValue());
}
else if (_dictType == kDictStr)
{
return objectForKey(((String*)key)->getCString());
return objectForKey( static_cast<String*>(key)->getCString());
}
else
{
@ -398,7 +398,7 @@ Dictionary* Dictionary::create()
Dictionary* Dictionary::createWithDictionary(Dictionary* srcDict)
{
Dictionary* pNewDict = (Dictionary*)srcDict->copy();
Dictionary* pNewDict = static_cast<Dictionary*>( srcDict->copy() );
pNewDict->autorelease();
return pNewDict;
}

View File

@ -73,7 +73,7 @@ CCBMFontConfiguration* FNTConfigLoadFile( const char *fntFile)
s_pConfigurations = new Dictionary();
}
pRet = (CCBMFontConfiguration*)s_pConfigurations->objectForKey(fntFile);
pRet = static_cast<CCBMFontConfiguration*>( s_pConfigurations->objectForKey(fntFile) );
if( pRet == NULL )
{
pRet = CCBMFontConfiguration::create(fntFile);
@ -649,7 +649,7 @@ void LabelBMFont::createFontChars()
Sprite *fontChar;
bool hasSprite = true;
fontChar = (Sprite*)(this->getChildByTag(i));
fontChar = static_cast<Sprite*>( this->getChildByTag(i) );
if(fontChar )
{
// Reusing previous Sprite
@ -763,7 +763,7 @@ void LabelBMFont::setString(unsigned short *newString, bool needUpdateLabel)
Object* child;
CCARRAY_FOREACH(_children, child)
{
Node* pNode = (Node*) child;
Node* pNode = static_cast<Node*>( child );
if (pNode)
{
pNode->setVisible(false);
@ -847,7 +847,7 @@ void LabelBMFont::setOpacityModifyRGB(bool var)
Object* child;
CCARRAY_FOREACH(_children, child)
{
Node* pNode = (Node*) child;
Node* pNode = static_cast<Node*>( child );
if (pNode)
{
RGBAProtocol *pRGBAProtocol = dynamic_cast<RGBAProtocol*>(pNode);
@ -871,7 +871,7 @@ void LabelBMFont::updateDisplayedOpacity(GLubyte parentOpacity)
Object* pObj;
CCARRAY_FOREACH(_children, pObj)
{
Sprite *item = (Sprite*)pObj;
Sprite *item = static_cast<Sprite*>( pObj );
item->updateDisplayedOpacity(_displayedOpacity);
}
}
@ -885,7 +885,7 @@ void LabelBMFont::updateDisplayedColor(const Color3B& parentColor)
Object* pObj;
CCARRAY_FOREACH(_children, pObj)
{
Sprite *item = (Sprite*)pObj;
Sprite *item = static_cast<Sprite*>( pObj );
item->updateDisplayedColor(_displayedColor);
}
}
@ -946,7 +946,7 @@ void LabelBMFont::updateLabel()
Sprite* characterSprite;
unsigned int justSkipped = 0;
while (!(characterSprite = (Sprite*)this->getChildByTag(j + skip + justSkipped)))
while (!(characterSprite = static_cast<Sprite*>( this->getChildByTag(j + skip + justSkipped))) )
{
justSkipped++;
}
@ -1118,7 +1118,7 @@ void LabelBMFont::updateLabel()
int index = i + line_length - 1 + lineNumber;
if (index < 0) continue;
Sprite* lastChar = (Sprite*)getChildByTag(index);
Sprite* lastChar = static_cast<Sprite*>( getChildByTag(index) );
if ( lastChar == NULL )
continue;
@ -1144,7 +1144,7 @@ void LabelBMFont::updateLabel()
index = i + j + lineNumber;
if (index < 0) continue;
Sprite* characterSprite = (Sprite*)getChildByTag(index);
Sprite* characterSprite = static_cast<Sprite*>( getChildByTag(index) );
characterSprite->setPosition(ccpAdd(characterSprite->getPosition(), ccp(shift, 0.0f)));
}
}

View File

@ -45,7 +45,7 @@ static void setProgram(Node *n, GLProgram *p)
Object* pObj = NULL;
CCARRAY_FOREACH(n->getChildren(), pObj)
{
setProgram((Node*)pObj, p);
setProgram(static_cast<Node*>(pObj), p);
}
}

View File

@ -105,7 +105,7 @@ void BatchNode::draw()
}
else
{
((Node *)object)->visit();
static_cast<Node*>(object)->visit();
}
}

View File

@ -493,9 +493,9 @@ void SpeedTest::onEnter()
void SpeedTest::altertime(float dt)
{
Speed* action1 = (Speed*)(_grossini->getActionByTag(kTagAction1));
Speed* action2 = (Speed*)(_tamara->getActionByTag(kTagAction1));
Speed* action3 = (Speed*)(_kathia->getActionByTag(kTagAction1));
Speed* action1 = static_cast<Speed*>(_grossini->getActionByTag(kTagAction1));
Speed* action2 = static_cast<Speed*>(_tamara->getActionByTag(kTagAction1));
Speed* action3 = static_cast<Speed*>(_kathia->getActionByTag(kTagAction1));
action1->setSpeed( CCRANDOM_MINUS1_1() * 2 );
action2->setSpeed( CCRANDOM_MINUS1_1() * 2 );

View File

@ -1887,9 +1887,9 @@ std::string Issue1327::subtitle()
return "See console: You should see: 0, 45, 90, 135, 180";
}
void Issue1327::logSprRotation(Node* pSender)
void Issue1327::logSprRotation(Sprite* pSender)
{
CCLog("%f", ((Sprite*)pSender)->getRotation());
CCLog("%f", pSender->getRotation());
}
//Issue1398

View File

@ -440,7 +440,7 @@ public:
virtual void onEnter();
virtual std::string subtitle();
virtual std::string title();
void logSprRotation(Node* pSender);
void logSprRotation(Sprite* pSender);
};
class Issue1398 : public ActionsDemo

View File

@ -93,7 +93,7 @@ MenuLayerMainMenu::MenuLayerMainMenu()
if(pObject == NULL)
break;
child = (Node*)pObject;
child = static_cast<Node*>(pObject);
Point dstPoint = child->getPosition();
int offset = (int) (s.width/2 + 50);
@ -138,12 +138,12 @@ MenuLayerMainMenu::~MenuLayerMainMenu()
void MenuLayerMainMenu::menuCallback(Object* sender)
{
((LayerMultiplex*)_parent)->switchTo(1);
static_cast<LayerMultiplex*>(_parent)->switchTo(1);
}
void MenuLayerMainMenu::menuCallbackConfig(Object* sender)
{
((LayerMultiplex*)_parent)->switchTo(3);
static_cast<LayerMultiplex*>(_parent)->switchTo(3);
}
void MenuLayerMainMenu::allowTouches(float dt)
@ -165,17 +165,17 @@ void MenuLayerMainMenu::menuCallbackDisabled(Object* sender)
void MenuLayerMainMenu::menuCallback2(Object* sender)
{
((LayerMultiplex*)_parent)->switchTo(2);
static_cast<LayerMultiplex*>(_parent)->switchTo(2);
}
void MenuLayerMainMenu::menuCallbackPriorityTest(Object* pSender)
{
((LayerMultiplex*)_parent)->switchTo(4);
static_cast<LayerMultiplex*>(_parent)->switchTo(4);
}
void MenuLayerMainMenu::menuCallbackBugsTest(Object *pSender)
{
((LayerMultiplex*)_parent)->switchTo(5);
static_cast<LayerMultiplex*>(_parent)->switchTo(5);
}
void MenuLayerMainMenu::onQuit(Object* sender)
@ -186,7 +186,7 @@ void MenuLayerMainMenu::onQuit(Object* sender)
void MenuLayerMainMenu::menuMovingCallback(Object *pSender)
{
((LayerMultiplex*)_parent)->switchTo(6);
static_cast<LayerMultiplex*>(_parent)->switchTo(6);
}
//------------------------------------------------------------------
@ -230,7 +230,7 @@ void MenuLayer2::alignMenusH()
{
for(int i=0;i<2;i++)
{
Menu *menu = (Menu*)getChildByTag(100+i);
Menu *menu = static_cast<Menu*>( getChildByTag(100+i) );
menu->setPosition( _centeredMenu );
if(i==0)
{
@ -254,7 +254,7 @@ void MenuLayer2::alignMenusV()
{
for(int i=0;i<2;i++)
{
Menu *menu = (Menu*)getChildByTag(100+i);
Menu *menu = static_cast<Menu*>( getChildByTag(100+i) );
menu->setPosition( _centeredMenu );
if(i==0)
{
@ -275,12 +275,12 @@ void MenuLayer2::alignMenusV()
void MenuLayer2::menuCallback(Object* sender)
{
((LayerMultiplex*)_parent)->switchTo(0);
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
}
void MenuLayer2::menuCallbackOpacity(Object* sender)
{
Menu* menu = (Menu*)(((Node*)(sender))->getParent());
Menu* menu = static_cast<Menu*>( static_cast<Node*>(sender)->getParent() );
GLubyte opacity = menu->getOpacity();
if( opacity == 128 )
menu->setOpacity(255);
@ -315,7 +315,7 @@ MenuLayer3::MenuLayer3()
_disabledItem->stopAllActions();
});
MenuItemFont* item2 = MenuItemFont::create("--- Go Back ---", [&](Object *sender) {
((LayerMultiplex*)_parent)->switchTo(0);
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
});
Sprite *spriteNormal = Sprite::create(s_MenuItem, CCRectMake(0,23*2,115,23));
@ -454,7 +454,7 @@ void MenuLayer4::menuCallback(Object* sender)
void MenuLayer4::backCallback(Object* sender)
{
((LayerMultiplex*)_parent)->switchTo(0);
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
}
MenuLayerPriorityTest::MenuLayerPriorityTest()
@ -511,7 +511,7 @@ MenuLayerPriorityTest::~MenuLayerPriorityTest()
void MenuLayerPriorityTest::menuCallback(Object* pSender)
{
((LayerMultiplex*)_parent)->switchTo(0);
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
// [[Director sharedDirector] popScene];
}
@ -530,9 +530,9 @@ BugsTest::BugsTest()
menu->setPosition(ccp(s.width/2, s.height/2));
}
void BugsTest::issue1410MenuCallback(cocos2d::Object *pSender)
void BugsTest::issue1410MenuCallback(Object *sender)
{
Menu *menu = (Menu*)((MenuItem*)pSender)->getParent();
Menu *menu = static_cast<Menu*>( static_cast<Node*>(sender)->getParent() );
menu->setTouchEnabled(false);
menu->setTouchEnabled(true);

View File

@ -92,7 +92,7 @@ void MotionStreakTest2::onEnter()
void MotionStreakTest2::ccTouchesMoved(Set* touches, Event* event)
{
Touch* touch = (Touch*) touches->anyObject();
Touch* touch = static_cast<Touch*>( touches->anyObject() );
Point touchLocation = touch->getLocation();

View File

@ -204,7 +204,7 @@ Test4::Test4()
void Test4::delay2(float dt)
{
Sprite* node = (Sprite*)(getChildByTag(2));
Sprite* node = static_cast<Sprite*>(getChildByTag(2));
Action* action1 = RotateBy::create(1, 360);
node->runAction(action1);
}
@ -358,8 +358,8 @@ void StressTest1::shouldNotCrash(float dt)
Size s = Director::sharedDirector()->getWinSize();
// if the node has timers, it crashes
Node* explosion = ParticleSun::create();
((ParticleSun*)explosion)->setTexture(TextureCache::sharedTextureCache()->addImage("Images/fire.png"));
ParticleSun* explosion = ParticleSun::create();
explosion->setTexture(TextureCache::sharedTextureCache()->addImage("Images/fire.png"));
// if it doesn't, it works Ok.
// CocosNode *explosion = [Sprite create:@"grossinis_sister2.png");
@ -425,7 +425,7 @@ StressTest2::StressTest2()
void StressTest2::shouldNotLeak(float dt)
{
unschedule( schedule_selector(StressTest2::shouldNotLeak) );
Layer* sublayer = (Layer*)getChildByTag(kTagSprite1);
Layer* sublayer = static_cast<Layer*>( getChildByTag(kTagSprite1) );
sublayer->removeAllChildrenWithCleanup(true);
}
@ -753,9 +753,7 @@ ConvertToNode::ConvertToNode()
point->setPosition(sprite->getPosition());
RepeatForever* copy = (RepeatForever*) action->copy();
copy->autorelease();
sprite->runAction(copy);
sprite->runAction( action->clone() );
addChild(sprite, i);
}
}
@ -764,7 +762,7 @@ void ConvertToNode::ccTouchesEnded(Set* touches, Event *event)
{
for( SetIterator it = touches->begin(); it != touches->end(); ++it)
{
Touch* touch = (Touch*)(*it);
Touch* touch = static_cast<Touch*>(*it);
Point location = touch->getLocation();
for( int i = 0; i < 3; i++)

View File

@ -178,7 +178,7 @@ RenderTextureSave::~RenderTextureSave()
void RenderTextureSave::ccTouchesMoved(Set* touches, Event* event)
{
Touch *touch = (Touch *)touches->anyObject();
Touch *touch = static_cast<Touch*>( touches->anyObject() );
Point start = touch->getLocation();
Point end = touch->getPreviousLocation();

View File

@ -699,7 +699,7 @@ void SchedulerUpdate::removeUpdates(float dt)
Object* pObject;
CCARRAY_FOREACH(children, pObject)
{
pNode = (Node*)pObject;
pNode = static_cast<Node*>(pObject);
if (! pNode)
{
@ -876,7 +876,7 @@ ControlSlider* SchedulerTimeScale::sliderCtl()
void SchedulerTimeScale::sliderAction(Object* pSender, ControlEvent controlEvent)
{
ControlSlider* pSliderCtl = (ControlSlider*)pSender;
ControlSlider* pSliderCtl = static_cast<ControlSlider*>(pSender);
float scale;
scale = pSliderCtl->getValue();
@ -970,7 +970,7 @@ void TwoSchedulers::sliderAction(Object* sender, ControlEvent controlEvent)
{
float scale;
ControlSlider *slider = (ControlSlider*) sender;
ControlSlider *slider = static_cast<ControlSlider*>(sender);
scale = slider->getValue();
if( sender == sliderCtl1 )

View File

@ -684,7 +684,7 @@ void ShaderRetroEffect::update(float dt)
Object* pObj = NULL;
CCARRAY_FOREACH(pArray, pObj)
{
Sprite *sprite = (Sprite*)pObj;
Sprite *sprite = static_cast<Sprite*>(pObj);
i++;
Point oldPosition = sprite->getPosition();
sprite->setPosition(ccp( oldPosition.x, sinf( _accum * 2 + i/2.0) * 20 ));

View File

@ -1 +1 @@
68cd6c48fec5f50c0f98d427b8e677fb956a41eb
7883471eada76dffb69780d3e2a2d39d61ae4725

View File

@ -1495,7 +1495,7 @@ void TextureAsync::loadImages(float dt)
void TextureAsync::imageLoaded(Object* pObj)
{
Texture2D* tex = (Texture2D*)pObj;
Texture2D* tex = static_cast<Texture2D*>(pObj);
Director *director = Director::sharedDirector();
//CCAssert( [NSThread currentThread] == [director runningThread], @"FAIL. Callback should be on cocos2d thread");