remove set_value() specialization per-vartype

master
Daniel Kolesa 2021-05-06 04:13:11 +02:00
parent fa5e38afad
commit 6fede13b97
4 changed files with 27 additions and 101 deletions

View File

@ -196,6 +196,20 @@ struct LIBCUBESCRIPT_EXPORT builtin_var: ident {
*/ */
void set_raw_value(state &cs, any_value val); void set_raw_value(state &cs, any_value val);
/** @brief Set the value of the variable.
*
* If read only, an error is raised. If `do_write` is `false`, nothing
* 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 save(), assuming `do_write` is `true`.
* After that, set_raw_value() is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
*/
void set_value(
state &cs, any_value val, bool do_write = true, bool trigger = true
);
protected: protected:
builtin_var() = default; builtin_var() = default;
}; };
@ -205,21 +219,6 @@ protected:
* A specialization of cubescript::builtin_var for integer values. * A specialization of cubescript::builtin_var for integer values.
*/ */
struct LIBCUBESCRIPT_EXPORT integer_var: builtin_var { struct LIBCUBESCRIPT_EXPORT integer_var: builtin_var {
/** @brief Set the value of the variable.
*
* If read only, an error is raised. If `do_write` is `false`, nothing
* 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, builtin_var::set_raw_value()
* is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
*/
void set_value(
state &cs, integer_type val, bool do_write = true, bool trigger = true
);
protected: protected:
integer_var() = default; integer_var() = default;
}; };
@ -229,21 +228,6 @@ protected:
* A specialization of cubescript::builtin_var for float values. * A specialization of cubescript::builtin_var for float values.
*/ */
struct LIBCUBESCRIPT_EXPORT float_var: builtin_var { struct LIBCUBESCRIPT_EXPORT float_var: builtin_var {
/** @brief Set the value of the variable.
*
* If read only, an error is raised. If `do_write` is `false`, nothing
* 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, builtin_var::set_raw_value()
* is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
*/
void set_value(
state &cs, float_type val, bool do_write = true, bool trigger = true
);
protected: protected:
float_var() = default; float_var() = default;
}; };
@ -253,21 +237,6 @@ protected:
* A specialization of cubescript::builtin_var for string values. * A specialization of cubescript::builtin_var for string values.
*/ */
struct LIBCUBESCRIPT_EXPORT string_var: builtin_var { struct LIBCUBESCRIPT_EXPORT string_var: builtin_var {
/** @brief Set the value of the variable.
*
* If read only, an error is raised. If `do_write` is `false`, nothing
* 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, builtin_var::set_raw_value()
* is invoked, and then the trigger.
*
* @throw cubescript::error if read only or if the changed trigger throws.
*/
void set_value(
state &cs, string_ref val, bool do_write = true, bool trigger = true
);
protected: protected:
string_var() = default; string_var() = default;
}; };

View File

@ -333,8 +333,8 @@ LIBCUBESCRIPT_EXPORT void builtin_var::set_raw_value(
static_cast<var_impl *>(p_impl)->p_storage = std::move(val); static_cast<var_impl *>(p_impl)->p_storage = std::move(val);
} }
LIBCUBESCRIPT_EXPORT void integer_var::set_value( LIBCUBESCRIPT_EXPORT void builtin_var::set_value(
state &cs, integer_type val, bool do_write, bool trigger state &cs, any_value val, bool do_write, bool trigger
) { ) {
if (is_read_only()) { if (is_read_only()) {
throw error{ throw error{
@ -346,51 +346,7 @@ LIBCUBESCRIPT_EXPORT void integer_var::set_value(
} }
save(cs); save(cs);
auto oldv = value(); auto oldv = value();
any_value nv; set_raw_value(cs, std::move(val));
nv.set_integer(val);
set_raw_value(cs, std::move(nv));
if (trigger) {
var_changed(state_p{cs}.ts(), this, oldv);
}
}
LIBCUBESCRIPT_EXPORT void float_var::set_value(
state &cs, float_type val, bool do_write, bool trigger
) {
if (is_read_only()) {
throw error{
cs, "variable '%s' is read only", name().data()
};
}
if (!do_write) {
return;
}
save(cs);
auto oldv = value();
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 string_var::set_value(
state &cs, string_ref val, bool do_write, bool trigger
) {
if (is_read_only()) {
throw error{
cs, "variable '%s' is read only", name().data()
};
}
if (!do_write) {
return;
}
save(cs);
auto oldv = value();
any_value nv;
nv.set_string(std::move(val));
set_raw_value(cs, std::move(nv));
if (trigger) { if (trigger) {
var_changed(state_p{cs}.ts(), this, oldv); var_changed(state_p{cs}.ts(), this, oldv);
} }

View File

@ -130,7 +130,7 @@ state::state(alloc_func func, void *data) {
std::printf(INTEGER_FORMAT, iv.value().get_integer()); std::printf(INTEGER_FORMAT, iv.value().get_integer());
std::printf("\n"); std::printf("\n");
} else { } else {
iv.set_value(cs, args[1].get_integer()); iv.set_value(cs, args[1]);
} }
}); });
@ -148,7 +148,7 @@ state::state(alloc_func func, void *data) {
} }
std::printf("\n"); std::printf("\n");
} else { } else {
fv.set_value(cs, args[1].get_float()); fv.set_value(cs, args[1]);
} }
}); });
@ -164,7 +164,7 @@ state::state(alloc_func func, void *data) {
std::printf("%s = [%s]\n", sv.name().data(), val.data()); std::printf("%s = [%s]\n", sv.name().data(), val.data());
} }
} else { } else {
sv.set_value(cs, args[1].get_string(cs)); sv.set_value(cs, args[1]);
} }
}); });

View File

@ -332,19 +332,20 @@ int main(int argc, char **argv) {
} }
return; return;
} }
cs::any_value nv;
if (nargs == 2) { if (nargs == 2) {
iv.set_value(css, args[1].get_integer()); nv = args[1];
} else if (nargs == 3) { } else if (nargs == 3) {
iv.set_value( nv.set_integer(
css, (args[1].get_integer() << 8) | (args[1].get_integer() << 8) | (args[2].get_integer() << 16)
(args[2].get_integer() << 16)
); );
} else { } else {
iv.set_value( nv.set_integer(
css, args[1].get_integer() | (args[2].get_integer() << 8) | args[1].get_integer() | (args[2].get_integer() << 8) |
(args[3].get_integer() << 16) (args[3].get_integer() << 16)
); );
} }
iv.set_value(css, nv);
}); });
gcs.new_command("//var_changed", "$aa", [](auto &css, auto args, auto &) { gcs.new_command("//var_changed", "$aa", [](auto &css, auto args, auto &) {