From 1111251887e7e01cad96192e9a7c2a9af2985d7d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 22 Oct 2022 09:16:02 +0200 Subject: [PATCH] type safe CVAR declaration. --- source/common/audio/music/music_config.cpp | 20 ++++----- source/common/console/c_cvars.h | 42 +++++++++++-------- source/common/engine/d_event.cpp | 4 +- .../rendering/hwrenderer/data/hw_cvars.cpp | 2 +- .../rendering/hwrenderer/data/hw_skydome.cpp | 2 +- source/common/statusbar/base_sbar.cpp | 2 +- source/core/console/c_notifybuffer.cpp | 2 +- source/core/gamecvars.cpp | 2 +- source/core/rendering/scene/hw_skyportal.cpp | 2 +- 9 files changed, 43 insertions(+), 35 deletions(-) diff --git a/source/common/audio/music/music_config.cpp b/source/common/audio/music/music_config.cpp index ee8570611..efdb05637 100644 --- a/source/common/audio/music/music_config.cpp +++ b/source/common/audio/music/music_config.cpp @@ -75,12 +75,12 @@ CUSTOM_CVAR(Int, adl_emulator_id, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIR FORWARD_CVAR(adl_emulator_id); } -CUSTOM_CVAR(Bool, adl_run_at_pcm_rate, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Bool, adl_run_at_pcm_rate, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_BOOL_CVAR(adl_run_at_pcm_rate); } -CUSTOM_CVAR(Bool, adl_fullpan, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Bool, adl_fullpan, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_BOOL_CVAR(adl_fullpan); } @@ -90,7 +90,7 @@ CUSTOM_CVAR(Int, adl_bank, 14, CVAR_ARCHIVE | CVAR_VIRTUAL) FORWARD_CVAR(adl_bank); } -CUSTOM_CVAR(Bool, adl_use_custom_bank, 0, CVAR_ARCHIVE | CVAR_VIRTUAL) +CUSTOM_CVAR(Bool, adl_use_custom_bank, false, CVAR_ARCHIVE | CVAR_VIRTUAL) { FORWARD_BOOL_CVAR(adl_use_custom_bank); } @@ -201,7 +201,7 @@ CUSTOM_CVAR(Float, fluid_chorus_speed, 0.3f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | } // depth is in ms and actual maximum depends on the sample rate -CUSTOM_CVAR(Float, fluid_chorus_depth, 8, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Float, fluid_chorus_depth, 8.f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_CVAR(fluid_chorus_depth); } @@ -251,17 +251,17 @@ CUSTOM_CVAR(Int, opn_emulator_id, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIR FORWARD_CVAR(opn_emulator_id); } -CUSTOM_CVAR(Bool, opn_run_at_pcm_rate, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Bool, opn_run_at_pcm_rate, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_BOOL_CVAR(opn_run_at_pcm_rate); } -CUSTOM_CVAR(Bool, opn_fullpan, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Bool, opn_fullpan, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_BOOL_CVAR(opn_fullpan); } -CUSTOM_CVAR(Bool, opn_use_custom_bank, 0, CVAR_ARCHIVE | CVAR_VIRTUAL) +CUSTOM_CVAR(Bool, opn_use_custom_bank, false, CVAR_ARCHIVE | CVAR_VIRTUAL) { FORWARD_BOOL_CVAR(opn_use_custom_bank); } @@ -344,7 +344,7 @@ CUSTOM_CVAR(Bool, timidity_channel_pressure, false, CVAR_ARCHIVE | CVAR_GLOBALCO FORWARD_BOOL_CVAR(timidity_channel_pressure); } -CUSTOM_CVAR(Int, timidity_lpf_def, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Int, timidity_lpf_def, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_CVAR(timidity_lpf_def); } @@ -384,12 +384,12 @@ CUSTOM_CVAR(Int, timidity_key_adjust, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR FORWARD_CVAR(timidity_key_adjust); } -CUSTOM_CVAR(Float, timidity_tempo_adjust, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Float, timidity_tempo_adjust, 1.f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_CVAR(timidity_tempo_adjust); } -CUSTOM_CVAR(Float, timidity_min_sustain_time, 5000, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) +CUSTOM_CVAR(Float, timidity_min_sustain_time, 5000.f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_CVAR(timidity_min_sustain_time); } diff --git a/source/common/console/c_cvars.h b/source/common/console/c_cvars.h index a4fe371d9..d60771d2d 100644 --- a/source/common/console/c_cvars.h +++ b/source/common/console/c_cvars.h @@ -74,6 +74,19 @@ enum CVAR_CONFIG_ONLY = 1 << 18, // do not save var to savegame and do not send it across network. }; +enum ECVarType +{ + CVAR_Bool, + CVAR_Int, + CVAR_Float, + CVAR_String, + CVAR_Color, // stored as CVAR_Int + CVAR_Flag, // just redirects to another cvar + CVAR_Mask, // just redirects to another cvar + CVAR_Dummy, // Unknown +}; + + class FIntCVarRef; union UCVarValue { @@ -92,17 +105,12 @@ union UCVarValue constexpr UCVarValue(FIntCVarRef& v); }; -enum ECVarType -{ - CVAR_Bool, - CVAR_Int, - CVAR_Float, - CVAR_String, - CVAR_Color, // stored as CVAR_Int - CVAR_Flag, // just redirects to another cvar - CVAR_Mask, // just redirects to another cvar - CVAR_Dummy, // Unknown -}; +template constexpr UCVarValue CVarValue(bool v) { static_assert(t == CVAR_Bool); return v; } +template constexpr UCVarValue CVarValue(int v) { static_assert(t == CVAR_Int || t == CVAR_Color); return v; } +template constexpr UCVarValue CVarValue(float v) { static_assert(t == CVAR_Float); return v; } +template constexpr UCVarValue CVarValue(double v) { static_assert(t == CVAR_Float); return v; } +template constexpr UCVarValue CVarValue(const char* v) { static_assert(t == CVAR_String); return v; } +template constexpr UCVarValue CVarValue(FIntCVarRef& v) { static_assert(t == CVAR_Flag || t == CVAR_Mask); return v; } class FConfigFile; @@ -598,7 +606,7 @@ void C_ForgetCVars (void); #define CUSTOM_CVAR(type,name,def,flags) \ static void cvarfunc_##name(F##type##CVar &); \ F##type##CVarRef name; \ - static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, def, nullptr, reinterpret_cast(cvarfunc_##name) }; \ + static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, CVarValue(def), nullptr, reinterpret_cast(cvarfunc_##name) }; \ extern FCVarDecl const *const cvardeclref_##name; \ MSVC_VSEG FCVarDecl const *const cvardeclref_##name GCC_VSEG = &cvardecl_##name; \ static void cvarfunc_##name(F##type##CVar &self) @@ -607,14 +615,14 @@ void C_ForgetCVars (void); #define CUSTOM_CVAR_NAMED(type,name,cname,def,flags) \ static void cvarfunc_##name(F##type##CVar &); \ F##type##CVarRef name; \ - static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #cname, def, nullptr, reinterpret_cast(cvarfunc_##name) }; \ + static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #cname, CVarValue(def), nullptr, reinterpret_cast(cvarfunc_##name) }; \ extern FCVarDecl const *const cvardeclref_##name; \ MSVC_VSEG FCVarDecl const *const cvardeclref_##name GCC_VSEG = &cvardecl_##name; \ static void cvarfunc_##name(F##type##CVar &self) #define CVAR(type,name,def,flags) \ F##type##CVarRef name; \ - static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, def, nullptr, nullptr}; \ + static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, CVarValue(def), nullptr, nullptr}; \ extern FCVarDecl const *const cvardeclref_##name; \ MSVC_VSEG FCVarDecl const *const cvardeclref_##name GCC_VSEG = &cvardecl_##name; @@ -623,20 +631,20 @@ void C_ForgetCVars (void); #define CUSTOM_CVARD(type,name,def,flags,descr) \ static void cvarfunc_##name(F##type##CVar &); \ F##type##CVarRef name; \ - static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, def, descr, reinterpret_cast(cvarfunc_##name) }; \ + static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, CVarValue(def), descr, reinterpret_cast(cvarfunc_##name) }; \ extern FCVarDecl const *const cvardeclref_##name; \ MSVC_VSEG FCVarDecl const *const cvardeclref_##name GCC_VSEG = &cvardecl_##name; \ static void cvarfunc_##name(F##type##CVar &self) #define CVARD(type,name,def,flags, descr) \ F##type##CVarRef name; \ - static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, def, descr, nullptr}; \ + static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #name, CVarValue(def), descr, nullptr}; \ extern FCVarDecl const *const cvardeclref_##name; \ MSVC_VSEG FCVarDecl const *const cvardeclref_##name GCC_VSEG = &cvardecl_##name; #define CVARD_NAMED(type,name,varname,def,flags, descr) \ F##type##CVarRef name; \ - static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #varname, def, descr, nullptr}; \ + static FCVarDecl cvardecl_##name = { &name, CVAR_##type, (flags), #varname, CVarValue(def), descr, nullptr}; \ extern FCVarDecl const *const cvardeclref_##name; \ MSVC_VSEG FCVarDecl const *const cvardeclref_##name GCC_VSEG = &cvardecl_##name; diff --git a/source/common/engine/d_event.cpp b/source/common/engine/d_event.cpp index 9c2684455..b64aa6365 100644 --- a/source/common/engine/d_event.cpp +++ b/source/common/engine/d_event.cpp @@ -50,8 +50,8 @@ int eventhead; int eventtail; event_t events[MAXEVENTS]; -CVAR(Float, m_sensitivity_x, 4, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) -CVAR(Float, m_sensitivity_y, 2, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +CVAR(Float, m_sensitivity_x, 4.f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +CVAR(Float, m_sensitivity_y, 2.f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) //========================================================================== diff --git a/source/common/rendering/hwrenderer/data/hw_cvars.cpp b/source/common/rendering/hwrenderer/data/hw_cvars.cpp index 14f5f2a87..a7967b92e 100644 --- a/source/common/rendering/hwrenderer/data/hw_cvars.cpp +++ b/source/common/rendering/hwrenderer/data/hw_cvars.cpp @@ -115,7 +115,7 @@ CVAR(Int, gl_satformula, 1, CVAR_ARCHIVE|CVAR_GLOBALCONFIG); // Texture CVARs // //========================================================================== -CUSTOM_CVARD(Float, gl_texture_filter_anisotropic, 8, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL, "changes the OpenGL texture anisotropy setting") +CUSTOM_CVARD(Float, gl_texture_filter_anisotropic, 8.f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL, "changes the OpenGL texture anisotropy setting") { screen->SetTextureFilterMode(); } diff --git a/source/common/rendering/hwrenderer/data/hw_skydome.cpp b/source/common/rendering/hwrenderer/data/hw_skydome.cpp index 9d1f76ab5..eae09ffbb 100644 --- a/source/common/rendering/hwrenderer/data/hw_skydome.cpp +++ b/source/common/rendering/hwrenderer/data/hw_skydome.cpp @@ -70,7 +70,7 @@ // also shamelessly lifted from ZDoomGL! ;) // //----------------------------------------------------------------------------- -CVAR(Float, skyoffset, 0, 0) // for testing +CVAR(Float, skyoffset, 0.f, 0) // for testing struct SkyColor diff --git a/source/common/statusbar/base_sbar.cpp b/source/common/statusbar/base_sbar.cpp index 9e0431610..d51c5f79f 100644 --- a/source/common/statusbar/base_sbar.cpp +++ b/source/common/statusbar/base_sbar.cpp @@ -61,7 +61,7 @@ CVAR(Int, crosshairhealth, 2, CVAR_ARCHIVE); CVARD(Float, crosshairscale, 0.5, CVAR_ARCHIVE, "changes the size of the crosshair"); CVAR(Bool, crosshairgrow, false, CVAR_ARCHIVE); -CUSTOM_CVARD(Float, hud_scalefactor, 1, CVAR_ARCHIVE, "changes the hud scale") +CUSTOM_CVARD(Float, hud_scalefactor, 1.f, CVAR_ARCHIVE, "changes the hud scale") { if (self < 0.36f) self = 0.36f; else if (self > 1) self = 1; diff --git a/source/core/console/c_notifybuffer.cpp b/source/core/console/c_notifybuffer.cpp index 56db8edf6..d29c6a3ce 100644 --- a/source/core/console/c_notifybuffer.cpp +++ b/source/core/console/c_notifybuffer.cpp @@ -72,7 +72,7 @@ CUSTOM_CVAR(Int, con_notifylines, 4, CVAR_GLOBALCONFIG | CVAR_ARCHIVE) NotifyStrings.Shift(self); } -CUSTOM_CVAR(Float, con_notifyscale, 1, CVAR_ARCHIVE) +CUSTOM_CVAR(Float, con_notifyscale, 1.f, CVAR_ARCHIVE) { if (self < 0.36f) self = 0.36f; if (self > 1) self = 1; diff --git a/source/core/gamecvars.cpp b/source/core/gamecvars.cpp index 4ea844db0..867f5787c 100644 --- a/source/core/gamecvars.cpp +++ b/source/core/gamecvars.cpp @@ -186,7 +186,7 @@ CUSTOM_CVARD(Int, hud_stats, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "enable/disabl CVARD(Bool, hud_showmapname, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enable/disable map name display on load") CVARD(Bool, hud_position, false, CVAR_ARCHIVE, "aligns the status bar to the bottom/top") CVARD(Bool, hud_bgstretch, false, CVAR_ARCHIVE, "enable/disable background image stretching in wide resolutions") -CVARD(Bool, hud_messages, 1, CVAR_ARCHIVE, "enable/disable showing messages") +CVARD(Bool, hud_messages, true, CVAR_ARCHIVE, "enable/disable showing messages") // This cannot be done with the 'toggle' CCMD because it needs to control itself when to output the message CCMD (togglemessages) diff --git a/source/core/rendering/scene/hw_skyportal.cpp b/source/core/rendering/scene/hw_skyportal.cpp index 871288f51..02abd5c8a 100644 --- a/source/core/rendering/scene/hw_skyportal.cpp +++ b/source/core/rendering/scene/hw_skyportal.cpp @@ -26,7 +26,7 @@ #include "hw_renderstate.h" #include "skyboxtexture.h" -CVAR(Float, skyoffsettest, 0, 0) +CVAR(Float, skyoffsettest, 0.f, 0) //----------------------------------------------------------------------------- //