-
Hi! I'm making Python bindings for ImGui and trying to find a way to bind some function arguments with less boilerplate. I'm facing an enum pattern where enums are declared with normal C++ enums, but functions using the enum are in fact not using the enum type but rather an int typedef. Here's how the code looks like:
I can get the above translated into a Python IntFlag enum. This part is fine:
Here's how these types are used in ImGui:
Note how the int typedef This leads to a fair bit of boilerplate in
This pattern is repeated many times over in the ImGui API. I wonder if there's a better way to express this, without having to write those lambda wrappers for each API function that uses flags like above? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
C++ template metaprogramming can't tell the difference between a typedef and the type that it expands to. If you can't change the underlying library to use a stronger type for these flags arguments, then it's sort of fundamentally impossible for nanobind to tell what's going on on its own. C++ template metaprogramming also can't tell the argument names of a function, so any option that is less boilerplate-y would lose those in the bindings. If the signatures for these functions are relatively consistent, such as if the flags argument is always the final one, you might be able to eliminate some of the boilerplate using a def_visitor: (example is untested)
I don't think I would personally find this to be worth the trouble unless you have, like, more than a hundred of these. Less complicated option that would only work if you know on your platform's ABI that the int and the enum have the same calling convention:
|
Beta Was this translation helpful? Give feedback.
C++ template metaprogramming can't tell the difference between a typedef and the type that it expands to. If you can't change the underlying library to use a stronger type for these flags arguments, then it's sort of fundamentally impossible for nanobind to tell what's going on on its own.
C++ template metaprogramming also can't tell the argument names of a function, so any option that is less boilerplate-y would lose those in the bindings.
If the signatures for these functions are relatively consistent, such as if the flags argument is always…