From 80adda2ce81979d425dbe65106c901b4c37d87c0 Mon Sep 17 00:00:00 2001 From: andyque Date: Thu, 2 Jan 2014 17:54:52 +0800 Subject: [PATCH] fixed self assignment error and provide move semantics to pushBack of Vector --- cocos/base/CCMap.h | 16 +++-- cocos/base/CCString.cpp | 4 +- cocos/base/CCValue.cpp | 156 ++++++++++++++++++++-------------------- cocos/base/CCVector.h | 25 +++++-- 4 files changed, 111 insertions(+), 90 deletions(-) diff --git a/cocos/base/CCMap.h b/cocos/base/CCMap.h index 44c7846076..3f42bdc847 100644 --- a/cocos/base/CCMap.h +++ b/cocos/base/CCMap.h @@ -316,18 +316,22 @@ public: /** Copy assignment operator */ Map& operator= ( const Map& other ) { - CCLOGINFO("In the copy assignment operator of Map!"); - clear(); - _data = other._data; - addRefForAllObjects(); + if (this != &other) { + CCLOGINFO("In the copy assignment operator of Map!"); + clear(); + _data = other._data; + addRefForAllObjects(); + } return *this; } /** Move assignment operator */ Map& operator= ( Map&& other ) { - CCLOGINFO("In the move assignment operator of Map!"); - _data = std::move(other._data); + if (this != &other) { + CCLOGINFO("In the move assignment operator of Map!"); + _data = std::move(other._data); + } return *this; } diff --git a/cocos/base/CCString.cpp b/cocos/base/CCString.cpp index a8643372a0..8714861da4 100644 --- a/cocos/base/CCString.cpp +++ b/cocos/base/CCString.cpp @@ -58,7 +58,9 @@ __String::~__String() __String& __String::operator= (const __String& other) { - _string = other._string; + if (this != &other) { + _string = other._string; + } return *this; } diff --git a/cocos/base/CCValue.cpp b/cocos/base/CCValue.cpp index ac63402a50..e2a6112716 100644 --- a/cocos/base/CCValue.cpp +++ b/cocos/base/CCValue.cpp @@ -178,89 +178,93 @@ Value::~Value() Value& Value::operator= (const Value& other) { - switch (other._type) { - case Type::BYTE: - _baseData.byteVal = other._baseData.byteVal; - break; - case Type::INTEGER: - _baseData.intVal = other._baseData.intVal; - break; - case Type::FLOAT: - _baseData.floatVal = other._baseData.floatVal; - break; - case Type::DOUBLE: - _baseData.doubleVal = other._baseData.doubleVal; - break; - case Type::BOOLEAN: - _baseData.boolVal = other._baseData.boolVal; - break; - case Type::STRING: - _strData = other._strData; - break; - case Type::VECTOR: - if (_vectorData == nullptr) - _vectorData = new ValueVector(); - *_vectorData = *other._vectorData; - break; - case Type::MAP: - if (_mapData == nullptr) - _mapData = new ValueMap(); - *_mapData = *other._mapData; - break; - case Type::INT_KEY_MAP: - if (_intKeyMapData == nullptr) - _intKeyMapData = new ValueMapIntKey(); - *_intKeyMapData = *other._intKeyMapData; - break; - default: - break; + if (this != &other) { + switch (other._type) { + case Type::BYTE: + _baseData.byteVal = other._baseData.byteVal; + break; + case Type::INTEGER: + _baseData.intVal = other._baseData.intVal; + break; + case Type::FLOAT: + _baseData.floatVal = other._baseData.floatVal; + break; + case Type::DOUBLE: + _baseData.doubleVal = other._baseData.doubleVal; + break; + case Type::BOOLEAN: + _baseData.boolVal = other._baseData.boolVal; + break; + case Type::STRING: + _strData = other._strData; + break; + case Type::VECTOR: + if (_vectorData == nullptr) + _vectorData = new ValueVector(); + *_vectorData = *other._vectorData; + break; + case Type::MAP: + if (_mapData == nullptr) + _mapData = new ValueMap(); + *_mapData = *other._mapData; + break; + case Type::INT_KEY_MAP: + if (_intKeyMapData == nullptr) + _intKeyMapData = new ValueMapIntKey(); + *_intKeyMapData = *other._intKeyMapData; + break; + default: + break; + } + _type = other._type; } - _type = other._type; return *this; } Value& Value::operator= (Value&& other) { - switch (other._type) { - case Type::BYTE: - _baseData.byteVal = other._baseData.byteVal; - break; - case Type::INTEGER: - _baseData.intVal = other._baseData.intVal; - break; - case Type::FLOAT: - _baseData.floatVal = other._baseData.floatVal; - break; - case Type::DOUBLE: - _baseData.doubleVal = other._baseData.doubleVal; - break; - case Type::BOOLEAN: - _baseData.boolVal = other._baseData.boolVal; - break; - case Type::STRING: - _strData = other._strData; - break; - case Type::VECTOR: - CC_SAFE_DELETE(_vectorData); - _vectorData = other._vectorData; - break; - case Type::MAP: - CC_SAFE_DELETE(_mapData); - _mapData = other._mapData; - break; - case Type::INT_KEY_MAP: - CC_SAFE_DELETE(_intKeyMapData); - _intKeyMapData = other._intKeyMapData; - break; - default: - break; + if (this != &other) { + switch (other._type) { + case Type::BYTE: + _baseData.byteVal = other._baseData.byteVal; + break; + case Type::INTEGER: + _baseData.intVal = other._baseData.intVal; + break; + case Type::FLOAT: + _baseData.floatVal = other._baseData.floatVal; + break; + case Type::DOUBLE: + _baseData.doubleVal = other._baseData.doubleVal; + break; + case Type::BOOLEAN: + _baseData.boolVal = other._baseData.boolVal; + break; + case Type::STRING: + _strData = other._strData; + break; + case Type::VECTOR: + CC_SAFE_DELETE(_vectorData); + _vectorData = other._vectorData; + break; + case Type::MAP: + CC_SAFE_DELETE(_mapData); + _mapData = other._mapData; + break; + case Type::INT_KEY_MAP: + CC_SAFE_DELETE(_intKeyMapData); + _intKeyMapData = other._intKeyMapData; + break; + default: + break; + } + _type = other._type; + + other._vectorData = nullptr; + other._mapData = nullptr; + other._intKeyMapData = nullptr; + other._type = Type::NONE; } - _type = other._type; - - other._vectorData = nullptr; - other._mapData = nullptr; - other._intKeyMapData = nullptr; - other._type = Type::NONE; return *this; } diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 5fd07e4dc7..bbf578930b 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -107,18 +107,22 @@ public: /** Copy assignment operator */ Vector& operator=(const Vector& other) { - CCLOGINFO("In the copy assignment operator!"); - clear(); - _data = other._data; - addRefForAllObjects(); + if (this != &other) { + CCLOGINFO("In the copy assignment operator!"); + clear(); + _data = other._data; + addRefForAllObjects(); + } return *this; } /** Move assignment operator */ Vector& operator=(Vector&& other) { - CCLOGINFO("In the move assignment operator!"); - _data = std::move(other._data); + if (this != &other) { + CCLOGINFO("In the move assignment operator!"); + _data = std::move(other._data); + } return *this; } @@ -260,7 +264,14 @@ public: * which causes an automatic reallocation of the allocated storage space * if -and only if- the new vector size surpasses the current vector capacity. */ - void pushBack(T object) + void pushBack(const T& object) + { + CCASSERT(object != nullptr, "The object should not be nullptr"); + _data.push_back( object ); + object->retain(); + } + + void pushBack(const T&& object) { CCASSERT(object != nullptr, "The object should not be nullptr"); _data.push_back( object );