pytype_function.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright David Abrahams 2002, Nikolay Mladenov 2007.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef WRAP_PYTYPE_NM20070606_HPP
  6. # define WRAP_PYTYPE_NM20070606_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/converter/registered.hpp>
  9. # include <boost/python/detail/unwind_type.hpp>
  10. # include <boost/python/detail/type_traits.hpp>
  11. namespace boost { namespace python {
  12. namespace converter
  13. {
  14. template <PyTypeObject const* python_type>
  15. struct wrap_pytype
  16. {
  17. static PyTypeObject const* get_pytype()
  18. {
  19. return python_type;
  20. }
  21. };
  22. typedef PyTypeObject const* (*pytype_function)();
  23. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  24. namespace detail
  25. {
  26. struct unwind_type_id_helper{
  27. typedef python::type_info result_type;
  28. template <class U>
  29. static result_type execute(U* ){
  30. return python::type_id<U>();
  31. }
  32. };
  33. template <class T>
  34. inline python::type_info unwind_type_id_(boost::type<T>* = 0, mpl::false_ * =0)
  35. {
  36. return boost::python::detail::unwind_type<unwind_type_id_helper, T> ();
  37. }
  38. inline python::type_info unwind_type_id_(boost::type<void>* = 0, mpl::true_* =0)
  39. {
  40. return type_id<void>();
  41. }
  42. template <class T>
  43. inline python::type_info unwind_type_id(boost::type<T>* p= 0)
  44. {
  45. return unwind_type_id_(p, (mpl::bool_<boost::python::detail::is_void<T>::value >*)0 );
  46. }
  47. }
  48. template <class T>
  49. struct expected_pytype_for_arg
  50. {
  51. static PyTypeObject const *get_pytype()
  52. {
  53. const converter::registration *r=converter::registry::query(
  54. detail::unwind_type_id_((boost::type<T>*)0, (mpl::bool_<boost::python::detail::is_void<T>::value >*)0 )
  55. );
  56. return r ? r->expected_from_python_type(): 0;
  57. }
  58. };
  59. template <class T>
  60. struct registered_pytype
  61. {
  62. static PyTypeObject const *get_pytype()
  63. {
  64. const converter::registration *r=converter::registry::query(
  65. detail::unwind_type_id_((boost::type<T>*) 0, (mpl::bool_<boost::python::detail::is_void<T>::value >*)0 )
  66. );
  67. return r ? r->m_class_object: 0;
  68. }
  69. };
  70. template <class T>
  71. struct registered_pytype_direct
  72. {
  73. static PyTypeObject const* get_pytype()
  74. {
  75. return registered<T>::converters.m_class_object;
  76. }
  77. };
  78. template <class T>
  79. struct expected_from_python_type : expected_pytype_for_arg<T>{};
  80. template <class T>
  81. struct expected_from_python_type_direct
  82. {
  83. static PyTypeObject const* get_pytype()
  84. {
  85. return registered<T>::converters.expected_from_python_type();
  86. }
  87. };
  88. template <class T>
  89. struct to_python_target_type
  90. {
  91. static PyTypeObject const *get_pytype()
  92. {
  93. const converter::registration *r=converter::registry::query(
  94. detail::unwind_type_id_((boost::type<T>*)0, (mpl::bool_<boost::python::detail::is_void<T>::value >*)0 )
  95. );
  96. return r ? r->to_python_target_type(): 0;
  97. }
  98. };
  99. template <class T>
  100. struct to_python_target_type_direct
  101. {
  102. static PyTypeObject const *get_pytype()
  103. {
  104. return registered<T>::converters.to_python_target_type();
  105. }
  106. };
  107. #endif
  108. }}} // namespace boost::python
  109. #endif // WRAP_PYTYPE_NM20070606_HPP