Fix #534, Make unsigned char same with char.

a. char store as int
b. unsigned char store as unsigned int
This commit is contained in:
halx99 2021-11-09 12:08:37 +08:00
parent 5e0b99103b
commit 879c91e1ee
2 changed files with 19 additions and 54 deletions

View File

@ -45,9 +45,9 @@ Value::Value() : _type(Type::NONE)
memset(&_field, 0, sizeof(_field)); memset(&_field, 0, sizeof(_field));
} }
Value::Value(unsigned char v) : _type(Type::BYTE) Value::Value(unsigned char v) : _type(Type::INTEGER)
{ {
_field.byteVal = v; _field.uintVal = v;
} }
Value::Value(int v) : _type(Type::INTEGER) Value::Value(int v) : _type(Type::INTEGER)
@ -57,7 +57,7 @@ Value::Value(int v) : _type(Type::INTEGER)
Value::Value(unsigned int v) : _type(Type::UNSIGNED) Value::Value(unsigned int v) : _type(Type::UNSIGNED)
{ {
_field.unsignedVal = v; _field.uintVal = v;
} }
Value::Value(float v) : _type(Type::FLOAT) Value::Value(float v) : _type(Type::FLOAT)
@ -143,14 +143,11 @@ Value& Value::operator=(const Value& other)
switch (other._type) switch (other._type)
{ {
case Type::BYTE:
_field.byteVal = other._field.byteVal;
break;
case Type::INTEGER: case Type::INTEGER:
_field.intVal = other._field.intVal; _field.intVal = other._field.intVal;
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
_field.unsignedVal = other._field.unsignedVal; _field.uintVal = other._field.uintVal;
break; break;
case Type::FLOAT: case Type::FLOAT:
_field.floatVal = other._field.floatVal; _field.floatVal = other._field.floatVal;
@ -203,14 +200,11 @@ Value& Value::operator=(Value&& other)
clear(); clear();
switch (other._type) switch (other._type)
{ {
case Type::BYTE:
_field.byteVal = other._field.byteVal;
break;
case Type::INTEGER: case Type::INTEGER:
_field.intVal = other._field.intVal; _field.intVal = other._field.intVal;
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
_field.unsignedVal = other._field.unsignedVal; _field.uintVal = other._field.uintVal;
break; break;
case Type::FLOAT: case Type::FLOAT:
_field.floatVal = other._field.floatVal; _field.floatVal = other._field.floatVal;
@ -247,8 +241,8 @@ Value& Value::operator=(Value&& other)
Value& Value::operator=(unsigned char v) Value& Value::operator=(unsigned char v)
{ {
reset(Type::BYTE); reset(Type::UNSIGNED);
_field.byteVal = v; _field.uintVal = v;
return *this; return *this;
} }
@ -262,7 +256,7 @@ Value& Value::operator=(int v)
Value& Value::operator=(unsigned int v) Value& Value::operator=(unsigned int v)
{ {
reset(Type::UNSIGNED); reset(Type::UNSIGNED);
_field.unsignedVal = v; _field.uintVal = v;
return *this; return *this;
} }
@ -374,12 +368,10 @@ bool Value::operator==(const Value& v) const
return true; return true;
switch (_type) switch (_type)
{ {
case Type::BYTE:
return v._field.byteVal == this->_field.byteVal;
case Type::INTEGER: case Type::INTEGER:
return v._field.intVal == this->_field.intVal; return v._field.intVal == this->_field.intVal;
case Type::UNSIGNED: case Type::UNSIGNED:
return v._field.unsignedVal == this->_field.unsignedVal; return v._field.uintVal == this->_field.uintVal;
case Type::BOOLEAN: case Type::BOOLEAN:
return v._field.boolVal == this->_field.boolVal; return v._field.boolVal == this->_field.boolVal;
case Type::STRING: case Type::STRING:
@ -447,13 +439,11 @@ unsigned char Value::asByte(unsigned char defaultValue) const
switch (_type) switch (_type)
{ {
case Type::BYTE:
return _field.byteVal;
case Type::INTEGER: case Type::INTEGER:
return static_cast<unsigned char>(_field.intVal); return static_cast<unsigned char>(_field.intVal);
case Type::UNSIGNED: case Type::UNSIGNED:
return static_cast<unsigned char>(_field.unsignedVal); return static_cast<unsigned char>(_field.uintVal);
case Type::STRING: case Type::STRING:
return static_cast<unsigned char>(atoi(_field.strVal->c_str())); return static_cast<unsigned char>(atoi(_field.strVal->c_str()));
@ -482,11 +472,8 @@ int Value::asInt(int defaultValue) const
return _field.intVal; return _field.intVal;
case Type::UNSIGNED: case Type::UNSIGNED:
CCASSERT(_field.unsignedVal < INT_MAX, "Can only convert values < INT_MAX"); CCASSERT(_field.uintVal < INT_MAX, "Can only convert values < INT_MAX");
return (int)_field.unsignedVal; return (int)_field.uintVal;
case Type::BYTE:
return _field.byteVal;
case Type::STRING: case Type::STRING:
return atoi(_field.strVal->c_str()); return atoi(_field.strVal->c_str());
@ -512,15 +499,12 @@ unsigned int Value::asUnsignedInt(unsigned int defaultValue) const
switch (_type) switch (_type)
{ {
case Type::UNSIGNED: case Type::UNSIGNED:
return _field.unsignedVal; return _field.uintVal;
case Type::INTEGER: case Type::INTEGER:
CCASSERT(_field.intVal >= 0, "Only values >= 0 can be converted to unsigned"); CCASSERT(_field.intVal >= 0, "Only values >= 0 can be converted to unsigned");
return static_cast<unsigned int>(_field.intVal); return static_cast<unsigned int>(_field.intVal);
case Type::BYTE:
return static_cast<unsigned int>(_field.byteVal);
case Type::STRING: case Type::STRING:
// NOTE: strtoul is required (need to augment on unsupported platforms) // NOTE: strtoul is required (need to augment on unsupported platforms)
return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10)); return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10));
@ -548,9 +532,6 @@ float Value::asFloat(float defaultValue) const
case Type::FLOAT: case Type::FLOAT:
return _field.floatVal; return _field.floatVal;
case Type::BYTE:
return static_cast<float>(_field.byteVal);
case Type::STRING: case Type::STRING:
return static_cast<float>(utils::atof(_field.strVal->c_str())); return static_cast<float>(utils::atof(_field.strVal->c_str()));
@ -558,7 +539,7 @@ float Value::asFloat(float defaultValue) const
return static_cast<float>(_field.intVal); return static_cast<float>(_field.intVal);
case Type::UNSIGNED: case Type::UNSIGNED:
return static_cast<float>(_field.unsignedVal); return static_cast<float>(_field.uintVal);
case Type::DOUBLE: case Type::DOUBLE:
return static_cast<float>(_field.doubleVal); return static_cast<float>(_field.doubleVal);
@ -580,9 +561,6 @@ double Value::asDouble(double defaultValue) const
case Type::DOUBLE: case Type::DOUBLE:
return _field.doubleVal; return _field.doubleVal;
case Type::BYTE:
return static_cast<double>(_field.byteVal);
case Type::STRING: case Type::STRING:
return static_cast<double>(utils::atof(_field.strVal->c_str())); return static_cast<double>(utils::atof(_field.strVal->c_str()));
@ -590,7 +568,7 @@ double Value::asDouble(double defaultValue) const
return static_cast<double>(_field.intVal); return static_cast<double>(_field.intVal);
case Type::UNSIGNED: case Type::UNSIGNED:
return static_cast<double>(_field.unsignedVal); return static_cast<double>(_field.uintVal);
case Type::FLOAT: case Type::FLOAT:
return static_cast<double>(_field.floatVal); return static_cast<double>(_field.floatVal);
@ -612,9 +590,6 @@ bool Value::asBool(bool defaultValue) const
case Type::BOOLEAN: case Type::BOOLEAN:
return _field.boolVal; return _field.boolVal;
case Type::BYTE:
return _field.byteVal == 0 ? false : true;
case Type::STRING: case Type::STRING:
return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true; return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true;
@ -622,7 +597,7 @@ bool Value::asBool(bool defaultValue) const
return _field.intVal == 0 ? false : true; return _field.intVal == 0 ? false : true;
case Type::UNSIGNED: case Type::UNSIGNED:
return _field.unsignedVal == 0 ? false : true; return _field.uintVal == 0 ? false : true;
case Type::FLOAT: case Type::FLOAT:
return _field.floatVal == 0.0f ? false : true; return _field.floatVal == 0.0f ? false : true;
@ -654,14 +629,11 @@ std::string Value::asString() const
size_t n = 0; size_t n = 0;
switch (_type) switch (_type)
{ {
case Type::BYTE:
ret = std::to_string(_field.byteVal);
break;
case Type::INTEGER: case Type::INTEGER:
ret = std::to_string(_field.intVal); ret = std::to_string(_field.intVal);
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
ret = std::to_string(_field.unsignedVal); ret = std::to_string(_field.uintVal);
break; break;
case Type::FLOAT: case Type::FLOAT:
ret.resize(NUMBER_MAX_DIGITS); ret.resize(NUMBER_MAX_DIGITS);
@ -791,7 +763,6 @@ static std::string visit(const Value& v, int depth)
switch (v.getType()) switch (v.getType())
{ {
case Value::Type::NONE: case Value::Type::NONE:
case Value::Type::BYTE:
case Value::Type::INTEGER: case Value::Type::INTEGER:
case Value::Type::UNSIGNED: case Value::Type::UNSIGNED:
case Value::Type::FLOAT: case Value::Type::FLOAT:
@ -829,14 +800,11 @@ void Value::clear()
// Free memory the old value allocated // Free memory the old value allocated
switch (_type) switch (_type)
{ {
case Type::BYTE:
_field.byteVal = 0;
break;
case Type::INTEGER: case Type::INTEGER:
_field.intVal = 0; _field.intVal = 0;
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
_field.unsignedVal = 0u; _field.uintVal = 0u;
break; break;
case Type::FLOAT: case Type::FLOAT:
_field.floatVal = 0.0f; _field.floatVal = 0.0f;

View File

@ -203,8 +203,6 @@ public:
{ {
/// no value is wrapped, an empty Value /// no value is wrapped, an empty Value
NONE = 0, NONE = 0,
/// wrap byte
BYTE,
/// wrap integer /// wrap integer
INTEGER, INTEGER,
/// wrap unsigned /// wrap unsigned
@ -237,9 +235,8 @@ private:
union union
{ {
unsigned char byteVal;
int intVal; int intVal;
unsigned int unsignedVal; unsigned int uintVal;
float floatVal; float floatVal;
double doubleVal; double doubleVal;
bool boolVal; bool boolVal;