move set_raw_value to builtin_var

master
Daniel Kolesa 2021-05-06 04:01:29 +02:00
parent 74f437f851
commit 6a892b4b2c
3 changed files with 45 additions and 42 deletions

View File

@ -188,6 +188,14 @@ struct LIBCUBESCRIPT_EXPORT builtin_var: ident {
/** @brief Get the value of the variable. */
any_value value() const;
/** @brief Set the value of the variable in a raw manner.
*
* This will always set the value and ignore any kinds of checks. It will
* not invoke any triggers either, nor it will save the the value. However,
* it will make sure to preserve the type (integer, float or string).
*/
void set_raw_value(state &cs, any_value val);
protected:
builtin_var() = default;
};
@ -203,7 +211,7 @@ struct LIBCUBESCRIPT_EXPORT integer_var: builtin_var {
* will be performed other than the read-only checking. If `trigger` is
* `false`, a potential variable change trigger command will not be
* invoked. The value is saved with builtin_var::save(), assuming
* `do_write` is `true`. After that, integer_var::set_raw_value()
* `do_write` is `true`. After that, builtin_var::set_raw_value()
* is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
@ -212,13 +220,6 @@ struct LIBCUBESCRIPT_EXPORT integer_var: builtin_var {
state &cs, integer_type val, bool do_write = true, bool trigger = true
);
/** @brief Set the value of the variable in a raw manner.
*
* This will always set the value and ignore any kinds of checks. It will
* not invoke any triggers either, nor it will save the the value.
*/
void set_raw_value(integer_type val);
/** @brief Call override for integer vars. */
any_value call(span_type<any_value> args, state &cs);
@ -237,7 +238,7 @@ struct LIBCUBESCRIPT_EXPORT float_var: builtin_var {
* will be performed other than the read-only checking. If `trigger` is
* `false`, a potential variable change trigger command will not be
* invoked. The value is saved with builtin_var::save(), assuming
* `do_write` is `true`. After that, integer_var::set_raw_value()
* `do_write` is `true`. After that, builtin_var::set_raw_value()
* is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
@ -246,13 +247,6 @@ struct LIBCUBESCRIPT_EXPORT float_var: builtin_var {
state &cs, float_type val, bool do_write = true, bool trigger = true
);
/** @brief Set the value of the variable in a raw manner.
*
* This will always set the value and ignore any kinds of checks. It will
* not invoke any triggers either, nor it will save the the value.
*/
void set_raw_value(float_type val);
/** @brief Call override for float vars. */
any_value call(span_type<any_value> args, state &cs);
@ -271,7 +265,7 @@ struct LIBCUBESCRIPT_EXPORT string_var: builtin_var {
* will be performed other than the read-only checking. If `trigger` is
* `false`, a potential variable change trigger command will not be
* invoked. The value is saved with builtin_var::save(), assuming
* `do_write` is `true`. After that, integer_var::set_raw_value()
* `do_write` is `true`. After that, builtin_var::set_raw_value()
* is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
@ -280,13 +274,6 @@ struct LIBCUBESCRIPT_EXPORT string_var: builtin_var {
state &cs, string_ref val, bool do_write = true, bool trigger = true
);
/** @brief Set the value of the variable in a raw manner.
*
* This will always set the value and ignore any kinds of checks. It will
* not invoke any triggers either, nor it will save the the value.
*/
void set_raw_value(string_ref val);
/** @brief Call override for string vars. */
any_value call(span_type<any_value> args, state &cs);

View File

@ -284,6 +284,26 @@ LIBCUBESCRIPT_EXPORT any_value builtin_var::value() const {
return static_cast<var_impl const *>(p_impl)->p_storage;
}
LIBCUBESCRIPT_EXPORT void builtin_var::set_raw_value(
state &cs, any_value val
) {
switch (static_cast<var_impl *>(p_impl)->p_storage.type()) {
case value_type::INTEGER:
val.force_integer();
break;
case value_type::FLOAT:
val.force_float();
break;
case value_type::STRING:
val.force_string(cs);
break;
default:
abort(); /* unreachable unless we have a bug */
break;
}
static_cast<var_impl *>(p_impl)->p_storage = std::move(val);
}
LIBCUBESCRIPT_EXPORT void integer_var::set_value(
state &cs, integer_type val, bool do_write, bool trigger
) {
@ -297,16 +317,14 @@ LIBCUBESCRIPT_EXPORT void integer_var::set_value(
}
save(cs);
auto oldv = value();
set_raw_value(val);
any_value nv;
nv.set_integer(val);
set_raw_value(cs, std::move(nv));
if (trigger) {
var_changed(state_p{cs}.ts(), this, oldv);
}
}
LIBCUBESCRIPT_EXPORT void integer_var::set_raw_value(integer_type val) {
static_cast<ivar_impl *>(this)->p_storage.set_integer(val);
}
inline any_value call_var(
ident &id, command *hid, span_type<any_value> &args, state &cs
) {
@ -346,16 +364,14 @@ LIBCUBESCRIPT_EXPORT void float_var::set_value(
}
save(cs);
auto oldv = value();
set_raw_value(val);
any_value nv;
nv.set_float(val);
set_raw_value(cs, std::move(nv));
if (trigger) {
var_changed(state_p{cs}.ts(), this, oldv);
}
}
LIBCUBESCRIPT_EXPORT void float_var::set_raw_value(float_type val) {
static_cast<fvar_impl *>(this)->p_storage.set_float(val);
}
LIBCUBESCRIPT_EXPORT any_value float_var::call(
span_type<any_value> args, state &cs
) {
@ -375,16 +391,14 @@ LIBCUBESCRIPT_EXPORT void string_var::set_value(
}
save(cs);
auto oldv = value();
set_raw_value(std::move(val));
any_value nv;
nv.set_string(std::move(val));
set_raw_value(cs, std::move(nv));
if (trigger) {
var_changed(state_p{cs}.ts(), this, oldv);
}
}
LIBCUBESCRIPT_EXPORT void string_var::set_raw_value(string_ref val) {
static_cast<svar_impl *>(this)->p_storage.set_string(std::move(val));
}
LIBCUBESCRIPT_EXPORT any_value string_var::call(
span_type<any_value> args, state &cs
) {

View File

@ -167,10 +167,12 @@ bool exec_alias(
st.val_s = std::move(args[offset + i]);
uargs[i] = true;
}
auto oldargs = anargs->value().get_integer();
auto oldargs = anargs->value();
auto oldflags = ts.ident_flags;
ts.ident_flags = aast.flags;
anargs->set_raw_value(integer_type(callargs));
any_value cv;
cv.set_integer(callargs);
anargs->set_raw_value(*ts.pstate, std::move(cv));
ident_link aliaslink = {a, ts.callstack, uargs};
ts.callstack = &aliaslink;
if (!aast.node->code) {
@ -199,7 +201,7 @@ bool exec_alias(
}
ts.idstack.resize(noff);
force_arg(*ts.pstate, result, op & BC_INST_RET_MASK);
anargs->set_raw_value(integer_type(oldargs));
anargs->set_raw_value(*ts.pstate, std::move(oldargs));
nargs = offset - skip;
};
try {