OpenTTD Source 20261112-master-gbcbec5e63f
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_type.h"
16
18protected:
20 std::span<StringParameter> parameters = {};
21
22 size_t offset = 0;
23 char32_t next_type = 0;
24
26
27public:
38
43 StringParameters(std::span<StringParameter> parameters = {}) : parameters(parameters) {}
44
45 void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
46
52 size_t GetOffset() { return this->offset; }
53
59 void SetOffset(size_t offset)
60 {
61 /*
62 * The offset must be fewer than the number of parameters when it is
63 * being set. Unless restoring a backup, then the original value is
64 * correct as well as long as the offset was not changed. In other
65 * words, when the offset was already at the end of the parameters and
66 * the string did not consume any parameters.
67 */
68 assert(offset < this->parameters.size() || this->offset == offset);
69 this->offset = offset;
70 }
71
77 void AdvanceOffset(size_t advance)
78 {
79 this->offset += advance;
80 assert(this->offset <= this->parameters.size());
81 }
82
90 {
91 struct visitor {
92 uint64_t operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as integer"); }
93 uint64_t operator()(const uint64_t &arg) { return arg; }
94 uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
95 };
96
97 const auto &param = this->GetNextParameterReference();
98 return std::visit(visitor{}, param.data);
99 }
100
108 template <typename T>
110 {
111 return static_cast<T>(this->GetNextParameter());
112 }
113
120 std::string_view GetNextParameterString()
121 {
122 struct visitor {
123 std::string_view operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as string"); }
124 std::string_view operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
125 std::string_view operator()(const std::string &arg) { return arg; }
126 };
127
128 const auto &param = this->GetNextParameterReference();
129 return std::visit(visitor{}, param.data);
130 }
131
141
153 {
154 return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
155 }
156
161 size_t GetDataLeft() const
162 {
163 return this->parameters.size() - this->offset;
164 }
165
170 size_t GetNumParameters() const
171 {
172 return this->parameters.size();
173 }
174
180 char32_t GetTypeAtOffset(size_t offset) const
181 {
182 assert(offset < this->parameters.size());
183 return this->parameters[offset].type;
184 }
185
186 void SetParam(size_t n, const StringParameterData &v)
187 {
188 assert(n < this->parameters.size());
189 this->parameters[n].data = v;
190 }
191
192 void SetParam(size_t n, uint64_t v)
193 {
194 assert(n < this->parameters.size());
195 this->parameters[n].data = v;
196 }
197
198 void SetParam(size_t n, ConvertibleThroughBase auto v)
199 {
200 SetParam(n, v.base());
201 }
202
203 void SetParam(size_t n, const std::string &str)
204 {
205 assert(n < this->parameters.size());
206 this->parameters[n].data = str;
207 }
208
209 void SetParam(size_t n, std::string &&str)
210 {
211 assert(n < this->parameters.size());
212 this->parameters[n].data = std::move(str);
213 }
214
215 const StringParameterData &GetParam(size_t n) const
216 {
217 assert(n < this->parameters.size());
218 return this->parameters[n].data;
219 }
220};
221
222void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
223void GetStringWithArgs(StringBuilder &builder, StringID string, std::span<StringParameter> params, uint case_index = 0, bool game_script = false);
224std::string GetStringWithArgs(StringID string, StringParameters &args);
225
226/* Do not leak the StringBuilder to everywhere. */
227void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
228void GetTownName(StringBuilder &builder, const struct Town *t);
229void GRFTownNameGenerate(StringBuilder &builder, GrfID grfid, uint16_t gen, uint32_t seed);
230
231char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer);
232
233#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:67
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
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
Compose strings from textual and binary data.
Parse 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:335
void GRFTownNameGenerate(StringBuilder &builder, GrfID grfid, uint16_t gen, uint32_t seed)
Construct a NewGRF town name into the builder.
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.
Definition townname.cpp:973
char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer)
Emit OpenTTD's internal string code for the different NewGRF string codes.
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > 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:64