-
Notifications
You must be signed in to change notification settings - Fork 104
Macro for creating enum classes #1841
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
Conversation
Allows creating an 'enum class' along with toString and <enum name>FromString functions and an Options::as<enum name>() overload for setting an instance of the enum class from options. Also includes operator<< overload so that Options::assign() and Options::withDefault() work.
|
I think this is probably essential! Worthwhile looking over some other implementations of compiletime enum reflection:
Some of these are C++17 only, but could be backported depending on what they use; some make an actual I've almost finished adding |
|
Thanks @johnomotani I think this is both a wonderful abuse of macros, and a very useful feature. The overload of |
|
Can the work in progress label be removed? |
|
Documentation of the new macro is definitely still needed. @ZedThree - are there any useful changes to make right now from your earlier comment, or do you think it is easy enough to upgrade later? |
include/bout/bout_enum_class.hxx
Outdated
| return found->second; \ | ||
| } \ | ||
| \ | ||
| inline enumname _MAKE_FROMSTRING_NAME(enumname)(std::string s) { \ |
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.
std::string s -> const std::string& s
|
Adding the fmt stuff can be done later once I work out how to do it. One thing to change now though is that identifiers beginning with an underscore followed by a capital are reserved, so maybe change those -- I suggest just prefixing everything with |
|
Also, might need to use this trick to get But you'll need a Windows system to test it on, so that can wait |
Leading underscore followed by capital is reserved. Prefix the macros with BOUT_ instead.
After the discussion on #1835 I was thinking about using an
enum classas an option. The way we are usingenum classes at the moment has some boiler-plate to definetoStringandenumFromStringfunctions. Using some magic copied frombout/macro_for_each.hxx, it seems to be possible to replace that with a macro so you can write something likeand get an
enum classwith possible valuesmyenum::fooandmyenum::bar, along withtoString(myenum e)andmyenumFromString(std::string s)functions. I've also put in overloads forOptions::as<>()andostream& operator<<so that theenum classcan be loaded from options, e.g.I'd also like to be able to assign a string to the
enum class, but it doesn't seem to be possible to overrideoperator=for anenum class.Before I think about this any more: is this a useful feature, or an abuse of macros?