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 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #ifndef PYBIND11_NCNN_BIND_H
  4. #define PYBIND11_NCNN_BIND_H
  5. #include <pybind11/functional.h>
  6. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. // virtual function pass by reference by https://github.com/pybind/pybind11/issues/2033
  8. #define PYBIND11_OVERRIDE_REFERENCE_IMPL(ret_type, cname, name, ...) \
  9. do \
  10. { \
  11. pybind11::gil_scoped_acquire gil; \
  12. pybind11::function override = pybind11::get_override(static_cast<const cname*>(this), name); \
  13. if (override) \
  14. { \
  15. auto o = override.operator()<pybind11::return_value_policy::reference>(__VA_ARGS__); \
  16. if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) \
  17. { \
  18. static pybind11::detail::override_caster_t<ret_type> caster; \
  19. return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
  20. } \
  21. else \
  22. return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
  23. } \
  24. } while (false)
  25. #define PYBIND11_OVERRIDE_REFERENCE_NAME(ret_type, cname, name, fn, ...) \
  26. do \
  27. { \
  28. PYBIND11_OVERRIDE_REFERENCE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
  29. return cname::fn(__VA_ARGS__); \
  30. } while (false)
  31. #define PYBIND11_OVERRIDE_REFERENCE(ret_type, cname, fn, ...) \
  32. PYBIND11_OVERRIDE_REFERENCE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. #endif