Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Simon Heybrock 5 : #include "pybind11.h" 6 : 7 : #include "scipp/variable/transform.h" 8 : 9 : using namespace scipp; 10 : 11 : namespace py = pybind11; 12 : 13 12 : template <class T, class... Ts> void bind_transform(py::module &m) { 14 21 : m.def("transform", [](py::object const &kernel, 15 : const std::conditional_t<true, Variable, Ts> &...vars) { 16 9 : auto fptr_address = kernel.attr("address").cast<intptr_t>(); 17 9 : auto fptr = reinterpret_cast<T (*)(Ts...)>(fptr_address); 18 9 : auto name = kernel.attr("name").cast<std::string>(); 19 10 : return variable::transform<std::tuple<Ts...>>( 20 : vars..., 21 : overloaded{core::transform_flags::expect_no_variance_arg<0>, 22 : core::transform_flags::expect_no_variance_arg<1>, 23 : core::transform_flags::expect_no_variance_arg<2>, 24 : core::transform_flags::expect_no_variance_arg<3>, 25 16 : [&kernel](const units::Unit &u, const auto &...us) { 26 8 : py::gil_scoped_acquire acquire; 27 16 : return py::cast<units::Unit>( 28 16 : kernel.attr("unit_func")(u, us...)); 29 8 : }, 30 12 : [fptr](const auto &...args) { return fptr(args...); }}, 31 17 : name); 32 9 : }); 33 12 : } 34 : 35 3 : void init_transform(py::module &m) { 36 3 : bind_transform<double, double>(m); 37 3 : bind_transform<double, double, double>(m); 38 3 : bind_transform<double, double, double, double>(m); 39 3 : bind_transform<double, double, double, double, double>(m); 40 3 : }