Is there a way to register a value type for an custom integer class? #25314
Unanswered
david-fong
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
0 replies
-
There's a way to do custom marshalling with embind. It's not really documented and there's been some debate whether we should allow it. If you find it useful though that could be helpful for future plans. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have a class in my library that acts as an integer, with an inclusive upper bound specified via non-type template parameter. The bounds are used to pick appropriate, efficient underlying builtin integer types at compile-time, and provide hints to the compiler that could help it optimize. It's a precondition that the values passed by the user to construct these types (whether implicitly or implicitly) are within-bounds- I.e. the burden is on the user of my library to honour the bounds (though I do as much as I can to provide sanity checks that don't affect performance of release-builds).
My library uses these types for function parameters and function return objects (including getter and setters for container-like types exposed by my library), and within the C++ world, the constructors and conversion functions of these integer wrapper classes are defined such that the user of the library doesn't have to explicitly construct them or unwrap them. They can largely write and read their code as if these classes didn't exist, while getting the space-efficiency, compiler optimization, and debug assertion benefits.
I don't want users of the JS bindings for my library to have to manage memory for these integer-wrapper objects. I would prefer that they stay beneath/behind the JS interface, and that there's automatic conversion between them and JS's Number type. The problem domain for my library is such that upper-bounds are unlikely to go higher than 2^32 (well within
Number.MAX_SAFE_INTEGER
). I see that there's such a thing as value types, but I only seevalue_array
andvalue_object
.Is there such a thing as
value_integer
? Should I raise a feature-request? Is there a workaround along the lines of what I see skimming #18813? (there's a comment there that the workaround may break in the future... Are there any current planned changes that would cause it to break?)A related question- is there a way to make this throw a JS exception if user input is greater than the integer-wrapper type's upper-bound?
I know I have the option in my bindings file to define overloads for all my library functions that handle this, and I'm guessing there are other bindings-related workarounds, but I'm hoping there's an easier way.
Beta Was this translation helpful? Give feedback.
All reactions