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

Classier enums #6760

Merged
merged 7 commits into from
Aug 24, 2023
Merged

Classier enums #6760

merged 7 commits into from
Aug 24, 2023

Conversation

DomClark
Copy link
Member

@DomClark DomClark commented Jul 2, 2023

Converts most enums to enum classes. A few were left alone for one of the following reasons:

  • Not used as an enum, but as an easy way to define sequential integer constants. (Mostly in LV2 code.)
  • Designed to be extensible (or themselves an extension of another extensible enum), so making them enum classes would render them incompatible with their extensions. (E.g. IPC message IDs, MIDI controller IDs, and Qt data type IDs.)

I also added an lmms::Flags<T> class template, which functions similarly to QFlags<T>. In line with the conversion to enum classes, it is slightly stricter than its Qt equivalent in that it does not implicitly convert to an integer (but this can still be achieved with an explicit cast). There is a new macro in this header for declaring the appropriate operator| for the associated enum type - I'm not proud of it, but I tried and failed to achieve anything nearly as useable with templates.

Other changes worth noting:

  • Unused enums have been removed rather than converted.
  • Final enumerators representing the total number of enumerators have all been renamed to Count. Any that were used numerically have had a std::size_t constant with the original enumerator name added alongside the enum.
  • Unused Count enumerators were removed instead.
  • Added operator+(Octave, Note) -> int. This enables Octave and Note to be converted to enum classes without becoming unwieldy to use, and helps enforce correct usage of these types (i.e. nonsensical operations like Key::F + Key::A don't work).
  • Some bounds checks of enum values have been removed where the relevant enum is only used as a constant. Now enum classes are used, the only way to get these invalid values is to write something obviously wrong like foo(static_cast<FooType>(9001)).
  • A few default cases have been added to switches lacking them - changing the switched value from an integer to an enum type caused some warnings to appear about these, which needed to be fixed since we build with -Werror.

Formatting changes have been kept to a minimum, as this is already a large PR.

@sakertooth sakertooth mentioned this pull request Jul 8, 2023
Copy link
Member

@messmerd messmerd left a comment

Choose a reason for hiding this comment

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

Almost everything looks good to me. I just have a couple questions.

src/core/DataFile.cpp Show resolved Hide resolved
plugins/Sid/SidInstrument.h Outdated Show resolved Hide resolved
@DomClark DomClark merged commit f102777 into LMMS:master Aug 24, 2023
9 checks passed
@DomClark DomClark deleted the classier-enums branch August 24, 2023 18:16
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.

Refactoring: Change enum types to enum class whereever possible.
3 participants