|
14 | 14 |
|
15 | 15 | #include <thread>
|
16 | 16 |
|
| 17 | +namespace test_callbacks { |
| 18 | +namespace boost_histogram { // See PR #5580 |
| 19 | + |
| 20 | +double custom_transform_double(double value) { return value * 3; } |
| 21 | +int custom_transform_int(int value) { return value; } |
| 22 | + |
| 23 | +// Originally derived from |
| 24 | +// https://github.com/scikit-hep/boost-histogram/blob/460ef90905d6a8a9e6dd3beddfe7b4b49b364579/include/bh_python/transform.hpp#L68-L85 |
| 25 | +double apply_custom_transform(const py::object &src, double value) { |
| 26 | + using raw_t = double(double); |
| 27 | + |
| 28 | + py::detail::make_caster<std::function<raw_t>> func_caster; |
| 29 | + if (!func_caster.load(src, /*convert*/ false)) { |
| 30 | + return -100; |
| 31 | + } |
| 32 | + auto func = static_cast<std::function<raw_t> &>(func_caster); |
| 33 | + auto *cfunc = func.target<raw_t *>(); |
| 34 | + if (cfunc == nullptr) { |
| 35 | + return -200; |
| 36 | + } |
| 37 | + return (*cfunc)(value); |
| 38 | +} |
| 39 | + |
| 40 | +} // namespace boost_histogram |
| 41 | +} // namespace test_callbacks |
| 42 | + |
17 | 43 | int dummy_function(int i) { return i + 1; }
|
18 | 44 |
|
19 | 45 | TEST_SUBMODULE(callbacks, m) {
|
@@ -272,4 +298,11 @@ TEST_SUBMODULE(callbacks, m) {
|
272 | 298 | // rec_capsule with nullptr name
|
273 | 299 | py::capsule rec_capsule2(std::malloc(1), [](void *data) { std::free(data); });
|
274 | 300 | m.add_object("custom_function2", PyCFunction_New(custom_def, rec_capsule2.ptr()));
|
| 301 | + |
| 302 | + m.def("boost_histogram_custom_transform_double", |
| 303 | + test_callbacks::boost_histogram::custom_transform_double); |
| 304 | + m.def("boost_histogram_custom_transform_int", |
| 305 | + test_callbacks::boost_histogram::custom_transform_int); |
| 306 | + m.def("boost_histogram_apply_custom_transform", |
| 307 | + test_callbacks::boost_histogram::apply_custom_transform); |
275 | 308 | }
|
0 commit comments