mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: merge multiple string parameter arrays to single array of structs
This commit is contained in:
parent
fa8c50758b
commit
4e9a871718
|
@ -66,8 +66,7 @@ AllocatedStringParameters _global_string_params(20);
|
||||||
*/
|
*/
|
||||||
void StringParameters::PrepareForNextRun()
|
void StringParameters::PrepareForNextRun()
|
||||||
{
|
{
|
||||||
assert(this->type != nullptr);
|
for (auto ¶m : this->parameters) param.type = 0;
|
||||||
MemSetT(this->type, 0, this->num_param);
|
|
||||||
this->offset = 0;
|
this->offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,20 +78,20 @@ void StringParameters::PrepareForNextRun()
|
||||||
int64 StringParameters::GetInt64()
|
int64 StringParameters::GetInt64()
|
||||||
{
|
{
|
||||||
assert(this->next_type == 0 || (SCC_CONTROL_START <= this->next_type && this->next_type <= SCC_CONTROL_END));
|
assert(this->next_type == 0 || (SCC_CONTROL_START <= this->next_type && this->next_type <= SCC_CONTROL_END));
|
||||||
if (this->offset >= this->num_param) {
|
if (this->offset >= this->parameters.size()) {
|
||||||
Debug(misc, 0, "Trying to read invalid string parameter");
|
Debug(misc, 0, "Trying to read invalid string parameter");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (this->type != nullptr) {
|
|
||||||
if (this->type[this->offset] != 0 && this->type[this->offset] != this->next_type) {
|
auto ¶m = this->parameters[this->offset++];
|
||||||
Debug(misc, 0, "Trying to read string parameter with wrong type");
|
if (param.type != 0 && param.type != this->next_type) {
|
||||||
this->next_type = 0;
|
Debug(misc, 0, "Trying to read string parameter with wrong type");
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
this->type[this->offset] = next_type;
|
|
||||||
this->next_type = 0;
|
this->next_type = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return this->data[this->offset++];
|
param.type = next_type;
|
||||||
|
this->next_type = 0;
|
||||||
|
return param.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,47 +12,40 @@
|
||||||
|
|
||||||
#include "strings_func.h"
|
#include "strings_func.h"
|
||||||
#include "string_func.h"
|
#include "string_func.h"
|
||||||
|
#include "core/span_type.hpp"
|
||||||
|
|
||||||
|
/** The data required to format and validate a single parameter of a string. */
|
||||||
|
struct StringParameter {
|
||||||
|
uint64_t data; ///< The data of the parameter.
|
||||||
|
WChar type; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
|
||||||
|
};
|
||||||
|
|
||||||
class StringParameters {
|
class StringParameters {
|
||||||
protected:
|
protected:
|
||||||
StringParameters *parent = nullptr; ///< If not nullptr, this instance references data from this parent instance.
|
StringParameters *parent = nullptr; ///< If not nullptr, this instance references data from this parent instance.
|
||||||
uint64 *data; ///< Array with the actual data.
|
span<StringParameter> parameters = {}; ///< Array with the actual parameters.
|
||||||
WChar *type; ///< Array with type information about the data. See #StringControlCode.
|
|
||||||
|
|
||||||
|
size_t offset = 0; ///< Current offset in the parameters span.
|
||||||
WChar next_type = 0; ///< The type of the next data that is retrieved.
|
WChar next_type = 0; ///< The type of the next data that is retrieved.
|
||||||
size_t offset = 0; ///< Current offset in the data/type arrays.
|
|
||||||
|
|
||||||
/** Create a new StringParameters instance. */
|
StringParameters(span<StringParameter> parameters = {}) :
|
||||||
StringParameters(uint64 *data, size_t num_param, WChar *type) :
|
parameters(parameters)
|
||||||
data(data),
|
{}
|
||||||
type(type),
|
|
||||||
num_param(num_param)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
size_t num_param; ///< Length of the data array.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new StringParameters instance that can reference part of the data of
|
* Create a new StringParameters instance that can reference part of the data of
|
||||||
* the given partent instance.
|
* the given partent instance.
|
||||||
*/
|
*/
|
||||||
StringParameters(StringParameters &parent, size_t size) :
|
StringParameters(StringParameters &parent, size_t size) :
|
||||||
parent(&parent),
|
parent(&parent),
|
||||||
data(parent.data + parent.offset),
|
parameters(parent.parameters.subspan(parent.offset, size))
|
||||||
num_param(size)
|
{}
|
||||||
{
|
|
||||||
assert(size <= parent.GetDataLeft());
|
|
||||||
if (parent.type == nullptr) {
|
|
||||||
this->type = nullptr;
|
|
||||||
} else {
|
|
||||||
this->type = parent.type + parent.offset;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~StringParameters()
|
~StringParameters()
|
||||||
{
|
{
|
||||||
if (this->parent != nullptr) {
|
if (this->parent != nullptr) {
|
||||||
this->parent->offset += this->num_param;
|
this->parent->offset += this->parameters.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +73,7 @@ public:
|
||||||
* words, when the offset was already at the end of the parameters and
|
* words, when the offset was already at the end of the parameters and
|
||||||
* the string did not consume any parameters.
|
* the string did not consume any parameters.
|
||||||
*/
|
*/
|
||||||
assert(offset < this->num_param || this->offset == offset);
|
assert(offset < this->parameters.size() || this->offset == offset);
|
||||||
this->offset = offset;
|
this->offset = offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,26 +107,26 @@ public:
|
||||||
*/
|
*/
|
||||||
StringParameters GetRemainingParameters(size_t offset)
|
StringParameters GetRemainingParameters(size_t offset)
|
||||||
{
|
{
|
||||||
return StringParameters(&this->data[offset], GetDataLeft(), &this->type[offset]);
|
return StringParameters(this->parameters.subspan(this->offset, GetDataLeft()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return the amount of elements which can still be read. */
|
/** Return the amount of elements which can still be read. */
|
||||||
size_t GetDataLeft() const
|
size_t GetDataLeft() const
|
||||||
{
|
{
|
||||||
return this->num_param - this->offset;
|
return this->parameters.size() - this->offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the type of a specific element. */
|
/** Get the type of a specific element. */
|
||||||
WChar GetTypeAtOffset(size_t offset) const
|
WChar GetTypeAtOffset(size_t offset) const
|
||||||
{
|
{
|
||||||
assert(offset < this->num_param);
|
assert(offset < this->parameters.size());
|
||||||
return this->type[offset];
|
return this->parameters[offset].type;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetParam(size_t n, uint64 v)
|
void SetParam(size_t n, uint64 v)
|
||||||
{
|
{
|
||||||
assert(n < this->num_param);
|
assert(n < this->parameters.size());
|
||||||
this->data[n] = v;
|
this->parameters[n].data = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetParam(size_t n, const char *str) { this->SetParam(n, (uint64_t)(size_t)str); }
|
void SetParam(size_t n, const char *str) { this->SetParam(n, (uint64_t)(size_t)str); }
|
||||||
|
@ -142,8 +135,8 @@ public:
|
||||||
|
|
||||||
uint64 GetParam(size_t n) const
|
uint64 GetParam(size_t n) const
|
||||||
{
|
{
|
||||||
assert(n < this->num_param);
|
assert(n < this->parameters.size());
|
||||||
return this->data[n];
|
return this->parameters[n].data;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -152,14 +145,12 @@ public:
|
||||||
* the parameters.
|
* the parameters.
|
||||||
*/
|
*/
|
||||||
class AllocatedStringParameters : public StringParameters {
|
class AllocatedStringParameters : public StringParameters {
|
||||||
std::vector<uint64_t> params; ///< The actual parameters
|
std::vector<StringParameter> params; ///< The actual parameters
|
||||||
std::vector<WChar> types; ///< The actual types.
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AllocatedStringParameters(size_t parameters = 0) : StringParameters(nullptr, parameters, nullptr), params(parameters), types(parameters)
|
AllocatedStringParameters(size_t parameters = 0) : params(parameters)
|
||||||
{
|
{
|
||||||
this->data = params.data();
|
this->parameters = span(params.data(), params.size());
|
||||||
this->type = types.data();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue