Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: make creating temporary StringParameters easier #11007

Merged
merged 1 commit into from Jun 20, 2023

Conversation

rubidium42
Copy link
Contributor

@rubidium42 rubidium42 commented Jun 14, 2023

Motivation / Problem

The repetitiveness of:

int64 args_array[] = { this->index };
StringParameters tmp_params(args_array);

Description

Introduce a sub class of StringParameters that has its own buffer.
Introduce a templated method that can accept any number of arguments that creates and fills the appropriate "StringParameters" instance.
Use that templated function instead, so the above code becomes auto tmp_params = MakeParameters(this->index);.

As added bonus, stuff like:

int64 args_array[] = {(int64)(size_t)e->name.c_str()};
StringParameters tmp_params(args_array);

becomes auto tmp_params = MakeParameters(e->name);, by implementing SetParam for const std::string& and const char *.

Limitations

None I can think of, except that there are a few other places where this new class could be used. However, those are for a later PR.

String pointers/references are still cast to size_t and then uint64_t. Solving that is, again, for a later PR.

I would have liked to use std::array and rely on copy-elision. That does work with GCC, but neither Clang nor MSVC does copy-elision for MakeParameters making that route troublesome. So, I've gone with std::vector so it can just move a pointer around.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason all the AllocatedStringParameters variables can't be marked as const?

src/strings_func.h Fixed Show fixed Hide fixed
src/strings_func.h Fixed Show fixed Hide fixed
@rubidium42
Copy link
Contributor Author

Is there any particular reason all the AllocatedStringParameters variables can't be marked as const?

Most places require StringParameters &, because either they run GetInt64 which updates the offset variable within StringParameters or run SetParam which also updates the object.

@rubidium42 rubidium42 marked this pull request as draft June 14, 2023 19:29
src/strings_func.h Fixed Show fixed Hide fixed
@rubidium42 rubidium42 force-pushed the const-string-params branch 2 times, most recently from 347817c to 90c9485 Compare June 17, 2023 10:34
@rubidium42 rubidium42 marked this pull request as ready for review June 17, 2023 10:34
@rubidium42 rubidium42 merged commit 3e8f8c5 into OpenTTD:master Jun 20, 2023
19 checks passed
@rubidium42 rubidium42 deleted the const-string-params branch June 20, 2023 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants