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

Does this support converting strings to the appropriate enum? #42

Closed
Zerophase opened this issue Apr 15, 2017 · 4 comments
Closed

Does this support converting strings to the appropriate enum? #42

Zerophase opened this issue Apr 15, 2017 · 4 comments

Comments

@Zerophase
Copy link

Currently, I'm using better enums for the enums I need to be able to iterate over. It works excellent for that. The only issue is I'm using a separate enum for converting cstrings to enums with rt_crc32.

I noticed there is _from_string_nocase(), which works except I used underscores between words in my enum. It doesn't look like make_map is able to go from a string to the enum representation. The string I'm passing in doesn't have the underscore between the two words. Is there anyway to map string to enum, like enum to string has?

@aantron
Copy link
Owner

aantron commented Apr 15, 2017

As I understand you, _from_string_nocase works for strings with underscores exactly as in your enum constants, e.g. _from_string_nocase("Foo_bar") == YourEnum::Foo_bar, assuming you have a suitable definition of YourEnum.

Better Enums doesn't try to replaces spaces by underscores, convert camel case to underscores, or do anything else "fancy" like that – mostly because any choice it could make for one user would be wrong for another user. You may be able to write some kind of function that transforms your input strings before passing them to _from_string_nocase or _from_string, for example, by replacing spaces with underscores.

@aantron
Copy link
Owner

aantron commented Apr 15, 2017

If converting input strings to match your enum constants is not an option for you, you can go in the other direction. At startup, or perhaps using some kind of crazy metaprogramming, you could iterate over all the constants and convert the outputs of _to_string to strings you can then match your input against. You can store these in your favorite lookup data structure, for good performance, if your enum is large enough to worry about that.

@Zerophase
Copy link
Author

Zerophase commented Apr 15, 2017

Yeah, I just need to convert "FooBar" to "FOO_BAR". Yeah, I could write function that would do the conversion at runtime fairly easily. I was just hoping there was a means of defining a custom string representation for an enum. Oh, wait I could just assign an int value at compile time based on a hashed value for the expected string.

@aantron
Copy link
Owner

aantron commented Apr 15, 2017

hoping there was a means of defining a custom string representation for an enum

There isn't at the moment, but you can get the bytes making up the string. You could then write a custom comparison function against them without having to allocate a temporary intermediate string. The interface could also be adjusted, but so far, I think it is possible to get what you want without an excessive amount of trouble, given that this is a sort of "custom" need.

@aantron aantron closed this as completed Aug 17, 2019
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

No branches or pull requests

2 participants