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

Change: support passing std::string directly to DEBUG #9212

Closed
wants to merge 2 commits into from

Conversation

rubidium42
Copy link
Contributor

@rubidium42 rubidium42 commented May 8, 2021

Motivation / Problem

Many changes/conversions changing variables to std::string end up with changing calls to DEBUG to add .c_str().
This problem compounded by MSVC not complaining about this, and it even just seeming to work out of the box. So you might only find out the forgotten .c_str()s when having created the PR.

Description

Add effectively a C++-wrapper around the existing debug that performs the conversion where needed, and let DEBUG use that.

Limitations

Slightly more code in the debug header, but fewer chances on mistakes and fewer changes required when converting towards std::string. For now the conversion functions are in the debug header, though they should probably go to a different location once this functionality gets used in more places.
Furthermore WARN_FORMAT cannot look at the parameters anymore, so we'd be losing that bit of help. So the question is what's worse, not having that warning or c_str() in many places.

Future work

Other printf style functions could be converted, though there it cannot be injected as easily as the functions are called directly. In other words, the existing functions need to be renamed and then the wrapper can be added.

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 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')

@rubidium42
Copy link
Contributor Author

This approach does nog really seem to work nicely as it requires dropping format warnings from the compiler.

master...rubidium42:debug_stream is another approach that uses a std::stringstream, though that clutters up the code a lot.

Maybe the better approach would be to go from C-printf towards std::format, however the latter is C++20. There is also a fmt::format that is almost compatible with the std::format specification (https://fmt.dev/7.1.3/api.html#compatibility-with-c-20-std-format), so going with fmt::format might be a good interim solution to get the conversion going. Then, when we start requiring C++20, we can drop fmt and replace fmt:: with std::.

So the real question is, what approach should we take?

@rubidium42 rubidium42 marked this pull request as draft May 8, 2021 10:49
@rubidium42 rubidium42 closed this May 12, 2021
@rubidium42 rubidium42 deleted the debug_c_str branch May 12, 2021 14:51
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

1 participant