OpenTTD Source: src/strings_type.h Source File

OpenTTD Source 15.3

strings_type.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef STRINGS_TYPE_H
11#define STRINGS_TYPE_H
12
13#include "core/convertible_through_base.hpp"
14#include "core/strong_typedef_type.hpp"
15
19 typedef uint32_t StringID;
20 static const StringID INVALID_STRING_ID = 0xFFFF;
21 static const int MAX_CHAR_LENGTH = 4;
22 static const uint MAX_LANG = 0x7F;
23
25 enum TextDirection : uint8_t {
26 TD_LTR,
27 TD_RTL,
28};
29
31 enum StringTab : uint8_t {
32 /* Tabs 0..1 for regular strings */
33 TEXT_TAB_TOWN = 4,
34 TEXT_TAB_INDUSTRY = 9,
35 TEXT_TAB_STATION = 12,
36 TEXT_TAB_SPECIAL = 14,
37 TEXT_TAB_OLD_CUSTOM = 15,
38 TEXT_TAB_VEHICLE = 16,
39 /* Tab 17 for regular strings */
40 TEXT_TAB_OLD_NEWGRF = 26,
41 TEXT_TAB_END = 32,
42 TEXT_TAB_GAMESCRIPT_START = 32,
43 TEXT_TAB_NEWGRF_START = 64,
44};
45
47 using StringIndexInTab = StrongType::Typedef<uint32_t, struct StringIndexInTabTag, StrongType::Compare, StrongType::Integer>;
48
50 static const uint TAB_SIZE_BITS = 11;
52 static const uint TAB_SIZE = 1 << TAB_SIZE_BITS;
53
55 static const uint TAB_SIZE_GAMESCRIPT = TAB_SIZE * 32;
56
58 static const uint TAB_SIZE_NEWGRF = TAB_SIZE * 256;
59
61 static constexpr uint32_t BUILTIN_TOWNNAME_GENERATOR_COUNT = 21;
62
64 static constexpr StringID SPECSTR_TOWNNAME_START = 0x20C0;
65static constexpr StringID SPECSTR_TOWNNAME_END = SPECSTR_TOWNNAME_START + BUILTIN_TOWNNAME_GENERATOR_COUNT;
66
68 static constexpr StringID SPECSTR_COMPANY_NAME_START = 0x70EA;
69static constexpr StringID SPECSTR_COMPANY_NAME_END = SPECSTR_COMPANY_NAME_START + BUILTIN_TOWNNAME_GENERATOR_COUNT;
70
71 static constexpr StringID SPECSTR_SILLY_NAME = 0x70E5;
72 static constexpr StringID SPECSTR_ANDCO_NAME = 0x70E6;
73 static constexpr StringID SPECSTR_PRESIDENT_NAME = 0x70E7;
74
75using StringParameterData = std::variant<std::monostate, uint64_t, std::string>;
76
78 struct StringParameter {
79 StringParameterData data;
80 char32_t type;
81
82 StringParameter() = default;
83 inline StringParameter(StringParameterData &&data) : data(std::move(data)), type(0) {}
84
85 inline StringParameter(const std::monostate &data) : data(data), type(0) {}
86 inline StringParameter(uint64_t data) : data(data), type(0) {}
87
88 inline StringParameter(std::string_view data) : data(std::string{data}), type(0) {}
89 inline StringParameter(std::string &&data) : data(std::move(data)), type(0) {}
90 inline StringParameter(const std::string &data) : data(data), type(0) {}
91
92 inline StringParameter(const ConvertibleThroughBase auto &data) : data(static_cast<uint64_t>(data.base())), type(0) {}
93};
94
98 class EncodedString {
99public:
100 EncodedString() = default;
101
102 auto operator<=>(const EncodedString &) const = default;
103
104 std::string GetDecodedString() const;
105 EncodedString ReplaceParam(size_t param, StringParameter &&value) const;
106
107 inline void clear() { this->string.clear(); }
108 inline bool empty() const { return this->string.empty(); }
109
110private:
111 std::string string;
112
113 /* An EncodedString can only be created by GetEncodedStringWithArgs(). */
114 explicit EncodedString(std::string &&string) : string(std::move(string)) {}
115
116 friend EncodedString GetEncodedStringWithArgs(StringID str, std::span<const StringParameter> params);
117
118 template <typename Tcont, typename Titer>
119 friend class EndianBufferWriter;
120 friend class EndianBufferReader;
121 friend class ScriptText;
122};
123
124#endif /* STRINGS_TYPE_H */
EncodedString
Container for an encoded string, created by GetEncodedString.
Definition strings_type.h:98
EncodedString::GetEncodedStringWithArgs
friend EncodedString GetEncodedStringWithArgs(StringID str, std::span< const StringParameter > params)
Encode a string with its parameters into an encoded string.
Definition strings.cpp:102
EncodedString::string
std::string string
The encoded string.
Definition strings_type.h:111
EncodedString::GetDecodedString
std::string GetDecodedString() const
Decode the encoded string.
Definition strings.cpp:207
EncodedString::ReplaceParam
EncodedString ReplaceParam(size_t param, StringParameter &&value) const
Replace a parameter of this EncodedString.
Definition strings.cpp:150
EndianBufferReader
Endian-aware buffer adapter that always reads values in little endian order.
Definition endian_buffer.hpp:145
EndianBufferWriter
Endian-aware buffer adapter that always writes values in little endian order.
Definition endian_buffer.hpp:24
ConvertibleThroughBase
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Definition convertible_through_base.hpp:19
convertible_through_base.hpp
Concept for unifying the convert through 'base()' behaviour of several 'strong' types.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition strings_type.h:20
SPECSTR_COMPANY_NAME_START
static constexpr StringID SPECSTR_COMPANY_NAME_START
Special strings for company names on the form "TownName transport".
Definition strings_type.h:68
SPECSTR_SILLY_NAME
static constexpr StringID SPECSTR_SILLY_NAME
Special string for silly company names.
Definition strings_type.h:71
TAB_SIZE_NEWGRF
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition strings_type.h:58
TAB_SIZE_GAMESCRIPT
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
Definition strings_type.h:55
MAX_LANG
static const uint MAX_LANG
Maximum number of languages supported by the game, and the NewGRF specs.
Definition strings_type.h:22
TAB_SIZE_BITS
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
Definition strings_type.h:50
BUILTIN_TOWNNAME_GENERATOR_COUNT
static constexpr uint32_t BUILTIN_TOWNNAME_GENERATOR_COUNT
The number of builtin generators for town names.
Definition strings_type.h:61
SPECSTR_ANDCO_NAME
static constexpr StringID SPECSTR_ANDCO_NAME
Special string for Surname & Co company names.
Definition strings_type.h:72
SPECSTR_PRESIDENT_NAME
static constexpr StringID SPECSTR_PRESIDENT_NAME
Special string for the president's name.
Definition strings_type.h:73
SPECSTR_TOWNNAME_START
static constexpr StringID SPECSTR_TOWNNAME_START
Special strings for town names.
Definition strings_type.h:64
TAB_SIZE
static const uint TAB_SIZE
Number of strings per StringTab.
Definition strings_type.h:52
StringTab
StringTab
StringTabs to group StringIDs.
Definition strings_type.h:31
TEXT_TAB_NEWGRF_START
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
Definition strings_type.h:43
TEXT_TAB_GAMESCRIPT_START
@ TEXT_TAB_GAMESCRIPT_START
Start of GameScript supplied strings.
Definition strings_type.h:42
TEXT_TAB_END
@ TEXT_TAB_END
End of language files.
Definition strings_type.h:41
TextDirection
TextDirection
Directions a text can go to.
Definition strings_type.h:25
TD_LTR
@ TD_LTR
Text is written left-to-right by default.
Definition strings_type.h:26
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:27
MAX_CHAR_LENGTH
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition strings_type.h:21
strong_typedef_type.hpp
Type (helpers) for making a strong typedef that is a distinct type.
StringParameter
The data required to format and validate a single parameter of a string.
Definition strings_type.h:78
StringParameter::data
StringParameterData data
The data of the parameter.
Definition strings_type.h:79
StringParameter::type
char32_t type
The StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
Definition strings_type.h:80
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130