Changing default bound type of a property #925
Unanswered
KerstinKeller
asked this question in
Q&A
Replies: 1 comment
-
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We're currently trying to replace our handwritten Python bindings with nanobind for eCAL.
I have multiple question, but the first one is, how do I easily change the type of the property of the wrapped Python class.
Let's asume the following C++ class:
With this wrapping, the
descriptor
property will be of typestr
.What is the easiest way, and what are the options I have in general, so that the python type for the
descriptor
property isbytes
?Beta Was this translation helpful? Give feedback.
All reactions