|
| 1 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 2 | +// See https://llvm.org/LICENSE.txt for license information. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | +// Copyright (c) 2025. |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <nanobind/nanobind.h> |
| 9 | + |
| 10 | +namespace eudsl { |
| 11 | +template <typename T, typename... Ts> |
| 12 | +struct non_copying_non_moving_class_ : nanobind::class_<T, Ts...> { |
| 13 | + template <typename... Extra> |
| 14 | + NB_INLINE non_copying_non_moving_class_(nanobind::handle scope, |
| 15 | + const char *name, |
| 16 | + const Extra &...extra) { |
| 17 | + nanobind::detail::type_init_data d; |
| 18 | + |
| 19 | + d.flags = 0; |
| 20 | + d.align = (uint8_t)alignof(typename nanobind::class_<T, Ts...>::Alias); |
| 21 | + d.size = (uint32_t)sizeof(typename nanobind::class_<T, Ts...>::Alias); |
| 22 | + d.name = name; |
| 23 | + d.scope = scope.ptr(); |
| 24 | + d.type = &typeid(T); |
| 25 | + |
| 26 | + if constexpr (!std::is_same_v<typename nanobind::class_<T, Ts...>::Base, |
| 27 | + T>) { |
| 28 | + d.base = &typeid(typename nanobind::class_<T, Ts...>::Base); |
| 29 | + d.flags |= (uint32_t)nanobind::detail::type_init_flags::has_base; |
| 30 | + } |
| 31 | + |
| 32 | + if constexpr (std::is_destructible_v<T>) { |
| 33 | + d.flags |= (uint32_t)nanobind::detail::type_flags::is_destructible; |
| 34 | + |
| 35 | + if constexpr (!std::is_trivially_destructible_v<T>) { |
| 36 | + d.flags |= (uint32_t)nanobind::detail::type_flags::has_destruct; |
| 37 | + d.destruct = nanobind::detail::wrap_destruct<T>; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if constexpr (nanobind::detail::has_shared_from_this_v<T>) { |
| 42 | + d.flags |= (uint32_t)nanobind::detail::type_flags::has_shared_from_this; |
| 43 | + d.keep_shared_from_this_alive = [](PyObject *self) noexcept { |
| 44 | + if (auto sp = nanobind::inst_ptr<T>(self)->weak_from_this().lock()) { |
| 45 | + nanobind::detail::keep_alive( |
| 46 | + self, new auto(std::move(sp)), |
| 47 | + [](void *p) noexcept { delete (decltype(sp) *)p; }); |
| 48 | + return true; |
| 49 | + } |
| 50 | + return false; |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + (nanobind::detail::type_extra_apply(d, extra), ...); |
| 55 | + |
| 56 | + this->m_ptr = nanobind::detail::nb_type_new(&d); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +template <typename NewReturn, typename Return, typename... Args> |
| 61 | +constexpr auto coerceReturn(Return (*pf)(Args...)) noexcept { |
| 62 | + return [&pf](Args &&...args) -> NewReturn { |
| 63 | + return pf(std::forward<Args>(args)...); |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +template <typename NewReturn, typename Return, typename Class, typename... Args> |
| 68 | +constexpr auto coerceReturn(Return (Class::*pmf)(Args...), |
| 69 | + std::false_type = {}) noexcept { |
| 70 | + return [&pmf](Class *cls, Args &&...args) -> NewReturn { |
| 71 | + return (cls->*pmf)(std::forward<Args>(args)...); |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | + * If you get |
| 77 | + * ``` |
| 78 | + * Called object type 'void(MyClass::*)(vector<Item>&,int)' is not a function or |
| 79 | + * function pointer |
| 80 | + * ``` |
| 81 | + * it's because you're calling a member function without |
| 82 | + * passing the `this` pointer as the first arg |
| 83 | + */ |
| 84 | +template <typename NewReturn, typename Return, typename Class, typename... Args> |
| 85 | +constexpr auto coerceReturn(Return (Class::*pmf)(Args...) const, |
| 86 | + std::true_type) noexcept { |
| 87 | + // copy the *pmf, not capture by ref |
| 88 | + return [pmf](const Class &cls, Args &&...args) -> NewReturn { |
| 89 | + return (cls.*pmf)(std::forward<Args>(args)...); |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +inline size_t wrap(Py_ssize_t i, size_t n) { |
| 94 | + if (i < 0) |
| 95 | + i += (Py_ssize_t)n; |
| 96 | + |
| 97 | + if (i < 0 || (size_t)i >= n) |
| 98 | + throw nanobind::index_error(); |
| 99 | + |
| 100 | + return (size_t)i; |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace eudsl |
0 commit comments