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 */ /** Copy assignment operator */
Map<K, V>& operator= ( const Map<K, V>& other ) Map<K, V>& operator= ( const Map<K, V>& other )
{ {
CCLOGINFO("In the copy assignment operator of Map!"); if (this != &other) {
clear(); CCLOGINFO("In the copy assignment operator of Map!");
_data = other._data; clear();
addRefForAllObjects(); _data = other._data;
addRefForAllObjects();
}
return *this; return *this;
} }
/** Move assignment operator */ /** Move assignment operator */
Map<K, V>& operator= ( Map<K, V>&& other ) Map<K, V>& operator= ( Map<K, V>&& other )
{ {
CCLOGINFO("In the move assignment operator of Map!"); if (this != &other) {
_data = std::move(other._data); CCLOGINFO("In the move assignment operator of Map!");
_data = std::move(other._data);
}
return *this; return *this;
} }

View File

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

View File

@ -178,89 +178,93 @@ Value::~Value()
Value& Value::operator= (const Value& other) Value& Value::operator= (const Value& other)
{ {
switch (other._type) { if (this != &other) {
case Type::BYTE: switch (other._type) {
_baseData.byteVal = other._baseData.byteVal; case Type::BYTE:
break; _baseData.byteVal = other._baseData.byteVal;
case Type::INTEGER: break;
_baseData.intVal = other._baseData.intVal; case Type::INTEGER:
break; _baseData.intVal = other._baseData.intVal;
case Type::FLOAT: break;
_baseData.floatVal = other._baseData.floatVal; case Type::FLOAT:
break; _baseData.floatVal = other._baseData.floatVal;
case Type::DOUBLE: break;
_baseData.doubleVal = other._baseData.doubleVal; case Type::DOUBLE:
break; _baseData.doubleVal = other._baseData.doubleVal;
case Type::BOOLEAN: break;
_baseData.boolVal = other._baseData.boolVal; case Type::BOOLEAN:
break; _baseData.boolVal = other._baseData.boolVal;
case Type::STRING: break;
_strData = other._strData; case Type::STRING:
break; _strData = other._strData;
case Type::VECTOR: break;
if (_vectorData == nullptr) case Type::VECTOR:
_vectorData = new ValueVector(); if (_vectorData == nullptr)
*_vectorData = *other._vectorData; _vectorData = new ValueVector();
break; *_vectorData = *other._vectorData;
case Type::MAP: break;
if (_mapData == nullptr) case Type::MAP:
_mapData = new ValueMap(); if (_mapData == nullptr)
*_mapData = *other._mapData; _mapData = new ValueMap();
break; *_mapData = *other._mapData;
case Type::INT_KEY_MAP: break;
if (_intKeyMapData == nullptr) case Type::INT_KEY_MAP:
_intKeyMapData = new ValueMapIntKey(); if (_intKeyMapData == nullptr)
*_intKeyMapData = *other._intKeyMapData; _intKeyMapData = new ValueMapIntKey();
break; *_intKeyMapData = *other._intKeyMapData;
default: break;
break; default:
break;
}
_type = other._type;
} }
_type = other._type;
return *this; return *this;
} }
Value& Value::operator= (Value&& other) Value& Value::operator= (Value&& other)
{ {
switch (other._type) { if (this != &other) {
case Type::BYTE: switch (other._type) {
_baseData.byteVal = other._baseData.byteVal; case Type::BYTE:
break; _baseData.byteVal = other._baseData.byteVal;
case Type::INTEGER: break;
_baseData.intVal = other._baseData.intVal; case Type::INTEGER:
break; _baseData.intVal = other._baseData.intVal;
case Type::FLOAT: break;
_baseData.floatVal = other._baseData.floatVal; case Type::FLOAT:
break; _baseData.floatVal = other._baseData.floatVal;
case Type::DOUBLE: break;
_baseData.doubleVal = other._baseData.doubleVal; case Type::DOUBLE:
break; _baseData.doubleVal = other._baseData.doubleVal;
case Type::BOOLEAN: break;
_baseData.boolVal = other._baseData.boolVal; case Type::BOOLEAN:
break; _baseData.boolVal = other._baseData.boolVal;
case Type::STRING: break;
_strData = other._strData; case Type::STRING:
break; _strData = other._strData;
case Type::VECTOR: break;
CC_SAFE_DELETE(_vectorData); case Type::VECTOR:
_vectorData = other._vectorData; CC_SAFE_DELETE(_vectorData);
break; _vectorData = other._vectorData;
case Type::MAP: break;
CC_SAFE_DELETE(_mapData); case Type::MAP:
_mapData = other._mapData; CC_SAFE_DELETE(_mapData);
break; _mapData = other._mapData;
case Type::INT_KEY_MAP: break;
CC_SAFE_DELETE(_intKeyMapData); case Type::INT_KEY_MAP:
_intKeyMapData = other._intKeyMapData; CC_SAFE_DELETE(_intKeyMapData);
break; _intKeyMapData = other._intKeyMapData;
default: break;
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; return *this;
} }

View File

@ -107,18 +107,22 @@ public:
/** Copy assignment operator */ /** Copy assignment operator */
Vector<T>& operator=(const Vector<T>& other) Vector<T>& operator=(const Vector<T>& other)
{ {
CCLOGINFO("In the copy assignment operator!"); if (this != &other) {
clear(); CCLOGINFO("In the copy assignment operator!");
_data = other._data; clear();
addRefForAllObjects(); _data = other._data;
addRefForAllObjects();
}
return *this; return *this;
} }
/** Move assignment operator */ /** Move assignment operator */
Vector<T>& operator=(Vector<T>&& other) Vector<T>& operator=(Vector<T>&& other)
{ {
CCLOGINFO("In the move assignment operator!"); if (this != &other) {
_data = std::move(other._data); CCLOGINFO("In the move assignment operator!");
_data = std::move(other._data);
}
return *this; return *this;
} }
@ -260,7 +264,14 @@ public:
* which causes an automatic reallocation of the allocated storage space * which causes an automatic reallocation of the allocated storage space
* if -and only if- the new vector size surpasses the current vector capacity. * 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"); CCASSERT(object != nullptr, "The object should not be nullptr");
_data.push_back( object ); _data.push_back( object );