mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2026-07-25 08:22:17 +00:00
Codechange: properly decode VarType to determine whether the maximum value fits in the type
This commit is contained in:
@@ -26,18 +26,6 @@
|
||||
#include "signal_type.h"
|
||||
#include "timetable.h"
|
||||
|
||||
/** @{
|
||||
* Used to validate sizes of "max" value in settings. */
|
||||
const size_t MAX_SLE_UINT8 = UINT8_MAX;
|
||||
const size_t MAX_SLE_UINT16 = UINT16_MAX;
|
||||
const size_t MAX_SLE_UINT32 = UINT32_MAX;
|
||||
const size_t MAX_SLE_UINT = UINT_MAX;
|
||||
const size_t MAX_SLE_INT8 = INT8_MAX;
|
||||
const size_t MAX_SLE_INT16 = INT16_MAX;
|
||||
const size_t MAX_SLE_INT32 = INT32_MAX;
|
||||
const size_t MAX_SLE_INT = INT_MAX;
|
||||
/** @} */
|
||||
|
||||
/** Settings profiles and highscore tables. */
|
||||
enum SettingsProfile : uint8_t {
|
||||
SP_BEGIN = 0,
|
||||
|
||||
@@ -21,23 +21,35 @@ static StringID SettingHelpWallclock(const IntSettingDesc &sd);
|
||||
* DO NOT edit this file. This file is automatically generated from the contents of /src/table/
|
||||
*/
|
||||
|
||||
/* Helpers for validation. When deducing template arguments for functions overloads,
|
||||
* the compiler will create a 'partial ordering of overloaded function templates' by
|
||||
* preferring more specialized templates over more generic ones.
|
||||
* For scoped enums the first overload is more specialized and gets chosen. For signed
|
||||
* integers it is the second overload as we always compare with an unsigned limit. For anything
|
||||
* else, the concepts reject the specific overloads, leaving only the last overload. */
|
||||
|
||||
template <typename T> requires is_scoped_enum_v<T>
|
||||
constexpr auto ConvertEnumClass(T value)
|
||||
/**
|
||||
* Check whether the given maximum value fits within the range of the variable of the given type.
|
||||
* @param max The maximum value.
|
||||
* @param type The \c VarType to get the variable type from.
|
||||
* @return \c true iff \c max is less than or equal to the maximum value that can be stored in \c type.
|
||||
*/
|
||||
template <typename T>
|
||||
constexpr bool DoesMaxFitVariable(T max, VarType type)
|
||||
{
|
||||
return to_underlying(value);
|
||||
switch (GetVarMemType(type)) {
|
||||
case SLE_VAR_BL: return max <= 1;
|
||||
case SLE_VAR_I8: return max <= INT8_MAX;
|
||||
case SLE_VAR_U8: return std::make_unsigned_t<T>(max) <= UINT8_MAX;
|
||||
case SLE_VAR_I16: return max <= INT16_MAX;
|
||||
case SLE_VAR_U16: return std::make_unsigned_t<T>(max) <= UINT16_MAX;
|
||||
case SLE_VAR_I32: return max <= INT32_MAX;
|
||||
case SLE_VAR_U32: return std::make_unsigned_t<T>(max) <= UINT32_MAX;
|
||||
/* 64 bits variables are not supported by the settings logic. For strings checking the maximum makes no sense. */
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
template <std::signed_integral T>
|
||||
constexpr auto ConvertEnumClass(T value) { return std::make_unsigned_t<T>(value); }
|
||||
/** @copydoc DoesMaxFitVariable */
|
||||
template <typename T> requires is_scoped_enum_v<T>
|
||||
constexpr bool DoesMaxFitVariable(T max, VarType type) { return DoesMaxFitVariable(to_underlying(max), type); }
|
||||
|
||||
template <typename T> constexpr T ConvertEnumClass(T value) { return value; }
|
||||
/** @copydoc DoesMaxFitVariable */
|
||||
template <typename T> requires ConvertibleThroughBase<T>
|
||||
constexpr bool DoesMaxFitVariable(T max, VarType type) { return DoesMaxFitVariable(max.base(), type); }
|
||||
|
||||
/**
|
||||
* Settings-macro usage:
|
||||
|
||||
@@ -24,7 +24,7 @@ SDT_BOOL = SDT_BOOL(CompanySettings, $var, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR(CompanySettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for CompanySettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for CompanySettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags = SettingFlag::PerCompany
|
||||
|
||||
@@ -15,7 +15,7 @@ SDT_VAR = SDT_VAR (CurrencySpec, $var, $type, SettingFlags({$flags}), $def, $mi
|
||||
SDT_SSTR = SDT_SSTR(CurrencySpec, $var, $type, SettingFlags({$flags}), $def, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for CurrencySpec.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for CurrencySpec.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||
|
||||
@@ -28,8 +28,8 @@ SDT_VAR = SDT_VAR (GameSettings, $var, $type, SettingFlags({$flags}), $def,
|
||||
SDT_OMANY = SDT_OMANY(GameSettings, $var, $type, SettingFlags({$flags}), $def, $max, $full, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $from, $to, $load, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTG_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDTG_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -23,7 +23,7 @@ SDT_BOOL = SDT_BOOL(GameSettings, $var, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR (GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -35,9 +35,9 @@ SDT_OMANY = SDT_OMANY(GameSettings, $var, $type, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTG_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDT_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDTG_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDT_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -32,8 +32,8 @@ SDTC_OMANY = SDTC_OMANY( $var, $type, SettingFlags({$flags}), $def,
|
||||
SDTC_VAR = SDTC_VAR( $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTC_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDTC_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -15,7 +15,7 @@ static const SettingVariant _linkgraph_settings_table[] = {
|
||||
SDT_VAR = SDT_VAR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -26,8 +26,8 @@ SDT_OMANY = SDT_OMANY(GameSettings, $var, $type, SettingFlags({$flags}), $def,
|
||||
SDT_SSTR = SDT_SSTR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTG_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDT_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDTG_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDT_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -33,8 +33,8 @@ SDTG_BOOL = SDTG_BOOL($name, SettingFlags({$flags}), $var, $def,
|
||||
SDTG_VAR = SDTG_VAR($name, $type, SettingFlags({$flags}), $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTG_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTG_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTG_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDTG_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||
|
||||
@@ -17,7 +17,7 @@ SDTC_LIST = SDTC_LIST( $var, $type, SettingFlags({$flags}), $def,
|
||||
SDTC_VAR = SDTC_VAR( $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTC_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -19,7 +19,7 @@ SDTC_OMANY = SDTC_OMANY( $var, $type, SettingFlags({$flags}), $def,
|
||||
SDTC_SSTR = SDTC_SSTR( $var, $type, SettingFlags({$flags}), $def, $length, $pre_cb, $post_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTC_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -21,8 +21,8 @@ SDTC_OMANY = SDTC_OMANY( $var, $type, SettingFlags({$flags}), $def,
|
||||
SDTC_VAR = SDTC_VAR( $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTC_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDTC_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -16,7 +16,7 @@ static const SettingVariant _news_display_settings_table[] = {
|
||||
SDTC_OMANY = SDTC_OMANY( $var, $type, SettingFlags({$flags}), $def, $max, $full, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTC_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -39,11 +39,11 @@ SDT_OMANY = SDT_OMANY(GameSettings, $var, $type, SettingFlags({$flags}), $de
|
||||
SDT_VAR = SDT_VAR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTG_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTG_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTC_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDT_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDTG_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDTG_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDTC_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
SDT_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -16,7 +16,7 @@ SDT_BOOL = SDT_BOOL(GameSettings, $var, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -18,8 +18,8 @@ SDT_OMANY = SDT_OMANY(GameSettings, $var, $type, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
@@ -21,7 +21,7 @@ SDTG_BOOL = SDTG_BOOL($name, SettingFlags({$flags}), $var, $def,
|
||||
SDTG_VAR = SDTG_VAR($name, $type, SettingFlags({$flags}), $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDTG_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for $var exceeds storage size");
|
||||
SDTG_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for $var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||
|
||||
@@ -17,7 +17,7 @@ SDT_BOOL = SDT_BOOL(WindowDesc, $var, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR(WindowDesc, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for WindowDesc.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for WindowDesc.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||
|
||||
@@ -23,8 +23,8 @@ SDT_OMANY = SDT_OMANY(GameSettings, $var, $type, SettingFlags({$flags}), $def,
|
||||
SDT_VAR = SDT_VAR(GameSettings, $var, $type, SettingFlags({$flags}), $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $def_cb, $range_cb, $from, $to, $cat, $extra, $startup),
|
||||
|
||||
[validation]
|
||||
SDT_OMANY = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(ConvertEnumClass($max) <= MAX_$type, "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_OMANY = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
SDT_VAR = static_assert(DoesMaxFitVariable($max, $type), "Maximum value for GameSettings.$var exceeds storage size");
|
||||
|
||||
[defaults]
|
||||
flags =
|
||||
|
||||
Reference in New Issue
Block a user