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

type security to enumerated string, extra test of non-class enum #36284

Merged
merged 2 commits into from
Oct 16, 2023

Conversation

rboston628
Copy link
Contributor

@rboston628 rboston628 commented Oct 13, 2023

Description of work

Added a check to EnumeratedString to ensure class E is an enum. More tests of comparisons to underlying type of the enum.

Purpose of work

Ahead of lightning talk on EnumeratedString at mantid dev workshop 2023, I revisited this and added minor refactors.

Summary of work
Adding the line

static_assert(std::is_enum_v<E>);

which causes a compiler error if attempting to make an enumerated string without using an enum as the class.

Added some addition tests that verify behavior if not using an enum class, that the EnumeratedString can be compared both with the enum and with the underlying type.

Further detail of work

That's all there is.

To test:

Within EnumeratedStringTest.h, try adding the following to the namespace at L20:

struct NotAnEnum { static const int enum_count = 1; };
const std::vector<std::string> notAnEnum {"1"};

and inside the test class add:

  void testNotAnEnum() {
    EnumeratedString<NotAnEnum, &notAnEnum> bad (1);
  }

and try to compile. This should generate an error like the following:

mantid/Framework/Kernel/inc/MantidKernel/EnumeratedString.h:43:22: error: static assertion failed
      |   static_assert(std::is_enum_v<E>); // cause a compiler error if E is not an enum
      |                 ~~~~~^~~~~~~~~~~~
In file included from mantid/build/Framework/Kernel/test/EnumeratedStringTest.cpp:18:

There is no associated issue.

This does not require release notes because it is refacroting an internal feature that is not user-facing.


Reviewer

Please comment on the points listed below (full description).
Your comments will be used as part of the gatekeeper process, so please comment clearly on what you have checked during your review. If changes are made to the PR during the review process then your final comment will be the most important for gatekeepers. In this comment you should make it clear why any earlier review is still valid, or confirm that all requested changes have been addressed.

Code Review
  • Is the code of an acceptable quality?
  • Does the code conform to the coding standards?
  • Are the unit tests small and test the class in isolation?
  • If there is GUI work does it follow the GUI standards?
  • If there are changes in the release notes then do they describe the changes appropriately?
  • Do the release notes conform to the release notes guide?
Functional Tests
  • Do changes function as described? Add comments below that describe the tests performed?
  • Do the changes handle unexpected situations, e.g. bad input?
  • Has the relevant (user and developer) documentation been added/updated?

Does everything look good? Mark the review as Approve. A member of @mantidproject/gatekeepers will take care of it.

Gatekeeper

If you need to request changes to a PR then please add a comment and set the review status to "Request changes". This will stop the PR from showing up in the list for other gatekeepers.

@rboston628
Copy link
Contributor Author

I locally ran

cmake -DDCS_SCREENSHOTS=on .
ninja docs-html

just to be sure.

@rboston628 rboston628 marked this pull request as ready for review October 13, 2023 16:45
Copy link
Collaborator

@bolotovskyr bolotovskyr left a comment

Choose a reason for hiding this comment

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

This PR adds an important type safety check which should steer developers towards the correct usage of EnumeratedString. Also, a new unit test with a non-class enum is very useful. I have built the branch locally and verified that the compilation fails if we try to instantiate an EnumeratedString object with a non-enum type.

@sf1919 sf1919 added this to the Release 6.9 milestone Oct 15, 2023
@AndreiSavici AndreiSavici merged commit c47df2f into main Oct 16, 2023
8 checks passed
@AndreiSavici AndreiSavici deleted the patch-enumerated-string2 branch October 16, 2023 13:29
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.

4 participants