Skip to content

Commit

Permalink
issue #1564: Return a new object instead of 'const reference' for som…
Browse files Browse the repository at this point in the history
…e get methods.
  • Loading branch information
James Chen committed Nov 20, 2012
1 parent 4d39ecf commit b8b2af0
Show file tree
Hide file tree
Showing 43 changed files with 109 additions and 157 deletions.
4 changes: 2 additions & 2 deletions cocos2dx/base_nodes/CCAtlasNode.cpp
Expand Up @@ -123,7 +123,7 @@ bool CCAtlasNode::initWithTileFile(const char *tile, unsigned int tileWidth, uns

void CCAtlasNode::calculateMaxItems()
{
const CCSize& s = m_pTextureAtlas->getTexture()->getContentSize();
CCSize s = m_pTextureAtlas->getTexture()->getContentSize();
m_uItemsPerColumn = (int)(s.height / m_uItemHeight);
m_uItemsPerRow = (int)(s.width / m_uItemWidth);
}
Expand All @@ -148,7 +148,7 @@ void CCAtlasNode::draw(void)

// CCAtlasNode - RGBA protocol

const ccColor3B& CCAtlasNode:: getColor()
ccColor3B CCAtlasNode:: getColor()
{
if(m_bIsOpacityModifyRGB)
{
Expand Down
8 changes: 4 additions & 4 deletions cocos2dx/base_nodes/CCNode.cpp
Expand Up @@ -265,7 +265,7 @@ void CCNode::setScaleY(float newScaleY)
}

/// position getter
const CCPoint& CCNode::getPosition()
CCPoint CCNode::getPosition()
{
return m_obPosition;
}
Expand Down Expand Up @@ -363,13 +363,13 @@ void CCNode::setVisible(bool var)
m_bVisible = var;
}

const CCPoint& CCNode::getAnchorPointInPoints()
CCPoint CCNode::getAnchorPointInPoints()
{
return m_obAnchorPointInPoints;
}

/// anchorPoint getter
const CCPoint& CCNode::getAnchorPoint()
CCPoint CCNode::getAnchorPoint()
{
return m_obAnchorPoint;
}
Expand All @@ -385,7 +385,7 @@ void CCNode::setAnchorPoint(const CCPoint& point)
}

/// contentSize getter
const CCSize & CCNode::getContentSize()
CCSize CCNode::getContentSize()
{
return m_obContentSize;
}
Expand Down
8 changes: 4 additions & 4 deletions cocos2dx/base_nodes/CCNode.h
Expand Up @@ -237,7 +237,7 @@ class CC_DLL CCNode : public CCObject
virtual void setScaleY(float fScaleY);

/** Position (x,y) of the node in OpenGL coordinates. (0,0) is the left-bottom corner. */
virtual const CCPoint& getPosition();
virtual CCPoint getPosition();
virtual void setPosition(const CCPoint &position);

/** The X skew angle of the node in degrees.
Expand Down Expand Up @@ -319,20 +319,20 @@ class CC_DLL CCNode : public CCObject
The default anchorPoint is (0.5,0.5), so it starts in the center of the node.
@since v0.8
*/
virtual const CCPoint& getAnchorPoint();
virtual CCPoint getAnchorPoint();
virtual void setAnchorPoint(const CCPoint &anchorPoint);

/** The anchorPoint in absolute pixels.
Since v0.8 you can only read it. If you wish to modify it, use anchorPoint instead
*/
virtual const CCPoint& getAnchorPointInPoints();
virtual CCPoint getAnchorPointInPoints();

/** The untransformed size of the node.
The contentSize remains the same no matter the node is scaled or rotated.
All nodes has a size. Layer and Scene has the same size of the screen.
@since v0.8
*/
virtual const CCSize& getContentSize();
virtual CCSize getContentSize();
virtual void setContentSize(const CCSize &contentSize);

virtual bool isVisible();
Expand Down
4 changes: 2 additions & 2 deletions cocos2dx/effects/CCGrid.cpp
Expand Up @@ -99,7 +99,7 @@ bool CCGridBase::initWithSize(const ccGridSize& gridSize, CCTexture2D *pTexture,
CC_SAFE_RETAIN(m_pTexture);
m_bIsTextureFlipped = bFlipped;

const CCSize& texSize = m_pTexture->getContentSize();
CCSize texSize = m_pTexture->getContentSize();
m_obStep.x = texSize.width / m_sGridSize.x;
m_obStep.y = texSize.height / m_sGridSize.y;

Expand Down Expand Up @@ -229,7 +229,7 @@ void CCGridBase::afterDraw(cocos2d::CCNode *pTarget)

if (pTarget->getCamera()->isDirty())
{
const CCPoint& offset = pTarget->getAnchorPointInPoints();
CCPoint offset = pTarget->getAnchorPointInPoints();

//
// XXX: Camera should be applied in the AnchorPoint
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/include/CCProtocols.h
Expand Up @@ -44,7 +44,7 @@ class CC_DLL CCRGBAProtocol
/** returns the color
@since v0.8
*/
virtual const ccColor3B& getColor(void) = 0;
virtual ccColor3B getColor(void) = 0;

// returns the opacity
virtual GLubyte getOpacity(void) = 0;
Expand Down
11 changes: 0 additions & 11 deletions cocos2dx/include/cocos2d.h
Expand Up @@ -245,17 +245,6 @@ THE SOFTWARE.
#include "CCDirector.h"
#include "CCScheduler.h"

// Physics integration
#if CC_ENABLE_CHIPMUNK_INTEGRATION
#include "physics_nodes/CCPhysicsDebugNode.h"
#include "physics_nodes/CCPhysicsSprite.h"
#endif

#if CC_ENABLE_BOX2D_INTEGRATION
#include "physics_nodes/CCPhysicsDebugNode.h"
#include "physics_nodes/CCPhysicsSprite.h"
#endif

NS_CC_BEGIN

CC_DLL const char* cocos2dVersion();
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/label_nodes/CCLabelAtlas.cpp
Expand Up @@ -207,7 +207,7 @@ void CCLabelAtlas::draw()
{
CCAtlasNode::draw();

const CCSize& s = this->getContentSize();
CCSize s = this->getContentSize();
CCPoint vertices[4]={
ccp(0,0),ccp(s.width,0),
ccp(s.width,s.height),ccp(0,s.height),
Expand Down
8 changes: 5 additions & 3 deletions cocos2dx/label_nodes/CCLabelBMFont.cpp
Expand Up @@ -243,7 +243,7 @@ static void cc_utf8_trim_ws(std::vector<unsigned short>* str)
*
* Return value: the length of the string in characters
**/
static long
long
cc_utf8_strlen (const char * p, int max)
{
long len = 0;
Expand Down Expand Up @@ -1081,10 +1081,12 @@ void CCLabelBMFont::setColor(const ccColor3B& var)
}
}
}
const ccColor3B& CCLabelBMFont::getColor()

ccColor3B CCLabelBMFont::getColor()
{
return m_tColor;
}

void CCLabelBMFont::setOpacity(GLubyte var)
{
m_cOpacity = var;
Expand Down Expand Up @@ -1459,7 +1461,7 @@ const char* CCLabelBMFont::getFntFile()
void CCLabelBMFont::draw()
{
CCSpriteBatchNode::draw();
const CCSize& s = this->getContentSize();
CCSize s = this->getContentSize();
CCPoint vertices[4]={
ccp(0,0),ccp(s.width,0),
ccp(s.width,s.height),ccp(0,s.height),
Expand Down
8 changes: 4 additions & 4 deletions cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp
Expand Up @@ -479,7 +479,7 @@ void CCLayerColor::setOpacity(GLubyte var)
}

/// color getter
const ccColor3B& CCLayerColor::getColor()
ccColor3B CCLayerColor::getColor()
{
return m_tColor;
}
Expand Down Expand Up @@ -789,7 +789,7 @@ void CCLayerGradient::updateColor()
m_pSquareColors[3].a = E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c));
}

const ccColor3B& CCLayerGradient::getStartColor()
ccColor3B CCLayerGradient::getStartColor()
{
return m_tColor;
}
Expand All @@ -805,7 +805,7 @@ void CCLayerGradient::setEndColor(const ccColor3B& color)
updateColor();
}

const ccColor3B& CCLayerGradient::getEndColor()
ccColor3B CCLayerGradient::getEndColor()
{
return m_endColor;
}
Expand Down Expand Up @@ -838,7 +838,7 @@ void CCLayerGradient::setVector(const CCPoint& var)
updateColor();
}

const CCPoint& CCLayerGradient::getVector()
CCPoint CCLayerGradient::getVector()
{
return m_AlongVector;
}
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/menu_nodes/CCMenu.cpp
Expand Up @@ -632,7 +632,7 @@ void CCMenu::setColor(const ccColor3B& var)
}
}

const ccColor3B& CCMenu::getColor(void)
ccColor3B CCMenu::getColor(void)
{
return m_tColor;
}
Expand Down
12 changes: 7 additions & 5 deletions cocos2dx/menu_nodes/CCMenuItem.cpp
Expand Up @@ -158,7 +158,7 @@ void CCMenuItem::setTarget(CCObject *rec, SEL_MenuHandler selector)
//CCMenuItemLabel
//

const ccColor3B& CCMenuItemLabel::getDisabledColor()
ccColor3B CCMenuItemLabel::getDisabledColor()
{
return m_tDisabledColor;
}
Expand Down Expand Up @@ -311,7 +311,7 @@ void CCMenuItemLabel::setColor(const ccColor3B& color)
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(color);
}

const ccColor3B& CCMenuItemLabel::getColor()
ccColor3B CCMenuItemLabel::getColor()
{
return dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getColor();
}
Expand Down Expand Up @@ -573,7 +573,7 @@ GLubyte CCMenuItemSprite::getOpacity()
return dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->getOpacity();
}

const ccColor3B& CCMenuItemSprite::getColor()
ccColor3B CCMenuItemSprite::getColor()
{
return dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->getColor();
}
Expand Down Expand Up @@ -936,7 +936,7 @@ void CCMenuItemToggle::setSelectedIndex(unsigned int index)

CCMenuItem* item = (CCMenuItem*)m_pSubItems->objectAtIndex(m_uSelectedIndex);
this->addChild(item, 0, kCurrentItem);
const CCSize& s = item->getContentSize();
CCSize s = item->getContentSize();
this->setContentSize(s);
item->setPosition( ccp( s.width/2, s.height/2 ) );
}
Expand Down Expand Up @@ -1007,10 +1007,12 @@ void CCMenuItemToggle::setOpacity(GLubyte opacity)
}
}
}
const ccColor3B& CCMenuItemToggle::getColor()

ccColor3B CCMenuItemToggle::getColor()
{
return m_tColor;
}

void CCMenuItemToggle::setColor(const ccColor3B& color)
{
m_tColor = color;
Expand Down
4 changes: 2 additions & 2 deletions cocos2dx/menu_nodes/CCMenuItem.h
Expand Up @@ -154,7 +154,7 @@ class CC_DLL CCMenuItemLabel : public CCMenuItem, public CCRGBAProtocol
virtual void setOpacity(GLubyte opacity);
virtual GLubyte getOpacity();
virtual void setColor(const ccColor3B& color);
virtual const ccColor3B& getColor();
virtual ccColor3B getColor();

virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
Expand Down Expand Up @@ -294,7 +294,7 @@ class CC_DLL CCMenuItemSprite : public CCMenuItem, public CCRGBAProtocol
bool initWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
// super methods
virtual void setColor(const ccColor3B& color);
virtual const ccColor3B& getColor();
virtual ccColor3B getColor();
virtual void setOpacity(GLubyte opacity);
virtual GLubyte getOpacity();

Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/misc_nodes/CCMotionStreak.cpp
Expand Up @@ -195,7 +195,7 @@ void CCMotionStreak::setColor(const ccColor3B& color)
m_tColor = color;
}

const ccColor3B& CCMotionStreak::getColor(void)
ccColor3B CCMotionStreak::getColor(void)
{
return m_tColor;
}
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/misc_nodes/CCMotionStreak.h
Expand Up @@ -81,7 +81,7 @@ class CC_DLL CCMotionStreak : public CCNode, public CCTextureProtocol, public CC
virtual void setBlendFunc(ccBlendFunc blendFunc);
virtual ccBlendFunc getBlendFunc(void);
virtual void setColor(const ccColor3B& color);
virtual const ccColor3B& getColor(void);
virtual ccColor3B getColor(void);
virtual GLubyte getOpacity(void);
virtual void setOpacity(GLubyte opacity);
virtual void setOpacityModifyRGB(bool bValue);
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/misc_nodes/CCProgressTimer.cpp
Expand Up @@ -160,7 +160,7 @@ void CCProgressTimer::setColor(const ccColor3B& color)
updateColor();
}

const ccColor3B& CCProgressTimer::getColor(void)
ccColor3B CCProgressTimer::getColor(void)
{
return m_pSprite->getColor();
}
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/misc_nodes/CCProgressTimer.h
Expand Up @@ -77,7 +77,7 @@ class CC_DLL CCProgressTimer : public CCNode, public CCRGBAProtocol
void setAnchorPoint(CCPoint anchorPoint);

virtual void setColor(const ccColor3B& color);
virtual const ccColor3B& getColor(void);
virtual ccColor3B getColor(void);
virtual GLubyte getOpacity(void);
virtual void setOpacity(GLubyte opacity);
virtual void setOpacityModifyRGB(bool bValue);
Expand Down
4 changes: 2 additions & 2 deletions cocos2dx/misc_nodes/CCRenderTexture.cpp
Expand Up @@ -381,12 +381,12 @@ void CCRenderTexture::beginWithClear(float r, float g, float b, float a)

void CCRenderTexture::beginWithClear(float r, float g, float b, float a, float depthValue)
{
beginWithClear(r, g, b, a, depthValue, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
beginWithClear(r, g, b, a, depthValue, 0, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}

void CCRenderTexture::beginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue)
{
beginWithClear(r, g, b, a, depthValue, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
beginWithClear(r, g, b, a, depthValue, stencilValue, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
}

void CCRenderTexture::beginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue, GLbitfield flags)
Expand Down
12 changes: 6 additions & 6 deletions cocos2dx/particle_nodes/CCParticleSystem.cpp
Expand Up @@ -1025,7 +1025,7 @@ void CCParticleSystem::setDuration(float var)
m_fDuration = var;
}

const CCPoint& CCParticleSystem::getSourcePosition()
CCPoint CCParticleSystem::getSourcePosition()
{
return m_tSourcePosition;
}
Expand All @@ -1035,7 +1035,7 @@ void CCParticleSystem::setSourcePosition(const CCPoint& var)
m_tSourcePosition = var;
}

const CCPoint& CCParticleSystem::getPosVar()
CCPoint CCParticleSystem::getPosVar()
{
return m_tPosVar;
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ void CCParticleSystem::setEndSizeVar(float var)
m_fEndSizeVar = var;
}

const ccColor4F& CCParticleSystem::getStartColor()
ccColor4F CCParticleSystem::getStartColor()
{
return m_tStartColor;
}
Expand All @@ -1135,7 +1135,7 @@ void CCParticleSystem::setStartColor(const ccColor4F& var)
m_tStartColor = var;
}

const ccColor4F& CCParticleSystem::getStartColorVar()
ccColor4F CCParticleSystem::getStartColorVar()
{
return m_tStartColorVar;
}
Expand All @@ -1145,7 +1145,7 @@ void CCParticleSystem::setStartColorVar(const ccColor4F& var)
m_tStartColorVar = var;
}

const ccColor4F& CCParticleSystem::getEndColor()
ccColor4F CCParticleSystem::getEndColor()
{
return m_tEndColor;
}
Expand All @@ -1155,7 +1155,7 @@ void CCParticleSystem::setEndColor(const ccColor4F& var)
m_tEndColor = var;
}

const ccColor4F& CCParticleSystem::getEndColorVar()
ccColor4F CCParticleSystem::getEndColorVar()
{
return m_tEndColorVar;
}
Expand Down
2 changes: 1 addition & 1 deletion cocos2dx/particle_nodes/CCParticleSystemQuad.cpp
Expand Up @@ -210,7 +210,7 @@ void CCParticleSystemQuad::setTextureWithRect(CCTexture2D *texture, const CCRect
}
void CCParticleSystemQuad::setTexture(CCTexture2D* texture)
{
const CCSize& s = texture->getContentSize();
CCSize s = texture->getContentSize();
this->setTextureWithRect(texture, CCRectMake(0, 0, s.width, s.height));
}
void CCParticleSystemQuad::setDisplayFrame(CCSpriteFrame *spriteFrame)
Expand Down

11 comments on commit b8b2af0

@moadib
Copy link
Contributor

@moadib moadib commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you replace returning of const reference to returning of copy?

@minggo
Copy link
Contributor

@minggo minggo commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is easy to return a reference to a temple variable, which we met many times. So it is more safe to return a copy. I know it will take more memory, but it is not called frequently.

So i think it is not so bad.
May be i am wrong.

@dumganhar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @moadib,
It's because this interface is virtual, and it returns a reference of member variable, but it's subclass may return a temporary variable, it will cause crashes. And it's a dangerous operation if returning reference when the function is a virtual function.

@moadib
Copy link
Contributor

@moadib moadib commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anytime when you try return reference to local or temporary object c++ compiler gives you C4172 warning, isn't it?

Forewarned is forearmed. We talk about performance-critical applications so such things important. Of course I can continue to support our internal version of cocos2d-x without this stuff, but I want to cocos2dx was better.

Also as i can see CCPoint still inherited from CCObject. If this ugly inheritance needed only for CatMull-splines maybe you must surround it with I_WANT_USE_CATMULL which must be named I_WANT_USE_CATMULL_AND_WARNED_ABOUT_PERFORMANCE__LOSS_AND_MEMORY_CONSUMPTION in fact?

Same i can suggest for script-support-specific variables in CCObject.

@minggo
Copy link
Contributor

@minggo minggo commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moadib
I think you are right. We should change them back.
Thank you.

About the variable in CCObject. It is better to use macro to control it. Because cocos2d-x windows project is shared for all samples. So it is hard to use macro.
Any idea?

@minggo
Copy link
Contributor

@minggo minggo commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moadib
Copy link
Contributor

@moadib moadib commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe then

#ifndef I_DONT_NEED_SCRIPT_SUPPORT
unsigned int        m_uID;
int                 m_nLuaID;
#endif

and I_DONT_NEED_CATMULL_SPLINES?

Then user can set this defines for his project and all tests and samples will work as usual.

@minggo Thanks! I have a dream that one day I can use cocos2d-x unchanged. And that day is getting closer and closer.

@dumganhar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On windows, the libcocos2dx is shared for TestCpp, TestJavascript, HelloLua. And the macro defined in TestJavascript will not affect the configuration of libcocos2dx.
It could be done by adding libcocos2dx-lua, libcocos2dx-js, but there will be three engine probjects.

@moadib
Copy link
Contributor

@moadib moadib commented on b8b2af0 Jan 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is something to think about...

@dumganhar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is something to think about...

Hi @moadib, let me know if you find the way. Thanks.

@dumganhar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @moadib
I have reverted this commit here(#1838).
Thanks.

Please sign in to comment.