Skip to content

Changing default bound type of a property #925

Answered by oremanj
KerstinKeller asked this question in Q&A
Discussion options

You must be logged in to vote

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, "DataT…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@KerstinKeller
Comment options

@oremanj
Comment options

@KerstinKeller
Comment options

Answer selected by KerstinKeller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants