Changing default bound type of a property #925
-
We're currently trying to replace our handwritten Python bindings with nanobind for eCAL.
With this wrapping, the
What is the easiest way, and what are the options I have in general, so that the python type for the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The easiest option is probably to use a def_visitor: template <class T>
struct bytestring_property : nb::def_visitor<bytestring_property<T>> {
bytestring_property(const char* name, std::string T::*mptr) : name(name), mptr(mptr) {}
const char* name;
std::string T::*mptr;
template <typename Class, typename... Extra>
void execute(Class &cl, const Extra&... extra) {
cl.def_prop_rw(name,
[mptr=mptr](const T& self) { return nb::bytes((self.*mptr).c_str(), (self.*mptr).size()); },
[mptr=mptr](T& self, nb::bytes val) { (self.*mptr).assign(val.c_str(), val.size()); });
}
};
nb::class_<DataTypeInformation>(m, "DataTypeInformation")
.def(nb::init<>())
.def(bytestring_property("name", &DataTypeInformation::name))
.def(bytestring_property("encoding", &DataTypeInformation::encoding))
.def(bytestring_property("descriptor", &DataTypeInformation::descriptor)); This is untested but you probably get the picture. If every |
Beta Was this translation helpful? Give feedback.
The easiest option is probably to use a def_visitor: