-
Notifications
You must be signed in to change notification settings - Fork 847
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
Conversation
Provocative thought of the day: Removing the numbers from the enums is a bit of spring cleaning, the "modern C++" way is to use |
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. |
MakePair("NONE", ENUM_STREAMWISE_PERIODIC::NONE) | ||
MakePair("PRESSURE_DROP", ENUM_STREAMWISE_PERIODIC::PRESSURE_DROP) | ||
MakePair("MASSFLOW", ENUM_STREAMWISE_PERIODIC::MASSFLOW) |
There was a problem hiding this comment.
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.
Common/include/option_structure.inl
Outdated
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); |
There was a problem hiding this comment.
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.
Common/include/option_structure.inl
Outdated
void SetDefault() override { | ||
this->field = this->def; | ||
} | ||
void SetDefault() override { field = static_cast<TField>(def); } |
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorted
template <class Tenum, class TField> | ||
class COptionEnumList final : public COptionBase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
available for lists
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) { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 :)
This pull request introduces 2 alerts when merging bd95226 into 949f4e5 - view on LGTM.com new alerts:
|
This pull request introduces 2 alerts when merging 5492805 into 14d7724 - view on LGTM.com new alerts:
|
|
||
default: | ||
SU2_MPI::Error("Unsupported INLET_TYPE.", CURRENT_FUNCTION); | ||
break; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
This pull request introduces 2 alerts when merging e27da1d into 3b20982 - view on LGTM.com new alerts:
|
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 Thanks @pcarruscag for the idea and the setup in |
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.
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