1
0
Fork 0

Codechange: let OneOfMany and ManyOfMany be their own classes as well

pull/9302/head
rubidium42 2021-05-23 19:16:56 +02:00 committed by rubidium42
parent 860003458f
commit e666a962b1
5 changed files with 123 additions and 143 deletions

View File

@ -127,31 +127,23 @@ static const char * const _list_group_names[] = {
/** /**
* Find the index value of a ONEofMANY type in a string separated by | * Find the index value of a ONEofMANY type in a string separated by |
* @param str the current value of the setting for which a value needs found
* @param len length of the string
* @param many full domain of values the ONEofMANY setting can have * @param many full domain of values the ONEofMANY setting can have
* @param one the current value of the setting for which a value needs found
* @param onelen force calculation of the *one parameter
* @return the integer index of the full-list, or -1 if not found * @return the integer index of the full-list, or -1 if not found
*/ */
static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0) size_t OneOfManySettingDesc::ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many)
{ {
const char *s;
size_t idx;
if (onelen == 0) onelen = strlen(one);
/* check if it's an integer */ /* check if it's an integer */
if (*one >= '0' && *one <= '9') return strtoul(one, nullptr, 0); if (isdigit(*str)) return strtoul(str, nullptr, 0);
idx = 0; size_t idx = 0;
for (;;) { for (auto one : many) {
/* find end of item */ if (one.size() == len && strncmp(one.c_str(), str, len) == 0) return idx;
s = many;
while (*s != '|' && *s != 0) s++;
if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
if (*s == 0) return (size_t)-1;
many = s + 1;
idx++; idx++;
} }
return (size_t)-1;
} }
/** /**
@ -161,7 +153,7 @@ static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen =
* of separated by a whitespace,tab or | character * of separated by a whitespace,tab or | character
* @return the 'fully' set integer, or -1 if a set is not found * @return the 'fully' set integer, or -1 if a set is not found
*/ */
static size_t LookupManyOfMany(const char *many, const char *str) static size_t LookupManyOfMany(const std::vector<std::string> &many, const char *str)
{ {
const char *s; const char *s;
size_t r; size_t r;
@ -175,7 +167,7 @@ static size_t LookupManyOfMany(const char *many, const char *str)
s = str; s = str;
while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++; while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
r = LookupOneOfMany(many, str, s - str); r = OneOfManySettingDesc::ParseSingleValue(str, s - str, many);
if (r == (size_t)-1) return r; if (r == (size_t)-1) return r;
SetBit(res, (uint8)r); // value found, set it SetBit(res, (uint8)r); // value found, set it
@ -311,66 +303,30 @@ void ListSettingDesc::FormatValue(char *buf, const char *last, const void *objec
} }
} }
/** char *OneOfManySettingDesc::FormatSingleValue(char *buf, const char *last, uint id) const
* Convert a ONEofMANY structure to a string representation.
* @param buf output buffer where the string-representation will be stored
* @param last last item to write to in the output buffer
* @param many the full-domain string of possible values
* @param id the value of the variable and whose string-representation must be found
*/
static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
{ {
int orig_id = id; if (id >= this->many.size()) {
return buf + seprintf(buf, last, "%d", id);
/* Look for the id'th element */
while (--id >= 0) {
for (; *many != '|'; many++) {
if (*many == '\0') { // not found
seprintf(buf, last, "%d", orig_id);
return;
}
}
many++; // pass the |-character
} }
return strecpy(buf, this->many[id].c_str(), last);
/* copy string until next item (|) or the end of the list if this is the last one */
while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
*buf = '\0';
} }
/** void OneOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
* Convert a MANYofMANY structure to a string representation.
* @param buf output buffer where the string-representation will be stored
* @param last last item to write to in the output buffer
* @param many the full-domain string of possible values
* @param x the value of the variable and whose string-representation must
* be found in the bitmasked many string
*/
static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
{ {
const char *start; uint id = (uint)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
int i = 0; this->FormatSingleValue(buf, last, id);
bool init = true; }
for (; x != 0; x >>= 1, i++) { void ManyOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
start = many; {
while (*many != 0 && *many != '|') many++; // advance to the next element uint bitmask = (uint)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
uint id = 0;
if (HasBit(x, 0)) { // item found, copy it bool first = true;
if (!init) buf += seprintf(buf, last, "|"); FOR_EACH_SET_BIT(id, bitmask) {
init = false; if (!first) buf = strecpy(buf, "|", last);
if (start == many) { buf = this->FormatSingleValue(buf, last, id);
buf += seprintf(buf, last, "%d", i); first = false;
} else {
memcpy(buf, start, many - start);
buf += many - start;
}
}
if (*many == '|') many++;
} }
*buf = '\0';
} }
/** /**
@ -380,52 +336,46 @@ static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32
*/ */
size_t IntSettingDesc::ParseValue(const char *str) const size_t IntSettingDesc::ParseValue(const char *str) const
{ {
switch (this->cmd) { char *end;
case SDT_NUMX: { size_t val = strtoul(str, &end, 0);
char *end; if (end == str) {
size_t val = strtoul(str, &end, 0); ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
if (end == str) { msg.SetDParamStr(0, str);
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE); msg.SetDParamStr(1, this->name);
msg.SetDParamStr(0, str); _settings_error_list.push_back(msg);
msg.SetDParamStr(1, this->name); return this->def;
_settings_error_list.push_back(msg);
break;
}
if (*end != '\0') {
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
msg.SetDParamStr(0, this->name);
_settings_error_list.push_back(msg);
}
return val;
}
case SDT_ONEOFMANY: {
size_t r = LookupOneOfMany(this->many, str);
/* if the first attempt of conversion from string to the appropriate value fails,
* look if we have defined a converter from old value to new value. */
if (r == (size_t)-1 && this->many_cnvt != nullptr) r = this->many_cnvt(str);
if (r != (size_t)-1) return r; // and here goes converted value
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, this->name);
_settings_error_list.push_back(msg);
break;
}
case SDT_MANYOFMANY: {
size_t r = LookupManyOfMany(this->many, str);
if (r != (size_t)-1) return r;
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, this->name);
_settings_error_list.push_back(msg);
break;
}
default: NOT_REACHED();
} }
if (*end != '\0') {
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
msg.SetDParamStr(0, this->name);
_settings_error_list.push_back(msg);
}
return val;
}
size_t OneOfManySettingDesc::ParseValue(const char *str) const
{
size_t r = OneOfManySettingDesc::ParseSingleValue(str, strlen(str), this->many);
/* if the first attempt of conversion from string to the appropriate value fails,
* look if we have defined a converter from old value to new value. */
if (r == (size_t)-1 && this->many_cnvt != nullptr) r = this->many_cnvt(str);
if (r != (size_t)-1) return r; // and here goes converted value
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, this->name);
_settings_error_list.push_back(msg);
return this->def;
}
size_t ManyOfManySettingDesc::ParseValue(const char *str) const
{
size_t r = LookupManyOfMany(this->many, str);
if (r != (size_t)-1) return r;
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, this->name);
_settings_error_list.push_back(msg);
return this->def; return this->def;
} }
@ -663,12 +613,7 @@ static void IniSaveSettings(IniFile *ini, const SettingTable &settings_table, co
void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
{ {
uint32 i = (uint32)ReadValue(GetVariableAddress(object, &this->save), this->save.conv); uint32 i = (uint32)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
switch (this->cmd) { seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : (this->save.conv & SLF_HEX) ? "%X" : "%u", i);
case SDT_NUMX: seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : (this->save.conv & SLF_HEX) ? "%X" : "%u", i); break;
case SDT_ONEOFMANY: MakeOneOfMany(buf, last, this->many, i); break;
case SDT_MANYOFMANY: MakeManyOfMany(buf, last, this->many, i); break;
default: NOT_REACHED();
}
} }
void BoolSettingDesc::FormatValue(char *buf, const char *last, const void *object) const void BoolSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
@ -1263,7 +1208,8 @@ static bool CheckRoadSide(int p1)
static size_t ConvertLandscape(const char *value) static size_t ConvertLandscape(const char *value)
{ {
/* try with the old values */ /* try with the old values */
return LookupOneOfMany("normal|hilly|desert|candy", value); static std::vector<std::string> _old_landscape_values{"normal", "hilly", "desert", "candy"};
return OneOfManySettingDesc::ParseSingleValue(value, strlen(value), _old_landscape_values);
} }
static bool CheckFreeformEdges(int32 p1) static bool CheckFreeformEdges(int32 p1)

View File

@ -133,9 +133,9 @@ struct SettingDesc {
struct IntSettingDesc : SettingDesc { struct IntSettingDesc : SettingDesc {
IntSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, int32 def, IntSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, int32 def,
int32 min, uint32 max, int32 interval, StringID str, StringID str_help, StringID str_val, int32 min, uint32 max, int32 interval, StringID str, StringID str_help, StringID str_val,
SettingCategory cat, OnChange *proc, const char *many = nullptr, OnConvert *many_cnvt = nullptr) : SettingCategory cat, OnChange *proc) :
SettingDesc(save, name, flags, cmd, startup), def(def), min(min), max(max), interval(interval), SettingDesc(save, name, flags, cmd, startup), def(def), min(min), max(max), interval(interval),
str(str), str_help(str_help), str_val(str_val), cat(cat), proc(proc), many(many), many_cnvt(many_cnvt) {} str(str), str_help(str_help), str_val(str_val), cat(cat), proc(proc) {}
virtual ~IntSettingDesc() {} virtual ~IntSettingDesc() {}
int32 def; ///< default value given when none is present int32 def; ///< default value given when none is present
@ -147,8 +147,6 @@ struct IntSettingDesc : SettingDesc {
StringID str_val; ///< (Translated) first string describing the value. StringID str_val; ///< (Translated) first string describing the value.
SettingCategory cat; ///< assigned categories of the setting SettingCategory cat; ///< assigned categories of the setting
OnChange *proc; ///< callback procedure for when the value is changed OnChange *proc; ///< callback procedure for when the value is changed
const char *many; ///< ONE/MANY_OF_MANY: string of possible values for this type
OnConvert *many_cnvt; ///< callback procedure when loading value mechanism fails
void ChangeValue(const void *object, int32 newvalue) const; void ChangeValue(const void *object, int32 newvalue) const;
void Write_ValidateSetting(const void *object, int32 value) const; void Write_ValidateSetting(const void *object, int32 value) const;
@ -170,6 +168,41 @@ struct BoolSettingDesc : IntSettingDesc {
void FormatValue(char *buf, const char *last, const void *object) const override; void FormatValue(char *buf, const char *last, const void *object) const override;
}; };
/** One of many setting. */
struct OneOfManySettingDesc : IntSettingDesc {
OneOfManySettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup,
int32 def, int32 max, StringID str, StringID str_help, StringID str_val, SettingCategory cat, OnChange *proc,
std::initializer_list<const char *> many, OnConvert *many_cnvt) :
IntSettingDesc(save, name, flags, cmd, startup, def, 0, max, 0, str, str_help, str_val, cat, proc), many_cnvt(many_cnvt)
{
for (auto one : many) this->many.push_back(one);
}
virtual ~OneOfManySettingDesc() {}
std::vector<std::string> many; ///< possible values for this type
OnConvert *many_cnvt; ///< callback procedure when loading value mechanism fails
static size_t ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many);
char *FormatSingleValue(char *buf, const char *last, uint id) const;
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
};
/** Many of many setting. */
struct ManyOfManySettingDesc : OneOfManySettingDesc {
ManyOfManySettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup,
int32 def, StringID str, StringID str_help, StringID str_val, SettingCategory cat, OnChange *proc,
std::initializer_list<const char *> many, OnConvert *many_cnvt) :
OneOfManySettingDesc(save, name, flags, cmd, startup, def, (1 << many.size()) - 1, str, str_help,
str_val, cat, proc, many, many_cnvt) {}
virtual ~ManyOfManySettingDesc() {}
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
};
/** String settings. */ /** String settings. */
struct StringSettingDesc : SettingDesc { struct StringSettingDesc : SettingDesc {
StringSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, const char *def, StringSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, const char *def,

View File

@ -12,16 +12,16 @@ uint8 _old_units; ///< Old units from old s
/* Most of these strings are used both for gameopt-backward compatibility /* Most of these strings are used both for gameopt-backward compatibility
* and the settings tables. The rest is here for consistency. */ * and the settings tables. The rest is here for consistency. */
static const char *_locale_currencies = "GBP|USD|EUR|YEN|ATS|BEF|CHF|CZK|DEM|DKK|ESP|FIM|FRF|GRD|HUF|ISK|ITL|NLG|NOK|PLN|RON|RUR|SIT|SEK|YTL|SKK|BRL|EEK|custom"; static std::initializer_list<const char*> _locale_currencies{"GBP", "USD", "EUR", "YEN", "ATS", "BEF", "CHF", "CZK", "DEM", "DKK", "ESP", "FIM", "FRF", "GRD", "HUF", "ISK", "ITL", "NLG", "NOK", "PLN", "RON", "RUR", "SIT", "SEK", "YTL", "SKK", "BRL", "EEK", "custom"};
static const char *_locale_units = "imperial|metric|si|gameunits"; static std::initializer_list<const char*> _locale_units{"imperial", "metric", "si", "gameunits"};
static const char *_town_names = "english|french|german|american|latin|silly|swedish|dutch|finnish|polish|slovak|norwegian|hungarian|austrian|romanian|czech|swiss|danish|turkish|italian|catalan"; static std::initializer_list<const char*> _town_names{"english", "french", "german", "american", "latin", "silly", "swedish", "dutch", "finnish", "polish", "slovak", "norwegian", "hungarian", "austrian", "romanian", "czech", "swiss", "danish", "turkish", "italian", "catalan"};
static const char *_climates = "temperate|arctic|tropic|toyland"; static std::initializer_list<const char*> _climates{"temperate", "arctic", "tropic", "toyland"};
static const char *_autosave_interval = "off|monthly|quarterly|half year|yearly"; static std::initializer_list<const char*> _autosave_interval{"off", "monthly", "quarterly", "half year", "yearly"};
static const char *_roadsides = "left|right"; static std::initializer_list<const char*> _roadsides{"left", "right"};
static const char *_savegame_date = "long|short|iso"; static std::initializer_list<const char*> _savegame_date{"long", "short", "iso"};
static const char *_osk_activation = "disabled|double|single|immediately"; static std::initializer_list<const char*> _osk_activation{"disabled", "double", "single", "immediately"};
static const char *_settings_profiles = "easy|medium|hard"; static std::initializer_list<const char*> _settings_profiles{"easy", "medium", "hard"};
static const char *_news_display = "off|summarized|full"; static std::initializer_list<const char*> _news_display{ "off", "summarized", "full"};
static const SettingTable _gameopt_settings{ static const SettingTable _gameopt_settings{
/* In version 4 a new difficulty setting has been added to the difficulty settings, /* In version 4 a new difficulty setting has been added to the difficulty settings,

View File

@ -7,7 +7,8 @@
[pre-amble] [pre-amble]
extern std::string _config_language_file; extern std::string _config_language_file;
static const char *_support8bppmodes = "no|system|hardware"; static std::initializer_list<const char*> _support8bppmodes{"no", "system" , "hardware"};
static std::initializer_list<const char*> _display_opt_modes{"SHOW_TOWN_NAMES", "SHOW_STATION_NAMES", "SHOW_SIGNS", "FULL_ANIMATION", "", "FULL_DETAIL", "WAYPOINTS", "SHOW_COMPETITOR_SIGNS"};
#ifdef WITH_COCOA #ifdef WITH_COCOA
extern bool _allow_hidpi_window; extern bool _allow_hidpi_window;
@ -53,7 +54,7 @@ name = ""display_opt""
type = SLE_UINT8 type = SLE_UINT8
var = _display_opt var = _display_opt
def = (1 << DO_SHOW_TOWN_NAMES | 1 << DO_SHOW_STATION_NAMES | 1 << DO_SHOW_SIGNS | 1 << DO_FULL_ANIMATION | 1 << DO_FULL_DETAIL | 1 << DO_SHOW_WAYPOINT_NAMES | 1 << DO_SHOW_COMPETITOR_SIGNS) def = (1 << DO_SHOW_TOWN_NAMES | 1 << DO_SHOW_STATION_NAMES | 1 << DO_SHOW_SIGNS | 1 << DO_FULL_ANIMATION | 1 << DO_FULL_DETAIL | 1 << DO_SHOW_WAYPOINT_NAMES | 1 << DO_SHOW_COMPETITOR_SIGNS)
full = ""SHOW_TOWN_NAMES|SHOW_STATION_NAMES|SHOW_SIGNS|FULL_ANIMATION||FULL_DETAIL|WAYPOINTS|SHOW_COMPETITOR_SIGNS"" full = _display_opt_modes
[SDTG_BOOL] [SDTG_BOOL]
name = ""fullscreen"" name = ""fullscreen""

View File

@ -71,10 +71,10 @@ static size_t ConvertLandscape(const char *value);
NSD(String, SLEG_GENERAL(SL_STDSTR, var, type | flags, sizeof(var), from, to, extra), name, guiflags, SDT_STDSTRING, startup, def, max_length, proc) NSD(String, SLEG_GENERAL(SL_STDSTR, var, type | flags, sizeof(var), from, to, extra), name, guiflags, SDT_STDSTRING, startup, def, max_length, proc)
#define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\ #define SDTG_OMANY(name, type, flags, guiflags, var, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\
NSD(Int, SLEG_GENERAL(SL_VAR, var, type | flags, 1, from, to, extra), name, guiflags, SDT_ONEOFMANY, startup, def, 0, max, 0, str, strhelp, strval, cat, proc, full) NSD(OneOfMany, SLEG_GENERAL(SL_VAR, var, type | flags, 1, from, to, extra), name, guiflags, SDT_ONEOFMANY, startup, def, max, str, strhelp, strval, cat, proc, full, nullptr)
#define SDTG_MMANY(name, type, flags, guiflags, var, def, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\ #define SDTG_MMANY(name, type, flags, guiflags, var, def, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\
NSD(Int, SLEG_GENERAL(SL_VAR, var, type | flags, 1, from, to, extra), name, guiflags, SDT_MANYOFMANY, startup, def, 0, 0, 0, str, strhelp, strval, cat, proc, full) NSD(ManyOfMany, SLEG_GENERAL(SL_VAR, var, type | flags, 1, from, to, extra), name, guiflags, SDT_MANYOFMANY, startup, def, str, strhelp, strval, cat, proc, full, nullptr)
#define SDTG_NULL(length, from, to)\ #define SDTG_NULL(length, from, to)\
NSD(Null, SLEG_NULL(length, from, to)) NSD(Null, SLEG_NULL(length, from, to))
@ -94,10 +94,10 @@ static size_t ConvertLandscape(const char *value);
NSD(String, SLE_GENERAL(SL_STDSTR, base, var, type | flags, sizeof(((base*)8)->var), from, to, extra), #var, guiflags, SDT_STDSTRING, startup, def, 0, proc) NSD(String, SLE_GENERAL(SL_STDSTR, base, var, type | flags, sizeof(((base*)8)->var), from, to, extra), #var, guiflags, SDT_STDSTRING, startup, def, 0, proc)
#define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat, extra, startup)\ #define SDT_OMANY(base, var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, load, cat, extra, startup)\
NSD(Int, SLE_GENERAL(SL_VAR, base, var, type | flags, 1, from, to, extra), #var, guiflags, SDT_ONEOFMANY, startup, def, 0, max, 0, str, strhelp, strval, cat, proc, full, load) NSD(OneOfMany, SLE_GENERAL(SL_VAR, base, var, type | flags, 1, from, to, extra), #var, guiflags, SDT_ONEOFMANY, startup, def, max, str, strhelp, strval, cat, proc, full, load)
#define SDT_MMANY(base, var, type, flags, guiflags, def, full, str, proc, strhelp, strval, from, to, cat, extra, startup)\ #define SDT_MMANY(base, var, type, flags, guiflags, def, full, str, proc, strhelp, strval, from, to, cat, extra, startup)\
NSD(Int, SLE_GENERAL(SL_VAR, base, var, type | flags, 1, from, to, extra), #var, guiflags, SDT_MANYOFMANY, startup, def, 0, 0, 0, str, strhelp, strval, cat, proc, full, nullptr) NSD(ManyOfMany, SLE_GENERAL(SL_VAR, base, var, type | flags, 1, from, to, extra), #var, guiflags, SDT_MANYOFMANY, startup, def, str, strhelp, strval, cat, proc, full, nullptr)
#define SDT_NULL(length, from, to)\ #define SDT_NULL(length, from, to)\
NSD(Null, SLE_CONDNULL(length, from, to)) NSD(Null, SLE_CONDNULL(length, from, to))