Add defaultValue CCValue::asXXX for number types [skip ci]

This commit is contained in:
halx99 2021-07-15 12:21:38 +08:00
parent f7338a045d
commit 522de34990
2 changed files with 22 additions and 20 deletions

View File

@ -1,8 +1,9 @@
/****************************************************************************
Copyright (c) 2013-2017 Chukong Technologies
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
http://www.cocos2d-x.org
https://adxe.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -441,7 +442,7 @@ bool Value::operator== (const Value& v) const
}
/// Convert value to a specified type
unsigned char Value::asByte() const
unsigned char Value::asByte(unsigned char defaultValue) const
{
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
@ -467,10 +468,10 @@ unsigned char Value::asByte() const
return _field.boolVal ? 1 : 0;
}
return 0;
return defaultValue;
}
int Value::asInt() const
int Value::asInt(int defaultValue) const
{
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
switch (_type) {
@ -496,11 +497,11 @@ int Value::asInt() const
case Type::BOOLEAN:
return _field.boolVal ? 1 : 0;
}
return 0;
return defaultValue;
}
unsigned int Value::asUnsignedInt() const
unsigned int Value::asUnsignedInt(unsigned int defaultValue) const
{
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
switch (_type) {
@ -528,10 +529,10 @@ unsigned int Value::asUnsignedInt() const
return _field.boolVal ? 1u : 0u;
}
return 0u;
return defaultValue;
}
float Value::asFloat() const
float Value::asFloat(float defaultValue) const
{
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
switch (_type) {
@ -556,10 +557,10 @@ float Value::asFloat() const
case Type::BOOLEAN:
return _field.boolVal ? 1.0f : 0.0f;
}
return 0.0f;
return defaultValue;
}
double Value::asDouble() const
double Value::asDouble(double defaultValue) const
{
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
switch (_type) {
@ -584,10 +585,10 @@ double Value::asDouble() const
case Type::BOOLEAN:
return _field.boolVal ? 1.0 : 0.0;
}
return 0.0;
return defaultValue;
}
bool Value::asBool() const
bool Value::asBool(bool defaultValue) const
{
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
switch (_type) {
@ -613,7 +614,7 @@ bool Value::asBool() const
return _field.doubleVal == 0.0 ? false : true;
}
return false;
return defaultValue;
}
std::string Value::asString() const

View File

@ -1,8 +1,9 @@
/****************************************************************************
Copyright (c) 2013-2017 Chukong Technologies
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
http://www.cocos2d-x.org
https://adxe.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -159,17 +160,17 @@ public:
bool operator== (const Value& v) const;
/** Gets as a byte value. Will convert to unsigned char if possible, or will trigger assert error. */
unsigned char asByte() const;
unsigned char asByte(unsigned char defaultValue = 0) const;
/** Gets as an integer value. Will convert to integer if possible, or will trigger assert error. */
int asInt() const;
int asInt(int defaultValue = 0) const;
/** Gets as an unsigned value. Will convert to unsigned if possible, or will trigger assert error. */
unsigned int asUnsignedInt() const;
unsigned int asUnsignedInt(unsigned defaultValue = 0) const;
/** Gets as a float value. Will convert to float if possible, or will trigger assert error. */
float asFloat() const;
float asFloat(float defaultValue = 0.0f) const;
/** Gets as a double value. Will convert to double if possible, or will trigger assert error. */
double asDouble() const;
double asDouble(double defaultValue = 0.0) const;
/** Gets as a bool value. Will convert to bool if possible, or will trigger assert error. */
bool asBool() const;
bool asBool(bool defaultValue = false) const;
/** to as a string value. Will convert to string if possible, or will trigger assert error. */
std::string asString() const;