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: replace magic numbers and C-style arrays with C++-style array for share owners #9829

Merged
merged 1 commit into from Mar 9, 2022

Conversation

rubidium42
Copy link
Contributor

Motivation / Problem

I was looking at some PR, saw magic numbers related to share owners and thought this could be done nicer/clearer with C++-style arrays and algorithms.

Description

Should be a 1:1 functional code change that replaces C-style arrays, magic numbers and lengthof with C++ algorithms and for loops.

Limitations

None... although with C++20 ranges it could be even simpler.

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

@2TallTyler
Copy link
Member

// We can not buy out a real company (temporarily). TODO: well, enable it obviously.

Wonder how long this temporary comment has been here. 😛

src/company_gui.cpp Outdated Show resolved Hide resolved
assert(unowned_share != c->share_owners.end()); // share owners is guaranteed to contain at least one INVALID_OWNER, i.e. unowned share
*unowned_share = _current_company;

auto current_company_owns_share = [](auto share_owner) { return share_owner == _current_company; };
Copy link

Choose a reason for hiding this comment

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

I think _current_company should be captured in this lambda.

Copy link
Member

Choose a reason for hiding this comment

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

_current_company is a global, it does not need a capture. No idea if a capture is optionally even allowed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clang is quite resolute about it not being allowed. GCC "only" warns for it.

/home/rubidium/openttd/src/economy.cpp:2043:39: error: '_current_company' cannot be captured because it does not have automatic storage duration
                auto current_company_owns_share = [&_current_company](auto share_owner) { return share_owner == _current_company; };
                                                    ^
/home/rubidium/openttd/src/company_func.h:34:18: note: '_current_company' declared here
extern CompanyID _current_company;
                 ^
1 error generated.

Copy link

Choose a reason for hiding this comment

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

ok, thank you for checking :)

@rubidium42 rubidium42 merged commit c73f578 into OpenTTD:master Mar 9, 2022
@rubidium42 rubidium42 deleted the no_magic_share_owner_cont branch March 9, 2022 17:05
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

5 participants