OpenTTD Source 15.0-beta2
strings_internal.h
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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef STRINGS_INTERNAL_H
11#define STRINGS_INTERNAL_H
12
13#include "strings_func.h"
14#include "string_func.h"
16
18protected:
20 std::span<StringParameter> parameters = {};
21
22 size_t offset = 0;
23 char32_t next_type = 0;
24
26
27public:
36
37 StringParameters(std::span<StringParameter> parameters = {}) : parameters(parameters) {}
38
39 void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
40
46 size_t GetOffset() { return this->offset; }
47
53 void SetOffset(size_t offset)
54 {
55 /*
56 * The offset must be fewer than the number of parameters when it is
57 * being set. Unless restoring a backup, then the original value is
58 * correct as well as long as the offset was not changed. In other
59 * words, when the offset was already at the end of the parameters and
60 * the string did not consume any parameters.
61 */
62 assert(offset < this->parameters.size() || this->offset == offset);
63 this->offset = offset;
64 }
65
71 void AdvanceOffset(size_t advance)
72 {
73 this->offset += advance;
74 assert(this->offset <= this->parameters.size());
75 }
76
84 {
85 struct visitor {
86 uint64_t operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as integer"); }
87 uint64_t operator()(const uint64_t &arg) { return arg; }
88 uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
89 };
90
91 const auto &param = this->GetNextParameterReference();
92 return std::visit(visitor{}, param.data);
93 }
94
102 template <typename T>
104 {
105 return static_cast<T>(this->GetNextParameter());
106 }
107
115 {
116 struct visitor {
117 const char *operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as string"); }
118 const char *operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
119 const char *operator()(const std::string &arg) { return arg.c_str(); }
120 };
121
122 const auto &param = this->GetNextParameterReference();
123 return std::visit(visitor{}, param.data);
124 }
125
135
147 {
148 return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
149 }
150
152 size_t GetDataLeft() const
153 {
154 return this->parameters.size() - this->offset;
155 }
156
158 size_t GetNumParameters() const
159 {
160 return this->parameters.size();
161 }
162
164 char32_t GetTypeAtOffset(size_t offset) const
165 {
166 assert(offset < this->parameters.size());
167 return this->parameters[offset].type;
168 }
169
170 void SetParam(size_t n, const StringParameterData &v)
171 {
172 assert(n < this->parameters.size());
173 this->parameters[n].data = v;
174 }
175
176 void SetParam(size_t n, uint64_t v)
177 {
178 assert(n < this->parameters.size());
179 this->parameters[n].data = v;
180 }
181
182 void SetParam(size_t n, ConvertibleThroughBase auto v)
183 {
184 SetParam(n, v.base());
185 }
186
187 void SetParam(size_t n, const char *str)
188 {
189 assert(n < this->parameters.size());
190 this->parameters[n].data = str;
191 }
192
193 void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
194
195 void SetParam(size_t n, std::string &&str)
196 {
197 assert(n < this->parameters.size());
198 this->parameters[n].data = std::move(str);
199 }
200
201 const StringParameterData &GetParam(size_t n) const
202 {
203 assert(n < this->parameters.size());
204 return this->parameters[n].data;
205 }
206};
207
208void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
209void GetStringWithArgs(StringBuilder &builder, StringID string, std::span<StringParameter> params, uint case_index = 0, bool game_script = false);
210std::string GetStringWithArgs(StringID string, StringParameters &args);
211
212/* Do not leak the StringBuilder to everywhere. */
213void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
214void GetTownName(StringBuilder &builder, const struct Town *t);
215void GRFTownNameGenerate(StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed);
216
217char32_t RemapNewGRFStringControlCode(char32_t scc, const char **str);
218
219#endif /* STRINGS_INTERNAL_H */
Compose data into a growing std::string.
StringParameters * parent
If not nullptr, this instance references data from this parent instance.
void SetOffset(size_t offset)
Set the offset within the string from where to return the next result of GetInt64 or GetInt32.
StringParameters GetRemainingParameters()
Get a new instance of StringParameters that is a "range" into the remaining existing parameters.
std::span< StringParameter > parameters
Array with the actual parameters.
const StringParameter & GetNextParameterReference()
Get the next parameter from our parameters.
Definition strings.cpp:69
void AdvanceOffset(size_t advance)
Advance the offset within the string from where to return the next result of GetInt64 or GetInt32.
uint64_t GetNextParameter()
Get the next parameter from our parameters.
size_t offset
Current offset in the parameters span.
StringParameters(StringParameters &parent, size_t size)
Create a new StringParameters instance that can reference part of the data of the given parent instan...
T GetNextParameter()
Get the next parameter from our parameters.
char32_t next_type
The type of the next data that is retrieved.
size_t GetOffset()
Get the current offset, so it can be backed up for certain processing steps, or be used to offset the...
const char * GetNextParameterString()
Get the next string parameter from our parameters.
size_t GetNumParameters() const
Return the number of parameters.
size_t GetDataLeft() const
Return the amount of elements which can still be read.
char32_t GetTypeAtOffset(size_t offset) const
Get the type of a specific element.
StringParameters GetRemainingParameters(size_t offset)
Get a new instance of StringParameters that is a "range" into the remaining existing parameters from ...
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
char32_t RemapNewGRFStringControlCode(char32_t scc, const char **str)
Emit OpenTTD's internal string code for the different NewGRF string codes.
Compose strings from textual and binary data.
Functions related to low-level strings.
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:336
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
The data required to format and validate a single parameter of a string.
Town data structure.
Definition town.h:52