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

127 lines
3.6 KiB

  1. /*
  2. * common.h
  3. *
  4. * Created on: 15 Apr 2016
  5. * Author: hbruintjes
  6. */
  7. #pragma once
  8. #include <pybind11/pybind11.h>
  9. #include <pybind11/operators.h>
  10. #include <pybind11/stl.h>
  11. #include <tuple>
  12. namespace py = pybind11;
  13. #if PY_MAJOR_VERSION >= 3
  14. #define PY_DIV "__truediv__"
  15. #define PY_RDIV "__rtruediv__"
  16. #else
  17. #define PY_DIV "__div__"
  18. #define PY_RDIV "__rdiv__"
  19. #endif
  20. PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
  21. PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T const>);
  22. namespace pybind11 {
  23. namespace detail {
  24. /**
  25. * Dummy type caster for handle, so functions can return pybind11 handles directly
  26. */
  27. /*template <> class type_caster<handle> {
  28. public:
  29. bool load(handle src, bool) {
  30. value = handle(src).inc_ref();
  31. return true;
  32. }
  33. static handle cast(handle src, return_value_policy policy, handle parent) {
  34. switch(policy) {
  35. case return_value_policy::automatic:
  36. case return_value_policy::copy:
  37. case return_value_policy::take_ownership:
  38. return handle(src);
  39. case return_value_policy::reference:
  40. return handle(src).inc_ref();
  41. case return_value_policy::reference_internal:
  42. parent.inc_ref();
  43. return handle(src);
  44. }
  45. return handle(src);
  46. }
  47. PYBIND11_TYPE_CASTER(handle, _("handle"));
  48. };*/
  49. /*
  50. template <typename TupleType, typename ... Keys> struct tuple_caster {
  51. typedef TupleType type;
  52. template<typename Type>
  53. using type_conv = type_caster<typename intrinsic_type<Type>::type>;
  54. bool load(handle src, bool convert) {
  55. pybind11::tuple tup(src, true);
  56. if (!tup.check())
  57. return false;
  58. return loadItem<0, Keys...>(tup, convert);
  59. }
  60. static handle cast(const type &src, return_value_policy policy, handle parent) {
  61. pybind11::tuple tup(sizeof...(Keys));
  62. if (!castItem<0, Keys...>(tup, src, policy, parent)) {
  63. return handle();
  64. }
  65. return tup.release();
  66. }
  67. private:
  68. template<int N, typename Type, typename ... Types>
  69. bool loadItem(pybind11::tuple& tup, bool convert) {
  70. type_conv<Type> conv;
  71. if (!conv.load(tup[N], convert))
  72. return false;
  73. std::get<N>(value) = static_cast<Type>(conv);
  74. return loadItem<N+1, Types...>(tup, convert);
  75. }
  76. template<int N, typename Type>
  77. bool loadItem(pybind11::tuple& tup, bool convert) {
  78. type_conv<Type> conv;
  79. if (!conv.load(tup[N], convert))
  80. return false;
  81. std::get<N>(value) = static_cast<Type>(conv);
  82. return true;
  83. }
  84. template<int N, typename Type, typename ... Types>
  85. static bool castItem(pybind11::tuple& tup, const type &src, return_value_policy policy, handle parent) {
  86. auto obj = type_conv<Type>::cast(std::get<N>(src), policy, parent);
  87. object value_ = object(obj, false);
  88. if (!obj) {
  89. return false;
  90. }
  91. return castItem<N+1, Types...>(tup, src, policy, parent);
  92. }
  93. template<int N, typename Type>
  94. static bool castItem(pybind11::tuple& tu, const type &src, return_value_policy policy, handle parent) {
  95. auto obj = type_conv<Type>::cast(std::get<N>(src), policy, parent);
  96. object value_ = object(obj, false);
  97. if (!obj) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. };
  103. template <typename ... Types> struct type_caster<std::tuple<Types...>>
  104. : tuple_caster<std::tuple<Types...>, Types...> { };
  105. template <typename Type1, typename Type2> struct type_caster<std::pair<Type1, Type2>>
  106. : tuple_caster<std::pair<Type1, Type2>, Type1, Type2> { };
  107. */
  108. }
  109. }