You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pybind11_bind.h 3.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #ifndef PYBIND11_NCNN_BIND_H
  16. #define PYBIND11_NCNN_BIND_H
  17. #include <pybind11/functional.h>
  18. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // virtual function pass by reference by https://github.com/pybind/pybind11/issues/2033
  20. #define PYBIND11_OVERRIDE_REFERENCE_IMPL(ret_type, cname, name, ...) \
  21. do \
  22. { \
  23. pybind11::gil_scoped_acquire gil; \
  24. pybind11::function override = pybind11::get_override(static_cast<const cname*>(this), name); \
  25. if (override) \
  26. { \
  27. auto o = override.operator()<pybind11::return_value_policy::reference>(__VA_ARGS__); \
  28. if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) \
  29. { \
  30. static pybind11::detail::override_caster_t<ret_type> caster; \
  31. return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
  32. } \
  33. else \
  34. return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
  35. } \
  36. } while (false)
  37. #define PYBIND11_OVERRIDE_REFERENCE_NAME(ret_type, cname, name, fn, ...) \
  38. do \
  39. { \
  40. PYBIND11_OVERRIDE_REFERENCE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
  41. return cname::fn(__VA_ARGS__); \
  42. } while (false)
  43. #define PYBIND11_OVERRIDE_REFERENCE(ret_type, cname, fn, ...) \
  44. PYBIND11_OVERRIDE_REFERENCE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. #endif