fixed self assignment error and provide move semantics to pushBack of Vector<T>

This commit is contained in:
andyque 2014-01-02 17:54:52 +08:00
parent 8de6cb1981
commit 80adda2ce8
4 changed files with 111 additions and 90 deletions

View File

@ -316,18 +316,22 @@ public:
/** Copy assignment operator */
Map<K, V>& operator= ( const Map<K, V>& 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<K, V>& operator= ( Map<K, V>&& 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;
}

View File

@ -58,7 +58,9 @@ __String::~__String()
__String& __String::operator= (const __String& other)
{
_string = other._string;
if (this != &other) {
_string = other._string;
}
return *this;
}

View File

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

View File

@ -107,18 +107,22 @@ public:
/** Copy assignment operator */
Vector<T>& operator=(const Vector<T>& 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<T>& operator=(Vector<T>&& 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 );