Make "enum" : "enum class" thus removing enum numbers#1242
Make "enum" : "enum class" thus removing enum numbers#1242TobiKattmann merged 21 commits intodevelopfrom
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.
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
| // 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.
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.
I added the static cast but this was not smart 🤦 (it would make enum class compatible with unsigned short...)
| template <class Tenum, class TField> | ||
| class COptionEnumList final : public COptionBase { |
| 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.
The same "add" function can be used without changes, we just let the compiler deduce the template parameters.
|
|
||
| void CGeometry::FilterValuesAtElementCG(const vector<su2double> &filter_radius, | ||
| const vector<pair<unsigned short,su2double> > &kernels, | ||
| const vector<pair<ENUM_FILTER_KERNEL,su2double> > &kernels, |
There was a problem hiding this comment.
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.
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
| /*--- 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.
Watch out for: implicitly relying that STEADY evaluates to zero/false and everything else to >0 and therefore true.
| 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.
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