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

Tile inspector element type #13134

Merged
merged 2 commits into from Jan 3, 2021
Merged

Tile inspector element type #13134

merged 2 commits into from Jan 3, 2021

Conversation

amdoku
Copy link
Contributor

@amdoku amdoku commented Oct 9, 2020

should fix #12444

introduced a convenience method to typesafe convert from the enum class to the underlying integer type, as the enum is used quite a lot as a lookup value for tables.

@tupaschoal tupaschoal added this to In progress in Refactor enums to use strong enums via automation Oct 9, 2020
Copy link
Member

@tupaschoal tupaschoal left a comment

Choose a reason for hiding this comment

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

Can you please rebase and do two things:

  • Drop the merge commit
  • Fold the other commits into a single one, named:
    Close #12444: Refactor TILE_INSPECTOR_PAGE to use strong enum

Here's a page explaiing it: https://github.com/OpenRCT2/OpenRCT2/wiki/Rebase-and-Sync-fork-with-OpenRCT2
You'll also need to fix the formatting issues pointed out by the CI

src/openrct2/windows/tile_inspector.h Outdated Show resolved Hide resolved
src/openrct2/interface/Window_internal.h Outdated Show resolved Hide resolved
@duncanspumpkin
Copy link
Contributor

This would go in contrast to every other window. I don't think we should make this change.

@amdoku
Copy link
Contributor Author

amdoku commented Oct 10, 2020

The page variable - this one - is accessed very often, and reused across different windows with different value ranges.

i.e. in Cheats.cpp it is used to get the value from a lookup table, which contains only four entries, TileInspectorPage containing 10.

This seems to be a reoccurring pattern in OpenRCT2, since there are a lot of union types already defined in Window_internal.h
My current approach is to replicate the union pattern for page as well.

I assume this is what @duncanspumpkin meant - please correct me if I am wrong.

@duncanspumpkin
Copy link
Contributor

The problem is that every window type has its own number of pages/tabs. If we use an enum class for each of them we will need a union of every window type page type. This would be annoying to use. Alternatively we could make a generic enum class for pages 1-X and have aliases that translate a page specific name to an enum class page number. But then it's not really giving us anything useful protection wise.

I think we need to ask ourselves what are we trying to achieve and what does the final product look like. What do we want developers not to do and what do we want them to do. Off the top of my head we want to reduce macro usage to reduce name pollution. Reduce enum usage to reduce name pollution and allow for strong typing. Reduce code duplication. Prevent setting pages that don't exist. We have talked for a while about moving windows to individual classes and that will give us more tools to tackle this sort of thing but we still need to think about what the end goal should be.

@Broxzier
Copy link
Member

We have talked for a while about moving windows to individual classes and that will give us more tools to tackle this sort of thing but we still need to think about what the end goal should be.

Pages could be lists of widgets in a vector stored with the window. Page indices or IDs are not needed, if you want to access a specific page, a pointer to the item in the vector could be used instead (which can be stored separately for better naming, of course).

@amdoku
Copy link
Contributor Author

amdoku commented Oct 16, 2020

Some thoughts:

  • A blank pointer into a std::vector, which might reallocate, would result in a use after free bug, a fixed sized std::array would have the same storage layout and very similar API - but avoids the potential use after free.
  • Any container approach would have to introduce polymorphism, otherwise you would not be able to store different window types in a container - I am not sure if this is something you would be opposed to (ignoring the std::vector<std::variant<A,B,C,D,E,F> > approach for now)
  • Going down the road to remove macro usage and have the undesirable union type of all panes, might not be a bad way to go forward. All the logic for the panes is already in their own class, reusing the union member variable for individual state storage. First converting to strong enums would allow for all state sharing bugs to be found and/or fixed. Then a gradual transition on a per window basis is possible without having to worry about side effects in other windows.

This is from a first time contributor to this project, so I am very much aware that all of this has been talked about by people who have much more knowledge about this project already and I just could not find it.

@duncanspumpkin
Copy link
Contributor

Currently the big unions in window_internal are for accessing variables that should be private variables of the window if it was turned into class. In general writing to the page variable is done only from the specific window. Reading of the page value is also generally from the specific window but is at times also read in other locations. I'm not really suggesting anything here just writing down thoughts.

@tupaschoal
Copy link
Member

I'm not really suggesting anything here just writing down thoughts.

Well we do need a consensus or a general direction at some point for the PR to be merged/closed ahahah

@tupaschoal
Copy link
Member

First of all, sorry for getting back to this after a while.

Going down the road to remove macro usage and have the undesirable union type of all panes, might not be a bad way to go forward. All the logic for the panes is already in their own class, reusing the union member variable for individual state storage. First converting to strong enums would allow for all state sharing bugs to be found and/or fixed. Then a gradual transition on a per window basis is possible without having to worry about side effects in other windows.

I'm very convinced about that too. The union will look ugly, but decoupling will be nice when we have windows split up. I'm ok with merging this, are you @duncanspumpkin ?

(If so, we'll need merge conflicts to be resolved @amdoku )

@duncanspumpkin
Copy link
Contributor

okay but please keep in mind this is not the end goal

@tupaschoal
Copy link
Member

Decoupling is not the end goal? I thought ultimately we wanted a polymorphic window infra

@duncanspumpkin
Copy link
Contributor

I meant that the union we have isn't the end goal.

@tupaschoal
Copy link
Member

I know (?), for me it's a means to an end, we get a big ugly union but then we slowly move the corresponding variable to its actual window class.

@amdoku
Copy link
Contributor Author

amdoku commented Dec 28, 2020

No problem. Discussions take time.

I'll try to find time around new years eve.

@tupaschoal tupaschoal merged commit 85efe04 into OpenRCT2:develop Jan 3, 2021
Refactor enums to use strong enums automation moved this from In progress to Done Jan 3, 2021
@tupaschoal tupaschoal added this to the v0.3.3 milestone Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Refactor TILE_INSPECTOR_PAGE to use strong enum
4 participants