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: use std::optional<std::string> over char * for text query result #12638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rubidium42
Copy link
Contributor

Motivation / Problem

OnQueryTextFinished gets a char * which in many places gets passed into StrEmpty.

The char * fulfils multiple meanings; when it's nullptr it means the window was closed and there is no string, when the string is empty (str[0] == '\0') this denotes the default value (also functionally; clearing the company name and then pressing OK will give you the default name), and then anything non-empty is considered valid data.

Since we're moving towards a more C++-styled codebase, convert this char * into std::optional<std::string> instead.

Description

Convert the parameter type of OnQueryTextFinished.

Limitations

This introduces quite a few ->c_str()s because many places use atoi and friends. Replacing atoi with a more modern variant is beyond the scope of this PR.

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, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@@ -404,14 +404,14 @@ struct CheatWindow : Window {
this->SetDirty();
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
Copy link
Member

Choose a reason for hiding this comment

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

A few more const-ings? I don't think these are modified anywhere within the OnQuery* functions?

Not sure about & either, presumably the std::optional is already avoiding a copy?

Copy link
Member

Choose a reason for hiding this comment

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

std::optional is not avoiding a copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

std::optional doesn't allow references.

Then there is the choice whether we want to make everything const, or do we want the string to be std::moveable? There are three places where we could use std::move as data is written directly to variables. In all other cases it's either ->c_str() or passed as a const std::string & into a command.

Copy link
Member

Choose a reason for hiding this comment

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

Who owned the string when it was char * form? std::string_view might be appropriate, but then again is also potentially as dangerous as char * in this context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

std::string_view requires changing many command signatures as well as move std::string_view much deeper into the settings. That would rabbit hole quite quickly.

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

3 participants