OpenTTD Source 20260311-master-g511d3794ce
strings_internal.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
9
10#ifndef STRINGS_INTERNAL_H
11#define STRINGS_INTERNAL_H
12
13#include "strings_func.h"
14#include "string_func.h"
17
19protected:
21 std::span<StringParameter> parameters = {};
22
23 size_t offset = 0;
24 char32_t next_type = 0;
25
27
28public:
39
44 StringParameters(std::span<StringParameter> parameters = {}) : parameters(parameters) {}
45
46 void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
47
53 size_t GetOffset() { return this->offset; }
54
60 void SetOffset(size_t offset)
61 {
62 /*
63 * The offset must be fewer than the number of parameters when it is
64 * being set. Unless restoring a backup, then the original value is
65 * correct as well as long as the offset was not changed. In other
66 * words, when the offset was already at the end of the parameters and
67 * the string did not consume any parameters.
68 */
69 assert(offset < this->parameters.size() || this->offset == offset);
70 this->offset = offset;
71 }
72
78 void AdvanceOffset(size_t advance)
79 {
80 this->offset += advance;
81 assert(this->offset <= this->parameters.size());
82 }
83
91 {
92 struct visitor {
93 uint64_t operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as integer"); }
94 uint64_t operator()(const uint64_t &arg) { return arg; }
95 uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
96 };
97
98 const auto &param = this->GetNextParameterReference();
99 return std::visit(visitor{}, param.data);
100 }
101
109 template <typename T>
111 {
112 return static_cast<T>(this->GetNextParameter());
113 }
114
121 std::string_view GetNextParameterString()
122 {
123 struct visitor {
124 std::string_view operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as string"); }
125 std::string_view operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
126 std::string_view operator()(const std::string &arg) { return arg; }
127 };
128
129 const auto &param = this->GetNextParameterReference();
130 return std::visit(visitor{}, param.data);
131 }
132
142
154 {
155 return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
156 }
157
162 size_t GetDataLeft() const
163 {
164 return this->parameters.size() - this->offset;
165 }
166
171 size_t GetNumParameters() const
172 {
173 return this->parameters.size();
174 }
175
181 char32_t GetTypeAtOffset(size_t offset) const
182 {
183 assert(offset < this->parameters.size());
184 return this->parameters[offset].type;
185 }
186
187 void SetParam(size_t n, const StringParameterData &v)
188 {
189 assert(n < this->parameters.size());
190 this->parameters[n].data = v;
191 }
192
193 void SetParam(size_t n, uint64_t v)
194 {
195 assert(n < this->parameters.size());
196 this->parameters[n].data = v;
197 }
198
199 void SetParam(size_t n, ConvertibleThroughBase auto v)
200 {
201 SetParam(n, v.base());
202 }
203
204 void SetParam(size_t n, const std::string &str)
205 {
206 assert(n < this->parameters.size());
207 this->parameters[n].data = str;
208 }
209
210 void SetParam(size_t n, std::string &&str)
211 {
212 assert(n < this->parameters.size());
213 this->parameters[n].data = std::move(str);
214 }
215
216 const StringParameterData &GetParam(size_t n) const
217 {
218 assert(n < this->parameters.size());
219 return this->parameters[n].data;
220 }
221};
222
223void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
224void GetStringWithArgs(StringBuilder &builder, StringID string, std::span<StringParameter> params, uint case_index = 0, bool game_script = false);
225std::string GetStringWithArgs(StringID string, StringParameters &args);
226
227/* Do not leak the StringBuilder to everywhere. */
228void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
229void GetTownName(StringBuilder &builder, const struct Town *t);
230void GRFTownNameGenerate(StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed);
231
232char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer);
233
234#endif /* STRINGS_INTERNAL_H */
Compose data into a growing std::string.
Parse data from a string / buffer.
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.
std::string_view GetNextParameterString()
Get the next string parameter from our parameters.
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:68
void AdvanceOffset(size_t advance)
Advance the offset within the string from where to return the next result of GetInt64 or GetInt32.
StringParameters(std::span< StringParameter > parameters={})
Create a new StringParameters instance with the given parameters.
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...
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 ...
#define T
Climate temperate.
Definition engines.h:91
Compose strings from textual and binary data.
Parse strings.
Functions related to low-level strings.
Functions related to OTTD's strings.
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index=0, bool game_script=false)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:336
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.
Definition townname.cpp:975
char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer)
Emit OpenTTD's internal string code for the different NewGRF string codes.
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:63