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();
+1 -1
View File
@@ -44,7 +44,7 @@ SCRIPT_FUNCTION(new_card) {
} else if (PackageChoiceValue* pvalue = dynamic_cast<PackageChoiceValue*>(value)) {
pvalue->package_name = v->toString();
} else if (ColorValue* cvalue = dynamic_cast<ColorValue*>(value)) {
cvalue->value = v->operator Color();
cvalue->value = v->toColor();
} else {
throw ScriptError(format_string(_("Can not set value '%s', it is not of the right type"),name));
}
+1 -1
View File
@@ -52,7 +52,7 @@ ScriptRegexP regex_from_script(const ScriptValueP& value) {
ScriptRegexP regex = dynamic_pointer_cast<ScriptRegex>(value);
if (!regex) {
// TODO: introduce some kind of caching?
regex = make_intrusive<ScriptRegex>(*value);
regex = make_intrusive<ScriptRegex>(value->toString());
}
return regex;
}
+2 -2
View File
@@ -37,12 +37,12 @@ inline size_t spelled_correctly(const String& input, size_t start, size_t end, S
if (extra_test) {
// try on untagged
ctx.setVariable(SCRIPT_VAR_input, to_script(word));
if (*extra_test->eval(ctx)) {
if (extra_test->eval(ctx)->toBool()) {
return true;
}
// try on tagged
ctx.setVariable(SCRIPT_VAR_input, to_script(input.substr(start,end-start)));
if (*extra_test->eval(ctx)) {
if (extra_test->eval(ctx)->toBool()) {
return true;
}
}
+23 -23
View File
@@ -37,31 +37,31 @@
#define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,;)
/// Macro to declare a new script function with custom dependency handling
#define SCRIPT_FUNCTION_WITH_DEP(name) \
SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
#define SCRIPT_FUNCTION_WITH_DEP(name) \
SCRIPT_FUNCTION_AUX(name, ScriptValueP dependencies(Context&, const Dependency&) const override;)
#define SCRIPT_FUNCTION_DEPENDENCIES(name) \
#define SCRIPT_FUNCTION_DEPENDENCIES(name) \
ScriptValueP ScriptBuiltIn_##name::dependencies(Context& ctx, const Dependency& dep) const
/// Macro to declare a new script function with custom closure simplification
#define SCRIPT_FUNCTION_WITH_SIMPLIFY(name) \
SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP simplifyClosure(ScriptClosure&) const;)
#define SCRIPT_FUNCTION_WITH_SIMPLIFY(name) \
SCRIPT_FUNCTION_AUX(name, ScriptValueP simplifyClosure(ScriptClosure&) const override;)
#define SCRIPT_FUNCTION_SIMPLIFY_CLOSURE(name) \
#define SCRIPT_FUNCTION_SIMPLIFY_CLOSURE(name) \
ScriptValueP ScriptBuiltIn_##name::simplifyClosure(ScriptClosure& closure) const
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
#define SCRIPT_FUNCTION_AUX(name,dep) \
class ScriptBuiltIn_##name : public ScriptValue { \
dep \
virtual ScriptType type() const \
{ return SCRIPT_FUNCTION; } \
virtual String typeName() const \
#define SCRIPT_FUNCTION_AUX(name,dep) \
class ScriptBuiltIn_##name : public ScriptValue { \
dep \
ScriptType type() const override \
{ return SCRIPT_FUNCTION; } \
String typeName() const override \
{ return _("built-in function '") _(#name) _("'"); } \
virtual ScriptValueP do_eval(Context&, bool) const; \
ScriptValueP eval(Context&, bool) const override; \
}; \
ScriptValueP script_##name(new ScriptBuiltIn_##name); \
ScriptValueP ScriptBuiltIn_##name::do_eval(Context& ctx, bool) const
ScriptValueP ScriptBuiltIn_##name::eval(Context& ctx, bool) const
/// Return a value from a SCRIPT_FUNCTION
#define SCRIPT_RETURN(value) return to_script(value)
@@ -157,10 +157,10 @@ inline Type from_script(const ScriptValueP& v, Variable var) {
class ScriptRule_##funname: public ScriptValue { \
public: \
inline ScriptRule_##funname(const type1& name1) : name1(name1) {} \
virtual ScriptType type() const { return SCRIPT_FUNCTION; } \
virtual String typeName() const { return _(#funname)_("_rule"); } \
ScriptType type() const override { return SCRIPT_FUNCTION; } \
String typeName() const override { return _(#funname)_("_rule"); } \
protected: \
virtual ScriptValueP do_eval(Context& ctx, bool) const; \
ScriptValueP eval(Context& ctx, bool) const override; \
private: \
type1 name1; \
}; \
@@ -172,7 +172,7 @@ inline Type from_script(const ScriptValueP& v, Variable var) {
SCRIPT_PARAM_N(type1, str1, name1); \
return ScriptRule_##funname(name1).eval(ctx); \
} \
ScriptValueP ScriptRule_##funname::do_eval(Context& ctx, bool) const
ScriptValueP ScriptRule_##funname::eval(Context& ctx, bool) const
/// Utility for defining a script rule with two parameters
#define SCRIPT_RULE_2(funname, type1, name1, type2, name2) \
@@ -185,7 +185,7 @@ inline Type from_script(const ScriptValueP& v, Variable var) {
/// Utility for defining a script rule with two named parameters, with dependencies
#define SCRIPT_RULE_2_N_DEP(funname, type1, str1, name1, type2, str2, name2) \
SCRIPT_RULE_2_N_AUX( funname, type1, str1, name1, type2, str2, name2, \
virtual ScriptValueP dependencies(Context&, const Dependency&) const; \
ScriptValueP dependencies(Context&, const Dependency&) const override; \
SCRIPT_FUNCTION_DEPENDENCIES(funname) { \
SCRIPT_PARAM_N(type1, str1, name1); \
SCRIPT_PARAM_N(type2, str2, name2); \
@@ -197,11 +197,11 @@ inline Type from_script(const ScriptValueP& v, Variable var) {
public: \
inline ScriptRule_##funname(const type1& name1, const type2& name2) \
: name1(name1), name2(name2) {} \
virtual ScriptType type() const { return SCRIPT_FUNCTION; } \
virtual String typeName() const { return _(#funname)_("_rule"); } \
ScriptType type() const override { return SCRIPT_FUNCTION; } \
String typeName() const override { return _(#funname)_("_rule"); } \
dep \
protected: \
virtual ScriptValueP do_eval(Context& ctx, bool) const; \
ScriptValueP eval(Context& ctx, bool) const override; \
private: \
type1 name1; \
type2 name2; \
@@ -217,7 +217,7 @@ inline Type from_script(const ScriptValueP& v, Variable var) {
return ScriptRule_##funname(name1, name2).eval(ctx); \
} \
more \
ScriptValueP ScriptRule_##funname::do_eval(Context& ctx, bool) const
ScriptValueP ScriptRule_##funname::eval(Context& ctx, bool) const
#define SCRIPT_RULE_2_DEPENDENCIES(name) \
ScriptValueP ScriptRule_##name::dependencies(Context& ctx, const Dependency& dep) const