OpenTTD Source: src/strings_internal.h Source File

OpenTTD Source 15.0-RC2

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