to_python_converter.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright David Abrahams 2002.
  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 TO_PYTHON_CONVERTER_DWA200221_HPP
  6. # define TO_PYTHON_CONVERTER_DWA200221_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/converter/registry.hpp>
  9. # include <boost/python/converter/as_to_python_function.hpp>
  10. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  11. # include <boost/python/converter/pytype_function.hpp>
  12. #endif
  13. # include <boost/python/type_id.hpp>
  14. namespace boost { namespace python {
  15. #if 0 //get_pytype member detection
  16. namespace detail
  17. {
  18. typedef char yes_type;
  19. typedef struct {char a[2]; } no_type;
  20. template<PyTypeObject const * (*f)()> struct test_get_pytype1 { };
  21. template<PyTypeObject * (*f)()> struct test_get_pytype2 { };
  22. template<class T> yes_type tester(test_get_pytype1<&T::get_pytype>*);
  23. template<class T> yes_type tester(test_get_pytype2<&T::get_pytype>*);
  24. template<class T> no_type tester(...);
  25. template<class T>
  26. struct test_get_pytype_base
  27. {
  28. BOOST_STATIC_CONSTANT(bool, value= (sizeof(detail::tester<T>(0)) == sizeof(yes_type)));
  29. };
  30. template<class T>
  31. struct test_get_pytype : boost::mpl::bool_<test_get_pytype_base<T>::value>
  32. {
  33. };
  34. }
  35. #endif
  36. template < class T, class Conversion, bool has_get_pytype=false >
  37. struct to_python_converter
  38. {
  39. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  40. #if 0 //defined _MSC_VER && _MSC_VER >=1310
  41. //probably other compilers could come here as well
  42. typedef typename detail::test_get_pytype<Conversion> HasGetPytype;
  43. #else
  44. typedef boost::mpl::bool_<has_get_pytype> HasGetPytype;
  45. #endif
  46. static PyTypeObject const* get_pytype_1(boost::mpl::true_ *)
  47. {
  48. return Conversion::get_pytype();
  49. }
  50. static PyTypeObject const* get_pytype_1(boost::mpl::false_ *)
  51. {
  52. return 0;
  53. }
  54. static PyTypeObject const* get_pytype_impl()
  55. {
  56. return get_pytype_1((HasGetPytype*)0);
  57. }
  58. #endif
  59. to_python_converter();
  60. };
  61. //
  62. // implementation
  63. //
  64. template <class T, class Conversion ,bool has_get_pytype>
  65. to_python_converter<T,Conversion, has_get_pytype>::to_python_converter()
  66. {
  67. typedef converter::as_to_python_function<
  68. T, Conversion
  69. > normalized;
  70. converter::registry::insert(
  71. &normalized::convert
  72. , type_id<T>()
  73. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  74. , &get_pytype_impl
  75. #endif
  76. );
  77. }
  78. }} // namespace boost::python
  79. #endif // TO_PYTHON_CONVERTER_DWA200221_HPP