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

JS_ENUM Defaults #46

Open
13022591351 opened this issue Apr 10, 2023 · 4 comments
Open

JS_ENUM Defaults #46

13022591351 opened this issue Apr 10, 2023 · 4 comments

Comments

@13022591351
Copy link

how can I setenum defaults like this
JS_ENUM(Color, Red = 0x01, Green = 0x02, Blue = 0x03, Yellow4 = 0x04, Purple = 0x05)
struct ColorData {
Color color;

JS_OBJ(color);

};
JS_ENUM_DECLARE_STRING_PARSER(Color)

Compilation can pass, but running or have assert in source code 4438 lines.

I looked at the code and probably understood that this is looking for the name of the enumeration member, but there is no related '=' processing。

Does this library support enumerations with initial values?

@jorgen
Copy link
Owner

jorgen commented Apr 10, 2023

You are correct, there is no support for initial values. The enum support needs some love if anyone out there has the time.

@gerwaric
Copy link
Contributor

gerwaric commented Oct 17, 2023

I have been toying with this problem, and I think a new parser macro for enumeration classes might be the way to go.

I tried improving JS_ENUM by extending populateEnumNames(), but the parser quickly gets complicated, or it has to impose restrictions on how enumerations are declared. I also looked into variadic macros and reflection, but that's tricky business..

So instead I'm using plain enumerations like enum class Color { Red = 1, Blue, Green }; with a macro to define the associated type handler, JS_ENUM_DECLARE_VALUE_PARSER(Color);. The parser checks the signedness of the underlying type and defers to either TypeHandler<int> or TypeHandler<unsigned int>, so the enumumeration is treated as an integer instead of as a string for reads and writes.

@jorgen, if that sounds interesting, would you be open to a pull request?

@jorgen
Copy link
Owner

jorgen commented Oct 17, 2023

Ah, cool! Sure thing👍

@gerwaric
Copy link
Contributor

Done. I've completed some rudimentary testing on my own, but if this looks usable, let me know if you want help creating test cases.

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

3 participants