Fix move assignment operator

This commit is contained in:
Vasilii Rogin 2023-05-15 22:59:42 +03:00
parent d01f04e48a
commit d084b720f9
1 changed files with 44 additions and 47 deletions

View File

@ -77,7 +77,7 @@ private:
float floatValue; float floatValue;
char* stringValue; char* stringValue;
void* pointerValue; void* pointerValue;
}; } value;
void init_from_string(const char* str, int sLen) void init_from_string(const char* str, int sLen)
{ {
@ -86,50 +86,47 @@ private:
if (sLen == -1) sLen = strlen(str); if (sLen == -1) sLen = strlen(str);
if (sLen >= ARRAY_MAX_STRING) sLen = ARRAY_MAX_STRING - 1; // memory safety if (sLen >= ARRAY_MAX_STRING) sLen = ARRAY_MAX_STRING - 1; // memory safety
stringValue = (char*)malloc(sLen + 1); value.stringValue = (char*)malloc(sLen + 1);
memcpy(stringValue, str, sLen); memcpy(value.stringValue, str, sLen);
stringValue[sLen] = '\0'; value.stringValue[sLen] = '\0';
}
void init_from_another(ArrayElement&& other)
{
// Maybe this can be done simpler way?
type = other.type;
switch (type) {
case ArrayElementType::INT:
integerValue = other.integerValue;
break;
case ArrayElementType::FLOAT:
floatValue = other.floatValue;
break;
case ArrayElementType::POINTER:
pointerValue = other.pointerValue;
break;
case ArrayElementType::STRING:
stringValue = other.stringValue;
other.stringValue = nullptr;
break;
default:
throw(std::exception());
}
} }
public: public:
ArrayElement() ArrayElement()
: type(ArrayElementType::INT) : type(ArrayElementType::INT)
, integerValue(0) { , value(
// nothing here { integerValue : 0 }) {
// Nothing here
}; };
ArrayElement(const ArrayElement& other) = delete; ArrayElement(const ArrayElement& other) = delete;
ArrayElement& operator=(const ArrayElement& rhs) = delete; ArrayElement& operator=(const ArrayElement& rhs) = delete;
ArrayElement(ArrayElement&& other) ArrayElement(ArrayElement&& other)
{ {
init_from_another(std::move(other)); // Maybe this can be done simpler way?
type = other.type;
switch (type) {
case ArrayElementType::INT:
value.integerValue = other.value.integerValue;
break;
case ArrayElementType::FLOAT:
value.floatValue = other.value.floatValue;
break;
case ArrayElementType::POINTER:
value.pointerValue = other.value.pointerValue;
break;
case ArrayElementType::STRING:
value.stringValue = other.value.stringValue;
other.value.stringValue = nullptr;
break;
default:
throw(std::exception());
}
}; };
ArrayElement& operator=(ArrayElement&& other) ArrayElement& operator=(ArrayElement&& other)
{ {
init_from_another(std::move(other)); std::swap(type, other.type);
std::swap(value, other.value);
return *this; return *this;
} }
@ -138,15 +135,15 @@ public:
switch (programValue.opcode) { switch (programValue.opcode) {
case VALUE_TYPE_INT: case VALUE_TYPE_INT:
type = ArrayElementType::INT; type = ArrayElementType::INT;
integerValue = programValue.integerValue; value.integerValue = programValue.integerValue;
break; break;
case VALUE_TYPE_FLOAT: case VALUE_TYPE_FLOAT:
type = ArrayElementType::FLOAT; type = ArrayElementType::FLOAT;
floatValue = programValue.floatValue; value.floatValue = programValue.floatValue;
break; break;
case VALUE_TYPE_PTR: case VALUE_TYPE_PTR:
type = ArrayElementType::POINTER; type = ArrayElementType::POINTER;
pointerValue = programValue.pointerValue; value.pointerValue = programValue.pointerValue;
break; break;
case VALUE_TYPE_STRING: case VALUE_TYPE_STRING:
case VALUE_TYPE_DYNAMIC_STRING: case VALUE_TYPE_DYNAMIC_STRING:
@ -171,19 +168,19 @@ public:
switch (type) { switch (type) {
case ArrayElementType::INT: case ArrayElementType::INT:
out.opcode = VALUE_TYPE_INT; out.opcode = VALUE_TYPE_INT;
out.integerValue = integerValue; out.integerValue = value.integerValue;
return out; return out;
case ArrayElementType::FLOAT: case ArrayElementType::FLOAT:
out.opcode = VALUE_TYPE_FLOAT; out.opcode = VALUE_TYPE_FLOAT;
out.floatValue = floatValue; out.floatValue = value.floatValue;
return out; return out;
case ArrayElementType::POINTER: case ArrayElementType::POINTER:
out.opcode = VALUE_TYPE_PTR; out.opcode = VALUE_TYPE_PTR;
out.pointerValue = pointerValue; out.pointerValue = value.pointerValue;
return out; return out;
case ArrayElementType::STRING: case ArrayElementType::STRING:
out.opcode = VALUE_TYPE_DYNAMIC_STRING; out.opcode = VALUE_TYPE_DYNAMIC_STRING;
out.integerValue = programPushString(program, stringValue); out.integerValue = programPushString(program, value.stringValue);
return out; return out;
default: default:
throw(std::exception()); throw(std::exception());
@ -197,13 +194,13 @@ public:
}; };
switch (type) { switch (type) {
case ArrayElementType::INT: case ArrayElementType::INT:
return integerValue < other.integerValue; return value.integerValue < other.value.integerValue;
case ArrayElementType::FLOAT: case ArrayElementType::FLOAT:
return floatValue < other.floatValue; return value.floatValue < other.value.floatValue;
case ArrayElementType::POINTER: case ArrayElementType::POINTER:
return pointerValue < other.pointerValue; return value.pointerValue < other.value.pointerValue;
case ArrayElementType::STRING: case ArrayElementType::STRING:
return strcmp(stringValue, other.stringValue) < 0; return strcmp(value.stringValue, other.value.stringValue) < 0;
default: default:
throw(std::exception()); throw(std::exception());
} }
@ -215,13 +212,13 @@ public:
}; };
switch (type) { switch (type) {
case ArrayElementType::INT: case ArrayElementType::INT:
return integerValue == other.integerValue; return value.integerValue == other.value.integerValue;
case ArrayElementType::FLOAT: case ArrayElementType::FLOAT:
return floatValue == other.floatValue; return value.floatValue == other.value.floatValue;
case ArrayElementType::POINTER: case ArrayElementType::POINTER:
return pointerValue == other.pointerValue; return value.pointerValue == other.value.pointerValue;
case ArrayElementType::STRING: case ArrayElementType::STRING:
return strcmp(stringValue, other.stringValue) == 0; return strcmp(value.stringValue, other.value.stringValue) == 0;
default: default:
throw(std::exception()); throw(std::exception());
} }
@ -230,7 +227,7 @@ public:
~ArrayElement() ~ArrayElement()
{ {
if (type == ArrayElementType::STRING) { if (type == ArrayElementType::STRING) {
free(stringValue); free(value.stringValue);
}; };
} }
}; };