diff --git a/subprojects/pyntcore/gen/GenericEntry.yml b/subprojects/pyntcore/gen/GenericEntry.yml index 895aceb7..ca2b2269 100644 --- a/subprojects/pyntcore/gen/GenericEntry.yml +++ b/subprojects/pyntcore/gen/GenericEntry.yml @@ -1,5 +1,8 @@ --- +extra_includes: +- src/nt_type_caster.h + classes: GenericSubscriber: methods: diff --git a/subprojects/pyntcore/gen/RawTopic.yml b/subprojects/pyntcore/gen/RawTopic.yml index 7fb6c47f..fd992c20 100644 --- a/subprojects/pyntcore/gen/RawTopic.yml +++ b/subprojects/pyntcore/gen/RawTopic.yml @@ -1,5 +1,8 @@ --- +extra_includes: +- src/nt_type_caster.h + classes: RawSubscriber: methods: diff --git a/subprojects/pyntcore/gen/ntcore_cpp_types.yml b/subprojects/pyntcore/gen/ntcore_cpp_types.yml index 204c812d..7a125a05 100644 --- a/subprojects/pyntcore/gen/ntcore_cpp_types.yml +++ b/subprojects/pyntcore/gen/ntcore_cpp_types.yml @@ -1,5 +1,8 @@ --- +extra_includes: +- src/nt_type_caster.h + defaults: ignore: true report_ignored_missing: false diff --git a/subprojects/pyntcore/ntcore/src/nt_type_caster.h b/subprojects/pyntcore/ntcore/src/nt_type_caster.h new file mode 100644 index 00000000..de9158f2 --- /dev/null +++ b/subprojects/pyntcore/ntcore/src/nt_type_caster.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +namespace pybind11 { +namespace detail { + +// ntcore uses std::vector anytime there is a raw value, so +// add this specialization to convert to/from bytes directly + +template<> +struct type_caster> { + using vector_type = std::vector; + PYBIND11_TYPE_CASTER(vector_type, const_name("bytes")); + + bool load(handle src, bool convert) { + if (!isinstance(src)) { + return false; + } + auto buf = reinterpret_borrow(src); + auto req = buf.request(); + if (req.ndim != 1) { + return false; + } + + auto begin = (const uint8_t*)req.ptr; + auto end = begin + req.size*req.itemsize; + + value = std::vector(begin, end); + return true; + } + + static handle cast(const std::vector &src, return_value_policy policy, handle parent) { + return py::bytes((char*)src.data(), src.size()).release(); + } +}; + +} +} \ No newline at end of file