diff --git a/src/core/convertible_through_base.hpp b/src/core/convertible_through_base.hpp index 6dc4a901be..cc27702547 100644 --- a/src/core/convertible_through_base.hpp +++ b/src/core/convertible_through_base.hpp @@ -29,4 +29,18 @@ concept ConvertibleThroughBase = requires(T const a) { template concept ConvertibleThroughBaseOrTo = std::is_convertible_v || ConvertibleThroughBase; +/** + * A sort-of mixin that adds 'at(pos)' and 'operator[](pos)' implementations for 'ConvertibleThroughBase' types. + * This to prevent having to call '.base()' for many container accesses. + */ +template +class ReferenceThroughBaseContainer : public Container { +public: + Container::reference at(ConvertibleThroughBase auto pos) { return this->Container::at(pos.base()); } + Container::const_reference at(ConvertibleThroughBase auto pos) const { return this->Container::at(pos.base()); } + + Container::reference operator[](ConvertibleThroughBase auto pos) { return this->Container::operator[](pos.base()); } + Container::const_reference operator[](ConvertibleThroughBase auto pos) const { return this->Container::operator[](pos.base()); } +}; + #endif /* CONVERTIBLE_THROUGH_BASE_HPP */ diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index f086f72407..8278da2663 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -318,7 +318,7 @@ GameStrings *_current_data = nullptr; const char *GetGameStringPtr(StringIndexInTab id) { if (_current_data == nullptr || _current_data->cur_language == nullptr || id.base() >= _current_data->cur_language->lines.size()) return GetStringPtr(STR_UNDEFINED); - return _current_data->cur_language->lines[id.base()].c_str(); + return _current_data->cur_language->lines[id].c_str(); } /** @@ -332,7 +332,7 @@ const StringParams &GetGameStringParams(StringIndexInTab id) static StringParams empty; if (id.base() >= _current_data->string_params.size()) return empty; - return _current_data->string_params[id.base()]; + return _current_data->string_params[id]; } /** @@ -346,7 +346,7 @@ const std::string &GetGameStringName(StringIndexInTab id) static const std::string undefined = "STR_UNDEFINED"; if (id.base() >= _current_data->string_names.size()) return undefined; - return _current_data->string_names[id.base()]; + return _current_data->string_names[id]; } /** diff --git a/src/game/game_text.hpp b/src/game/game_text.hpp index 1f9c0c5c34..e12851a928 100644 --- a/src/game/game_text.hpp +++ b/src/game/game_text.hpp @@ -38,7 +38,7 @@ void ReconsiderGameScriptLanguage(); /** Container for the raw (unencoded) language strings of a language. */ struct LanguageStrings { std::string language; ///< Name of the language (base filename). Empty string if invalid. - StringList lines; ///< The lines of the file to pass into the parser/encoder. + ReferenceThroughBaseContainer lines; ///< The lines of the file to pass into the parser/encoder. LanguageStrings() {} LanguageStrings(const std::string &lang) : language(lang) {} @@ -55,8 +55,8 @@ struct GameStrings { std::vector raw_strings; ///< The raw strings per language, first must be English/the master language!. std::vector compiled_strings; ///< The compiled strings per language, first must be English/the master language!. - StringList string_names; ///< The names of the compiled strings. - StringParamsList string_params; ///< The parameters for the strings. + ReferenceThroughBaseContainer string_names; ///< The names of the compiled strings. + ReferenceThroughBaseContainer string_params; ///< The parameters for the strings. void Compile(); diff --git a/src/newgrf_text.cpp b/src/newgrf_text.cpp index ceed99b30a..1d09cdea21 100644 --- a/src/newgrf_text.cpp +++ b/src/newgrf_text.cpp @@ -71,7 +71,7 @@ struct GRFTextEntry { }; -static std::vector _grf_text; +static ReferenceThroughBaseContainer> _grf_text; static uint8_t _currentLangID = GRFLX_ENGLISH; ///< by default, english is used. /** @@ -639,13 +639,13 @@ const char *GetGRFStringFromGRFText(const GRFTextWrapper &text) const char *GetGRFStringPtr(StringIndexInTab stringid) { assert(stringid.base() < _grf_text.size()); - assert(_grf_text[stringid.base()].grfid != 0); + assert(_grf_text[stringid].grfid != 0); - const char *str = GetGRFStringFromGRFText(_grf_text[stringid.base()].textholder); + const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder); if (str != nullptr) return str; /* Use the default string ID if the fallback string isn't available */ - return GetStringPtr(_grf_text[stringid.base()].def_string); + return GetStringPtr(_grf_text[stringid].def_string); } /**