Use toSomeType() instead of operator SomeType in ScriptValue.

This means that we are more explicit about type conversions.
Also use override specifiers for overriden virtual functions in ScriptValue.
This commit is contained in:
Twan van Laarhoven
2020-04-26 17:03:43 +02:00
parent 42b7cf52b8
commit 2e897edbbf
22 changed files with 524 additions and 490 deletions
+19 -19
View File
@@ -76,9 +76,9 @@ String format_input(const String& format, const ScriptValue& input) {
// determine type expected by format string
String fmt = _("%") + replace_all(format, _("%"), _(""));
if (format.find_first_of(_("DdIiOoXx")) != String::npos) {
return String::Format(fmt, (int)input);
return String::Format(fmt, input.toInt());
} else if (format.find_first_of(_("EeFfGg")) != String::npos) {
return String::Format(fmt, (double)input);
return String::Format(fmt, input.toDouble());
} else if (format.find_first_of(_("Ss")) != String::npos) {
return format_string(fmt, input.toString());
} else {
@@ -93,13 +93,13 @@ SCRIPT_FUNCTION(to_string) {
try {
if (format && format->type() == SCRIPT_STRING) {
// format specifier. Be careful, the built in function 'format' has the same name
SCRIPT_RETURN(format_input(*format, *input));
SCRIPT_RETURN(format_input(format->toString(), *input));
} else {
// simple conversion
SCRIPT_RETURN(input->toString());
}
} catch (const ScriptError& e) {
return make_intrusive<ScriptDelayedError>(e);
return delay_error(e);
}
}
@@ -109,9 +109,9 @@ SCRIPT_FUNCTION(to_int) {
try {
int result;
if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1 : 0;
result = input->toBool() ? 1 : 0;
} else if (t == SCRIPT_COLOR) {
Color c = input->operator Color();
Color c = input->toColor();
result = (c.Red() + c.Blue() + c.Green()) / 3;
} else if (t == SCRIPT_STRING) {
long l;
@@ -124,7 +124,7 @@ SCRIPT_FUNCTION(to_int) {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("integer")));
}
} else {
result = (int)*input;
result = input->toInt();
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
@@ -138,9 +138,9 @@ SCRIPT_FUNCTION(to_real) {
try {
double result;
if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1.0 : 0.0;
result = input->toBool() ? 1.0 : 0.0;
} else if (t == SCRIPT_COLOR) {
Color c = input->operator Color();
Color c = input->toColor();
result = (c.Red() + c.Blue() + c.Green()) / 3.0;
} else if (t == SCRIPT_STRING) {
String str = input->toString();
@@ -150,7 +150,7 @@ SCRIPT_FUNCTION(to_real) {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("double")));
}
} else {
result = (double)*input;
result = input->toDouble();
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
@@ -163,12 +163,12 @@ SCRIPT_FUNCTION(to_number) {
ScriptType t = input->type();
try {
if (t == SCRIPT_BOOL) {
SCRIPT_RETURN((bool)*input ? 1 : 0);
SCRIPT_RETURN(input->toBool() ? 1 : 0);
} else if (t == SCRIPT_COLOR) {
Color c = input->operator Color();
Color c = input->toColor();
SCRIPT_RETURN( (c.Red() + c.Blue() + c.Green()) / 3 );
} else if (t == SCRIPT_DOUBLE) {
SCRIPT_RETURN((double)*input);
SCRIPT_RETURN(input->toDouble());
} else if (t == SCRIPT_NIL) {
SCRIPT_RETURN(0);
} else {
@@ -195,9 +195,9 @@ SCRIPT_FUNCTION(to_boolean) {
ScriptType t = input->type();
bool result;
if (t == SCRIPT_INT) {
result = (int)*input != 0;
result = input->toInt() != 0;
} else {
result = (bool)*input;
result = input->toBool();
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
@@ -239,9 +239,9 @@ SCRIPT_FUNCTION(abs) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
if (t == SCRIPT_DOUBLE) {
SCRIPT_RETURN(fabs((double)*input));
SCRIPT_RETURN(fabs(input->toDouble()));
} else {
SCRIPT_RETURN(abs((int)*input));
SCRIPT_RETURN(abs(input->toInt()));
}
}
@@ -579,7 +579,7 @@ SCRIPT_FUNCTION(filter_list) {
ScriptValueP it = input->makeIterator();
while (ScriptValueP v = it->next()) {
ctx.setVariable(SCRIPT_VAR_input, v);
if (*filter->eval(ctx)) {
if (filter->eval(ctx)->toBool()) {
ret->value.push_back(v);
}
}
@@ -628,7 +628,7 @@ SCRIPT_FUNCTION(random_select_many) {
SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_PARAM(int, count) ;
SCRIPT_OPTIONAL_PARAM_C_(ScriptValueP, replace);
bool with_replace = replace && replace->type() != SCRIPT_FUNCTION && (bool)*replace;
bool with_replace = replace && replace->type() != SCRIPT_FUNCTION && replace->toBool();
// pick many
ScriptCustomCollectionP ret(new ScriptCustomCollection);
int itemCount = input->itemCount();