Faster way to bind many enums (and in general bind objects that do not require much customization)? #540
-
Sorry I am a beginner, but is there a way to bind objects which do not require much customization, for example large enums where I will not be changing the names of things? Perhaps this is impossible because the compiler destroys information like the names of these enums, but thought I would ask anyway. some c file I am trying to bind: typedef enum
{
EC_ERR_TYPE_SDO_ERROR = 0,
EC_ERR_TYPE_EMERGENCY = 1,
EC_ERR_TYPE_PACKET_ERROR = 3,
EC_ERR_TYPE_SDOINFO_ERROR = 4,
EC_ERR_TYPE_FOE_ERROR = 5,
EC_ERR_TYPE_FOE_BUF2SMALL = 6,
EC_ERR_TYPE_FOE_PACKETNUMBER = 7,
EC_ERR_TYPE_SOE_ERROR = 8,
EC_ERR_TYPE_MBX_ERROR = 9,
EC_ERR_TYPE_FOE_FILE_NOTFOUND = 10,
EC_ERR_TYPE_EOE_INVALID_RX_DATA = 11
} ec_err_type; and my nanobind that feels somewhat arduous: #include <nanobind/nanobind.h>
namespace nb = nanobind;
using namespace nb::literals;
NB_MODULE(soem_ext, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a, "This function adds two numbers and increments if only one is provided.");
// ethercat.h
// nothing in here, yeet
// ethercattype.h
nb::enum_<ec_err_type> (m, "ec_err_type")
.value("EC_ERR_TYPE_SDO_ERROR", ec_err_type::EC_ERR_TYPE_SDO_ERROR)
.value("EC_ERR_TYPE_EMERGENCY", ec_err_type::EC_ERR_TYPE_EMERGENCY)
.value("EC_ERR_TYPE_PACKET_ERROR", ec_err_type::EC_ERR_TYPE_PACKET_ERROR)
.value("EC_ERR_TYPE_SDOINFO_ERROR", ec_err_type::EC_ERR_TYPE_FOE_ERROR)
.value("EC_ERR_TYPE_FOE_ERROR", ec_err_type::EC_ERR_TYPE_FOE_ERROR)
.value("EC_ERR_TYPE_FOE_BUF2SMALL", ec_err_type::EC_ERR_TYPE_FOE_BUF2SMALL)
.value("EC_ERR_TYPE_FOE_PACKET_NUMBER", ec_err_type::EC_ERR_TYPE_FOE_PACKETNUMBER)
.value("EC_ERR_TYPE_SOE_ERROR", ec_err_type::EC_ERR_TYPE_SOE_ERROR)
.value("EC_ERR_TYPE_MBX_ERROR", ec_err_type::EC_ERR_TYPE_MBX_ERROR)
.value("EC_ERR_TYPE_FOE_FILE_NOTFOUND", ec_err_type::EC_ERR_TYPE_FOE_FILE_NOTFOUND)
.value("EC_ERR_TYPE_EOE_INVALID_RX_DATA", ec_err_type::EC_ERR_TYPE_EOE_INVALID_RX_DATA);
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You are looking for some kind of code generator that uses libclang or similar to produce bindings automatically. Such things have been created before (esp. in the pybind11 context): https://jslee02.github.io/awesome-cpp-python-binding-generator/ It's not a feature that would be considered for inclusion in nanobind (it's beyond the scope of this project). |
Beta Was this translation helpful? Give feedback.
You are looking for some kind of code generator that uses libclang or similar to produce bindings automatically. Such things have been created before (esp. in the pybind11 context): https://jslee02.github.io/awesome-cpp-python-binding-generator/
It's not a feature that would be considered for inclusion in nanobind (it's beyond the scope of this project).