| 1 | /* Tencent is pleased to support the open source community by making ncnn available. |
| 2 | * |
| 3 | * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | * |
| 5 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | * in compliance with the License. You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://opensource.org/licenses/BSD-3-Clause |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software distributed |
| 11 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | * specific language governing permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef PYBIND11_NCNN_BIND_H |
| 17 | #define PYBIND11_NCNN_BIND_H |
| 18 | |
| 19 | #include <pybind11/functional.h> |
| 20 | |
| 21 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 22 | // virtual function pass by reference by https://github.com/pybind/pybind11/issues/2033 |
| 23 | #define PYBIND11_OVERRIDE_REFERENCE_IMPL(ret_type, cname, name, ...) \ |
| 24 | do \ |
| 25 | { \ |
| 26 | pybind11::gil_scoped_acquire gil; \ |
| 27 | pybind11::function override = pybind11::get_override(static_cast<const cname*>(this), name); \ |
| 28 | if (override) \ |
| 29 | { \ |
| 30 | auto o = override.operator()<pybind11::return_value_policy::reference>(__VA_ARGS__); \ |
| 31 | if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) \ |
| 32 | { \ |
| 33 | static pybind11::detail::override_caster_t<ret_type> caster; \ |
| 34 | return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \ |
| 35 | } \ |
| 36 | else \ |
| 37 | return pybind11::detail::cast_safe<ret_type>(std::move(o)); \ |
| 38 | } \ |
| 39 | } while (false) |
| 40 | |
| 41 | #define PYBIND11_OVERRIDE_REFERENCE_NAME(ret_type, cname, name, fn, ...) \ |
| 42 | do \ |
| 43 | { \ |
| 44 | PYBIND11_OVERRIDE_REFERENCE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ |
| 45 | return cname::fn(__VA_ARGS__); \ |
| 46 | } while (false) |
| 47 | |
| 48 | #define PYBIND11_OVERRIDE_REFERENCE(ret_type, cname, fn, ...) \ |
| 49 | PYBIND11_OVERRIDE_REFERENCE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) |
| 50 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | #endif |