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

Make "enum" : "enum class" thus removing enum numbers #1242

Merged
merged 21 commits into from
Apr 7, 2021

Conversation

TobiKattmann
Copy link
Contributor

Proposed Changes

Hi all,

The title gives it away. I imagine that piece by piece some commits are chipped in to remove more and more enum numbers. Like that it is more obvious if sth breaks because somewhere someone uses builds on knowing the actual number to do some shenanigans.
Maybe just removing all in a big blow saves time bit #1220 already shows some limitations.

I used the following command (for streamwise periodic in this case) and looked over the results whether I notice sth fishy. Not perfect but might help catchin some easy problems.

grep -r 'NO_STREAMWISE_PERIODIC\|PRESSURE_DROP\|STREAMWISE_MASSFLOW\|Streamwise_Periodic_Map\|GetKind_Streamwise_Periodic' SU2_CFD SU2_DOT SU2_GEO SU2_DEF Common --include=*cpp --include=*hpp --include=*inl

So feel free to commit your removals in here and if it breaks and one doesn't want to search for it just revert that and mention the specific enum so that no-one repeats that

Happy enum-removal

Related Work

@bigfooted mentioned that a couple of times and #1220 discusses some limitations

PR Checklist

  • I am submitting my contribution to the develop branch.
  • My contribution generates no new compiler warnings (try with the '-Wall -Wextra -Wno-unused-parameter -Wno-empty-body' compiler flags, or simply --warnlevel=2 when using meson).
  • My contribution is commented and consistent with SU2 style.
  • I have added a test case that demonstrates my contribution, if necessary.
  • I have updated appropriate documentation (Tutorials, Docs Page, config_template.cpp) , if necessary.

@TobiKattmann TobiKattmann changed the title [WIP] Remove Enum number for streamwise periodic flow [WIP] Remove Enum numbers Mar 23, 2021
@pcarruscag
Copy link
Member

Provocative thought of the day: Removing the numbers from the enums is a bit of spring cleaning, the "modern C++" way is to use enum class and to never rely on implicit conversions from enum to integer type.
In any case, it should be safe to remove the numbers when they already start at zero.

@TobiKattmann
Copy link
Contributor Author

the "modern C++" way is to use enum class and to never rely on implicit conversions from enum to integer type.

I am in. It is a little (actually quite a bit) more involved as the enum namespace has to be given now everywhere an entry is used.
I was not familiar with enum vs enum class if someone else needs an entry point stack overflow, playin around in compiler explorer... oh and of course the c++ core guidelines entry endorses the use of enum class as well

Common/src/CConfig.cpp Outdated Show resolved Hide resolved
Comment on lines +2252 to +2254
MakePair("NONE", ENUM_STREAMWISE_PERIODIC::NONE)
MakePair("PRESSURE_DROP", ENUM_STREAMWISE_PERIODIC::PRESSURE_DROP)
MakePair("MASSFLOW", ENUM_STREAMWISE_PERIODIC::MASSFLOW)
Copy link
Member

Choose a reason for hiding this comment

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

It ends up being a little more verbose but you get to use "names" that would otherwise conflict with what is already in use for objective functions and things like that.

str.append(": invalid option value ");
str.append(option_value[0]);
str.append(". Check current SU2 options in config_template.cfg.");
return str;
}
// If it is there, set the option value
Tenum val = this->m[option_value[0]];
this->field = val;
field = static_cast<TField>(it->second);
Copy link
Member

Choose a reason for hiding this comment

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

The problem you were having is because operator [ ] modified the map if the keys does not exist yet.
But since we use find (which does not modify) we can just store the result and de-reference the iterator instead of "accessing" the map again.

void SetDefault() override {
this->field = this->def;
}
void SetDefault() override { field = static_cast<TField>(def); }
Copy link
Member

Choose a reason for hiding this comment

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

I added the static cast but this was not smart 🤦 (it would make enum class compatible with unsigned short...)

Copy link
Member

Choose a reason for hiding this comment

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

sorted

Comment on lines +191 to +192
template <class Tenum, class TField>
class COptionEnumList final : public COptionBase {
Copy link
Member

Choose a reason for hiding this comment

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

available for lists

Comment on lines -349 to +350
template <class Tenum>
void CConfig::addEnumOption(const string name, unsigned short & option_field, const map<string, Tenum> & enum_map, Tenum default_value) {
template <class Tenum, class TField>
void CConfig::addEnumOption(const string name, TField& option_field, const map<string,Tenum>& enum_map, Tenum default_value) {
Copy link
Member

Choose a reason for hiding this comment

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

The same "add" function can be used without changes, we just let the compiler deduce the template parameters.

const vector<pair<unsigned short,su2double> > &kernels,
const vector<pair<ENUM_FILTER_KERNEL,su2double> > &kernels,
Copy link
Member

Choose a reason for hiding this comment

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

enum class is the safe way to remove the numbers... it breaks the code everywhere :)

@lgtm-com
Copy link

lgtm-com bot commented Mar 24, 2021

This pull request introduces 2 alerts when merging bd95226 into 949f4e5 - view on LGTM.com

new alerts:

  • 2 for Resource not released in destructor

@lgtm-com
Copy link

lgtm-com bot commented Mar 26, 2021

This pull request introduces 2 alerts when merging 5492805 into 14d7724 - view on LGTM.com

new alerts:

  • 2 for Resource not released in destructor

Comment on lines +3746 to +3749

default:
SU2_MPI::Error("Unsupported INLET_TYPE.", CURRENT_FUNCTION);
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that the compiler gets to handle the enum explicitly, it complains if not all cases are listed.
I compile with gcc 5.3.0 and warnlevel=3 and could compile without any warning. The CI build with clang failed due to that warning.

Adding -Wswitch-enum to gcc shows those places but the warning won't go away by adding a simple default: break; like it does for clang, i.e. it is a bit more restrictive. So when using gcc find those spots with the additional warning -> add the default (and error if you like) -> no need to add all options.

Maybe we could be kinder on that warning as well, but on the other hand it is a bit more explicit this way

@@ -1597,7 +1597,7 @@ void CIncEulerSolver::Source_Residual(CGeometry *geometry, CSolver **solver_cont

/*--- Get the physical time. ---*/
su2double time = 0.0;
if (config->GetTime_Marching()) time = config->GetPhysicalTime();
if (config->GetTime_Marching() != TIME_MARCHING::STEADY) time = config->GetPhysicalTime();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Watch out for: implicitly relying that STEADY evaluates to zero/false and everything else to >0 and therefore true.

@@ -1087,7 +1087,7 @@ void CEulerSolver::SetNondimensionalization(CConfig *config, unsigned short iMes
su2double Beta = config->GetAoS()*PI_NUMBER/180.0;
su2double Mach = config->GetMach();
su2double Reynolds = config->GetReynolds();
bool unsteady = (config->GetTime_Marching() != NO);
bool unsteady = (config->GetTime_Marching() != TIME_MARCHING::STEADY);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using another enum's value like NO in this case. Note to self: Maybe introduce an inline bool isdual_time and isunsteady in option_structure as it is used quite often

@su2code su2code deleted a comment from lgtm-com bot Apr 7, 2021
@su2code su2code deleted a comment from lgtm-com bot Apr 7, 2021
@su2code su2code deleted a comment from lgtm-com bot Apr 7, 2021
@su2code su2code deleted a comment from lgtm-com bot Apr 7, 2021
@su2code su2code deleted a comment from lgtm-com bot Apr 7, 2021
@su2code su2code deleted a comment from lgtm-com bot Apr 7, 2021
@TobiKattmann TobiKattmann changed the title [WIP] Remove Enum numbers [WIP] Make "enum" : "enum class" thus removing enum numbers Apr 7, 2021
@lgtm-com
Copy link

lgtm-com bot commented Apr 7, 2021

This pull request introduces 2 alerts when merging e27da1d into 3b20982 - view on LGTM.com

new alerts:

  • 2 for Resource not released in destructor

@TobiKattmann TobiKattmann changed the title [WIP] Make "enum" : "enum class" thus removing enum numbers Make "enum" : "enum class" thus removing enum numbers Apr 7, 2021
@TobiKattmann
Copy link
Contributor Author

Merging this now to keep the PR and the number of problems, that will arise in other code, in bounds. This work will be continued in a future PR.

If your code breaks after merging this PR you most likely just have to add an <ENUM_NAME>:: in front of your enum variable.

Thanks @pcarruscag for the idea and the setup in option_structure.inl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants