diff --git a/py_src/bind_vector_copy.h b/py_src/bind_vector_copy.h index 2f4bd88..8a57f40 100644 --- a/py_src/bind_vector_copy.h +++ b/py_src/bind_vector_copy.h @@ -64,10 +64,10 @@ nanobind::class_ bind_vector_copy(nanobind::handle scope, const char *na cl.def(init(), "Copy constructor"); - cl.def("__init__", [](Vector *v, typed> &seq) { + cl.def("__init__", [](Vector *v, typed &seq) { new (v) Vector(); - v->reserve(len_hint(seq.value)); - for (handle h : seq.value) + v->reserve(len_hint(seq)); + for (handle h : seq) v->push_back(cast(h)); }, "Construct from an iterable object"); @@ -248,10 +248,10 @@ nanobind::class_> bind_shared_vector_copy(nanobind::hand new (self) self_t(std::move(std::make_shared(*other))); }, "Shallow Copy constructor"); - cl.def("__init__", [](self_t &v, typed> &seq) { + cl.def("__init__", [](self_t &v, typed &seq) { new (&v) self_t(std::move(std::make_shared())); - v->reserve(len_hint(seq.value)); - for (handle h : seq.value) + v->reserve(len_hint(seq)); + for (handle h : seq) v->push_back(cast(h)); }, "Construct from an iterable object"); @@ -457,10 +457,10 @@ nanobind::class_>> bind_shared_pyvec(nanob new (self) self_t(std::move(std::make_shared(*other))); }, "Shallow Copy constructor"); - cl.def("__init__", [](self_t &v, typed> &seq) { + cl.def("__init__", [](self_t &v, typed &seq) { new (&v) self_t(std::move(std::make_shared())); - v->reserve(len_hint(seq.value)); - for (handle h : seq.value) + v->reserve(len_hint(seq)); + for (handle h : seq) v->push_back(*cast(h)); }, "Construct from an iterable object"); diff --git a/py_src/core.cpp b/py_src/core.cpp index 86bd4d4..848e2da 100644 --- a/py_src/core.cpp +++ b/py_src/core.cpp @@ -173,12 +173,14 @@ auto bind_track(nb::module_& m, const std::string& name_) { mode_enums[i] = str_to_pianoroll_mode(modes[i]); } auto pianoroll = TrackPianoroll::from_track(*self, mode_enums, pitch_range, encode_velocity); + auto* data = const_cast(pianoroll.release()); + nb::capsule owner(data, [](void* d) noexcept { delete[] (u8*) d; }); return nb::ndarray{ - const_cast(pianoroll.release()), { + data, { std::get<0>(pianoroll.dims()), std::get<1>(pianoroll.dims()), std::get<2>(pianoroll.dims()), - } + }, owner }; }); } @@ -522,12 +524,15 @@ auto bind_score(nb::module_& m, const std::string& name_) { } auto pianoroll = ScorePianoroll::from_score(*self, mode_enums, pitch_range, encode_velocity); + auto* data = const_cast(pianoroll.release()); + nb::capsule owner(data, [](void* d) noexcept { delete[] (u8*) d; }); return nb::ndarray{ - const_cast(pianoroll.release()), - {std::get<0>(pianoroll.dims()), - std::get<1>(pianoroll.dims()), - std::get<2>(pianoroll.dims()), - std::get<3>(pianoroll.dims())} + data, { + std::get<0>(pianoroll.dims()), + std::get<1>(pianoroll.dims()), + std::get<2>(pianoroll.dims()), + std::get<3>(pianoroll.dims()) + }, owner }; }, nb::arg("modes"), diff --git a/pyproject.toml b/pyproject.toml index 43878bb..9179fe0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = [ "setuptools>=42", "scikit-build>=0.13", "scikit-build-core>=0.3.3", - "nanobind>=1.8.0", + "nanobind>=2.0.0", "cmake>=3.20", "ninja", ]