From 8418b8bb9e256a706f9dbd7f48c7114f33ec2e6e Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 18 Aug 2016 03:14:55 +0100 Subject: [PATCH] clean up the other var interfaces --- cs_vm.cc | 62 ++++++++++++++----------- cubescript.cc | 126 ++++++++++++++++++++++++++++++-------------------- cubescript.hh | 42 +++++++++++++---- 3 files changed, 143 insertions(+), 87 deletions(-) diff --git a/cs_vm.cc b/cs_vm.cc index 2176e5f8..658b4cd2 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -983,16 +983,16 @@ static ostd::Uint32 const *runcode( )); continue; case ID_SVAR: - arg.set_str(*static_cast(id)->storage); + arg.set_str(static_cast(id)->get_var_value()); continue; case ID_IVAR: arg.set_str(ostd::move( - intstr(*static_cast(id)->storage) + intstr(static_cast(id)->get_var_value()) )); continue; case ID_FVAR: arg.set_str(ostd::move( - floatstr(*static_cast(id)->storage) + floatstr(static_cast(id)->get_var_value()) )); continue; case ID_UNKNOWN: @@ -1025,14 +1025,16 @@ static ostd::Uint32 const *runcode( continue; case ID_SVAR: arg.set_int(cs_parse_int( - *static_cast(id)->storage + static_cast(id)->get_var_value() )); continue; case ID_IVAR: - arg.set_int(*static_cast(id)->storage); + arg.set_int(static_cast(id)->get_var_value()); continue; case ID_FVAR: - arg.set_int(CsInt(*static_cast(id)->storage)); + arg.set_int( + CsInt(static_cast(id)->get_var_value()) + ); continue; case ID_UNKNOWN: arg.set_int(0); @@ -1066,16 +1068,18 @@ static ostd::Uint32 const *runcode( continue; case ID_SVAR: arg.set_float(cs_parse_float( - *static_cast(id)->storage + static_cast(id)->get_var_value() )); continue; case ID_IVAR: arg.set_float(CsFloat( - *static_cast(id)->storage + static_cast(id)->get_var_value() )); continue; case ID_FVAR: - arg.set_float(*static_cast(id)->storage); + arg.set_float( + static_cast(id)->get_var_value() + ); continue; case ID_UNKNOWN: arg.set_float(CsFloat(0)); @@ -1106,13 +1110,15 @@ static ostd::Uint32 const *runcode( static_cast(id)->val_v.get_val(arg); continue; case ID_SVAR: - arg.set_str(*static_cast(id)->storage); + arg.set_str(static_cast(id)->get_var_value()); continue; case ID_IVAR: - arg.set_int(*static_cast(id)->storage); + arg.set_int(static_cast(id)->get_var_value()); continue; case ID_FVAR: - arg.set_float(*static_cast(id)->storage); + arg.set_float( + static_cast(id)->get_var_value() + ); continue; case ID_UNKNOWN: arg.set_null(); @@ -1142,16 +1148,16 @@ static ostd::Uint32 const *runcode( static_cast(id)->get_cstr(arg); continue; case ID_SVAR: - arg.set_cstr(*static_cast(id)->storage); + arg.set_cstr(static_cast(id)->get_var_value()); continue; case ID_IVAR: arg.set_str(ostd::move( - intstr(*static_cast(id)->storage) + intstr(static_cast(id)->get_var_value()) )); continue; case ID_FVAR: arg.set_str(ostd::move( - floatstr(*static_cast(id)->storage) + floatstr(static_cast(id)->get_var_value()) )); continue; case ID_UNKNOWN: @@ -1181,13 +1187,13 @@ static ostd::Uint32 const *runcode( static_cast(id)->get_cval(arg); continue; case ID_SVAR: - arg.set_cstr(*static_cast(id)->storage); + arg.set_cstr(static_cast(id)->get_var_value()); continue; case ID_IVAR: - arg.set_int(*static_cast(id)->storage); + arg.set_int(static_cast(id)->get_var_value()); continue; case ID_FVAR: - arg.set_float(*static_cast(id)->storage); + arg.set_float(static_cast(id)->get_var_value()); continue; case ID_UNKNOWN: arg.set_null(); @@ -1212,22 +1218,22 @@ static ostd::Uint32 const *runcode( case CODE_SVAR | RET_STR: case CODE_SVAR | RET_NULL: args[numargs++].set_str( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() ); continue; case CODE_SVAR | RET_INT: args[numargs++].set_int(cs_parse_int( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() )); continue; case CODE_SVAR | RET_FLOAT: args[numargs++].set_float(cs_parse_float( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() )); continue; case CODE_SVARM: args[numargs++].set_cstr( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() ); continue; case CODE_SVAR1: @@ -1240,17 +1246,17 @@ static ostd::Uint32 const *runcode( case CODE_IVAR | RET_INT: case CODE_IVAR | RET_NULL: args[numargs++].set_int( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() ); continue; case CODE_IVAR | RET_STR: args[numargs++].set_str(ostd::move(intstr( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() ))); continue; case CODE_IVAR | RET_FLOAT: args[numargs++].set_float(CsFloat( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() )); continue; case CODE_IVAR1: @@ -1276,17 +1282,17 @@ static ostd::Uint32 const *runcode( case CODE_FVAR | RET_FLOAT: case CODE_FVAR | RET_NULL: args[numargs++].set_float( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() ); continue; case CODE_FVAR | RET_STR: args[numargs++].set_str(ostd::move(floatstr( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() ))); continue; case CODE_FVAR | RET_INT: args[numargs++].set_int(int( - *static_cast(cs.identmap[op >> 8])->storage + static_cast(cs.identmap[op >> 8])->get_var_value() )); continue; case CODE_FVAR1: diff --git a/cubescript.cc b/cubescript.cc index 02eee031..c197c079 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -50,19 +50,19 @@ Ivar::Ivar( ostd::ConstCharRange name, CsInt m, CsInt x, CsInt *s, VarCb f, int fl ): Var(IdentType::ivar, name, ostd::move(f), fl | ((m > x) ? IDF_READONLY : 0)), - minval(m), maxval(x), overrideval(0), storage(s) + p_storage(s), p_minval(m), p_maxval(x), p_overrideval(0) {} Fvar::Fvar( ostd::ConstCharRange name, CsFloat m, CsFloat x, CsFloat *s, VarCb f, int fl ): Var(IdentType::fvar, name, ostd::move(f), fl | ((m > x) ? IDF_READONLY : 0)), - minval(m), maxval(x), overrideval(0), storage(s) + p_storage(s), p_minval(m), p_maxval(x), p_overrideval(0) {} Svar::Svar(ostd::ConstCharRange name, char **s, VarCb f, int fl): Var(IdentType::svar, name, ostd::move(f), fl), - overrideval(nullptr), storage(s) + p_storage(s), p_overrideval(nullptr) {} Alias::Alias(ostd::ConstCharRange name, char *a, int fl): @@ -204,6 +204,32 @@ Svar const *Ident::get_svar() const { return static_cast(this); } +CsInt Ivar::get_val_min() const { + return p_minval; +} +CsInt Ivar::get_val_max() const { + return p_maxval; +} + +CsInt Ivar::get_var_value() const { + return *p_storage; +} + +CsFloat Fvar::get_val_min() const { + return p_minval; +} +CsFloat Fvar::get_val_max() const { + return p_maxval; +} + +CsFloat Fvar::get_var_value() const { + return *p_storage; +} + +ostd::ConstCharRange Svar::get_var_value() const { + return *p_storage; +} + void cs_init_lib_base(CsState &cs); CsState::CsState() { @@ -250,20 +276,20 @@ void CsState::clear_override(Ident &id) { } case IdentType::ivar: { Ivar &iv = static_cast(id); - *iv.storage = iv.overrideval; + *iv.p_storage = iv.p_overrideval; iv.changed(); break; } case IdentType::fvar: { Fvar &fv = static_cast(id); - *fv.storage = fv.overrideval; + *fv.p_storage = fv.p_overrideval; fv.changed(); break; } case IdentType::svar: { Svar &sv = static_cast(id); - delete[] *sv.storage; - *sv.storage = sv.overrideval; + delete[] *sv.p_storage; + *sv.p_storage = sv.p_overrideval; sv.changed(); break; } @@ -388,7 +414,7 @@ void CsState::print_var_int(Ivar *iv, CsInt i) { return; } if (iv->get_flags() & IDF_HEX) { - if (iv->maxval == 0xFFFFFF) { + if (iv->get_val_max() == 0xFFFFFF) { writefln( "%s = 0x%.6X (%d, %d, %d)", iv->get_name(), i, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF @@ -417,17 +443,17 @@ void CsState::print_var(Var *v) { switch (v->get_type()) { case IdentType::ivar: { Ivar *iv = static_cast(v); - print_var_int(iv, *iv->storage); + print_var_int(iv, *iv->p_storage); break; } case IdentType::fvar: { Fvar *fv = static_cast(v); - print_var_float(fv, *fv->storage); + print_var_float(fv, *fv->p_storage); break; } case IdentType::svar: { Svar *sv = static_cast(v); - print_var_str(sv, *sv->storage); + print_var_str(sv, *sv->p_storage); break; } default: @@ -817,16 +843,16 @@ void CsState::set_var_int( Ivar *iv = static_cast(id); bool success = cs_override_var( *this, iv, iv->p_flags, - [&iv]() { iv->overrideval = *iv->storage; }, + [&iv]() { iv->p_overrideval = *iv->p_storage; }, []() {}, []() {} ); if (!success) { return; } if (doclamp) { - *iv->storage = ostd::clamp(v, iv->minval, iv->maxval); + *iv->p_storage = ostd::clamp(v, iv->get_val_min(), iv->get_val_max()); } else { - *iv->storage = v; + *iv->p_storage = v; } if (dofunc) { iv->changed(); @@ -843,16 +869,16 @@ void CsState::set_var_float( Fvar *fv = static_cast(id); bool success = cs_override_var( *this, fv, fv->p_flags, - [&fv]() { fv->overrideval = *fv->storage; }, + [&fv]() { fv->p_overrideval = *fv->p_storage; }, []() {}, []() {} ); if (!success) { return; } if (doclamp) { - *fv->storage = ostd::clamp(v, fv->minval, fv->maxval); + *fv->p_storage = ostd::clamp(v, fv->get_val_min(), fv->get_val_max()); } else { - *fv->storage = v; + *fv->p_storage = v; } if (dofunc) { fv->changed(); @@ -869,14 +895,14 @@ void CsState::set_var_str( Svar *sv = static_cast(id); bool success = cs_override_var( *this, sv, sv->p_flags, - [&sv]() { sv->overrideval = *sv->storage; }, - [&sv]() { delete[] sv->overrideval; }, - [&sv]() { delete[] *sv->storage; } + [&sv]() { sv->p_overrideval = *sv->p_storage; }, + [&sv]() { delete[] sv->p_overrideval; }, + [&sv]() { delete[] *sv->p_storage; } ); if (!success) { return; } - *sv->storage = cs_dup_ostr(v); + *sv->p_storage = cs_dup_ostr(v); if (dofunc) { sv->changed(); } @@ -887,7 +913,7 @@ ostd::Maybe CsState::get_var_int(ostd::ConstCharRange name) { if (!id || id->is_ivar()) { return ostd::nothing; } - return *static_cast(id)->storage; + return *static_cast(id)->p_storage; } ostd::Maybe CsState::get_var_float(ostd::ConstCharRange name) { @@ -895,7 +921,7 @@ ostd::Maybe CsState::get_var_float(ostd::ConstCharRange name) { if (!id || id->is_fvar()) { return ostd::nothing; } - return *static_cast(id)->storage; + return *static_cast(id)->p_storage; } ostd::Maybe CsState::get_var_str(ostd::ConstCharRange name) { @@ -903,7 +929,7 @@ ostd::Maybe CsState::get_var_str(ostd::ConstCharRange name) { if (!id || id->is_svar()) { return ostd::nothing; } - return ostd::String(*static_cast(id)->storage); + return ostd::String(*static_cast(id)->p_storage); } ostd::Maybe CsState::get_var_min_int(ostd::ConstCharRange name) { @@ -911,7 +937,7 @@ ostd::Maybe CsState::get_var_min_int(ostd::ConstCharRange name) { if (!id || id->is_ivar()) { return ostd::nothing; } - return static_cast(id)->minval; + return static_cast(id)->get_val_min(); } ostd::Maybe CsState::get_var_max_int(ostd::ConstCharRange name) { @@ -919,7 +945,7 @@ ostd::Maybe CsState::get_var_max_int(ostd::ConstCharRange name) { if (!id || id->is_ivar()) { return ostd::nothing; } - return static_cast(id)->maxval; + return static_cast(id)->get_val_max(); } ostd::Maybe CsState::get_var_min_float(ostd::ConstCharRange name) { @@ -927,7 +953,7 @@ ostd::Maybe CsState::get_var_min_float(ostd::ConstCharRange name) { if (!id || id->is_fvar()) { return ostd::nothing; } - return static_cast(id)->minval; + return static_cast(id)->get_val_min(); } ostd::Maybe CsState::get_var_max_float(ostd::ConstCharRange name) { @@ -935,7 +961,7 @@ ostd::Maybe CsState::get_var_max_float(ostd::ConstCharRange name) { if (!id || id->is_fvar()) { return ostd::nothing; } - return static_cast(id)->maxval; + return static_cast(id)->get_val_max(); } ostd::Maybe @@ -954,10 +980,10 @@ CsState::get_alias_val(ostd::ConstCharRange name) { } CsInt cs_clamp_var(CsState &cs, Ivar *iv, CsInt v) { - if (v < iv->minval) { - v = iv->minval; - } else if (v > iv->maxval) { - v = iv->maxval; + if (v < iv->get_val_min()) { + v = iv->get_val_min(); + } else if (v > iv->get_val_max()) { + v = iv->get_val_max(); } else { return v; } @@ -965,12 +991,12 @@ CsInt cs_clamp_var(CsState &cs, Ivar *iv, CsInt v) { cs, (iv->get_flags() & IDF_HEX) ? ( - (iv->minval <= 255) + (iv->get_val_min() <= 255) ? "valid range for '%s' is %d..0x%X" : "valid range for '%s' is 0x%X..0x%X" ) : "valid range for '%s' is %d..%d", - iv->get_name(), iv->minval, iv->maxval + iv->get_name(), iv->get_val_min(), iv->get_val_max() ); return v; } @@ -982,16 +1008,16 @@ void CsState::set_var_int_checked(Ivar *iv, CsInt v) { } bool success = cs_override_var( *this, iv, iv->p_flags, - [&iv]() { iv->overrideval = *iv->storage; }, + [&iv]() { iv->p_overrideval = *iv->p_storage; }, []() {}, []() {} ); if (!success) { return; } - if ((v < iv->minval) || (v > iv->maxval)) { + if ((v < iv->get_val_min()) || (v > iv->get_val_max())) { v = cs_clamp_var(*this, iv, v); } - *iv->storage = v; + *iv->p_storage = v; iv->changed(); } @@ -1007,16 +1033,16 @@ void CsState::set_var_int_checked(Ivar *iv, TvalRange args) { } CsFloat cs_clamp_fvar(CsState &cs, Fvar *fv, CsFloat v) { - if (v < fv->minval) { - v = fv->minval; - } else if (v > fv->maxval) { - v = fv->maxval; + if (v < fv->get_val_min()) { + v = fv->get_val_min(); + } else if (v > fv->get_val_max()) { + v = fv->get_val_max(); } else { return v; } cs_debug_code( - cs, "valid range for '%s' is %s..%s", floatstr(fv->minval), - floatstr(fv->maxval) + cs, "valid range for '%s' is %s..%s", floatstr(fv->get_val_min()), + floatstr(fv->get_val_max()) ); return v; } @@ -1028,16 +1054,16 @@ void CsState::set_var_float_checked(Fvar *fv, CsFloat v) { } bool success = cs_override_var( *this, fv, fv->p_flags, - [&fv]() { fv->overrideval = *fv->storage; }, + [&fv]() { fv->p_overrideval = *fv->p_storage; }, []() {}, []() {} ); if (!success) { return; } - if ((v < fv->minval) || (v > fv->maxval)) { + if ((v < fv->get_val_min()) || (v > fv->get_val_max())) { v = cs_clamp_fvar(*this, fv, v); } - *fv->storage = v; + *fv->p_storage = v; fv->changed(); } @@ -1048,14 +1074,14 @@ void CsState::set_var_str_checked(Svar *sv, ostd::ConstCharRange v) { } bool success = cs_override_var( *this, sv, sv->p_flags, - [&sv]() { sv->overrideval = *sv->storage; }, - [&sv]() { delete[] sv->overrideval; }, - [&sv]() { delete[] *sv->storage; } + [&sv]() { sv->p_overrideval = *sv->p_storage; }, + [&sv]() { delete[] sv->p_overrideval; }, + [&sv]() { delete[] *sv->p_storage; } ); if (!success) { return; } - *sv->storage = cs_dup_ostr(v); + *sv->p_storage = cs_dup_ostr(v); sv->changed(); } diff --git a/cubescript.hh b/cubescript.hh index b24408a9..fedc1a5b 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -228,6 +228,12 @@ private: using VarCb = ostd::Function; struct OSTD_EXPORT Var: Ident { + friend struct CsState; + +protected: + Var(IdentType tp, ostd::ConstCharRange name, VarCb func, int flags = 0); + +private: VarCb cb_var; void changed() { @@ -235,39 +241,57 @@ struct OSTD_EXPORT Var: Ident { cb_var(*this); } } - -protected: - Var(IdentType tp, ostd::ConstCharRange name, VarCb func, int flags = 0); }; struct OSTD_EXPORT Ivar: Var { - CsInt minval, maxval, overrideval; - CsInt *storage; + friend struct CsState; + + CsInt get_val_min() const; + CsInt get_val_max() const; + + CsInt get_var_value() const; Ivar( ostd::ConstCharRange n, CsInt m, CsInt x, CsInt *s, VarCb f = VarCb(), int flags = 0 ); + +private: + CsInt *p_storage; + CsInt p_minval, p_maxval, p_overrideval; }; struct OSTD_EXPORT Fvar: Var { - CsFloat minval, maxval, overrideval; - CsFloat *storage; + friend struct CsState; + + CsFloat get_val_min() const; + CsFloat get_val_max() const; + + CsFloat get_var_value() const; Fvar( ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat *s, VarCb f = VarCb(), int flags = 0 ); + +private: + CsFloat *p_storage; + CsFloat p_minval, p_maxval, p_overrideval; }; struct OSTD_EXPORT Svar: Var { - char *overrideval; - char **storage; + friend struct CsState; + + ostd::ConstCharRange get_var_value() const; Svar( ostd::ConstCharRange n, char **s, VarCb f = VarCb(), int flags = 0 ); + +private: + char **p_storage; + char *p_overrideval; }; struct OSTD_EXPORT Alias: Ident {