diff --git a/include/cubescript/cubescript/ident.hh b/include/cubescript/cubescript/ident.hh index f3f7299..fd86614 100644 --- a/include/cubescript/cubescript/ident.hh +++ b/include/cubescript/cubescript/ident.hh @@ -196,6 +196,20 @@ struct LIBCUBESCRIPT_EXPORT builtin_var: ident { */ 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: builtin_var() = default; }; @@ -205,21 +219,6 @@ protected: * A specialization of cubescript::builtin_var for integer values. */ 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: integer_var() = default; }; @@ -229,21 +228,6 @@ protected: * A specialization of cubescript::builtin_var for float values. */ 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: float_var() = default; }; @@ -253,21 +237,6 @@ protected: * A specialization of cubescript::builtin_var for string values. */ 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: string_var() = default; }; diff --git a/src/cs_ident.cc b/src/cs_ident.cc index c39dd12..f88f058 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -333,8 +333,8 @@ LIBCUBESCRIPT_EXPORT void builtin_var::set_raw_value( static_cast(p_impl)->p_storage = std::move(val); } -LIBCUBESCRIPT_EXPORT void integer_var::set_value( - state &cs, integer_type val, bool do_write, bool trigger +LIBCUBESCRIPT_EXPORT void builtin_var::set_value( + state &cs, any_value val, bool do_write, bool trigger ) { if (is_read_only()) { throw error{ @@ -346,51 +346,7 @@ LIBCUBESCRIPT_EXPORT void integer_var::set_value( } save(cs); auto oldv = value(); - 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 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)); + set_raw_value(cs, std::move(val)); if (trigger) { var_changed(state_p{cs}.ts(), this, oldv); } diff --git a/src/cs_state.cc b/src/cs_state.cc index 6e618f9..bb286c4 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -130,7 +130,7 @@ state::state(alloc_func func, void *data) { std::printf(INTEGER_FORMAT, iv.value().get_integer()); std::printf("\n"); } 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"); } 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()); } } else { - sv.set_value(cs, args[1].get_string(cs)); + sv.set_value(cs, args[1]); } }); diff --git a/tools/repl.cc b/tools/repl.cc index 11e0bb7..f601486 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -332,19 +332,20 @@ int main(int argc, char **argv) { } return; } + cs::any_value nv; if (nargs == 2) { - iv.set_value(css, args[1].get_integer()); + nv = args[1]; } else if (nargs == 3) { - iv.set_value( - css, (args[1].get_integer() << 8) | - (args[2].get_integer() << 16) + nv.set_integer( + (args[1].get_integer() << 8) | (args[2].get_integer() << 16) ); } else { - iv.set_value( - css, args[1].get_integer() | (args[2].get_integer() << 8) | + nv.set_integer( + args[1].get_integer() | (args[2].get_integer() << 8) | (args[3].get_integer() << 16) ); } + iv.set_value(css, nv); }); gcs.new_command("//var_changed", "$aa", [](auto &css, auto args, auto &) {