to_python_converter.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. typedef boost::mpl::bool_<has_get_pytype> HasGetPytype;
  41. static PyTypeObject const* get_pytype_1(boost::mpl::true_ *)
  42. {
  43. return Conversion::get_pytype();
  44. }
  45. static PyTypeObject const* get_pytype_1(boost::mpl::false_ *)
  46. {
  47. return 0;
  48. }
  49. static PyTypeObject const* get_pytype_impl()
  50. {
  51. return get_pytype_1((HasGetPytype*)0);
  52. }
  53. #endif
  54. to_python_converter();
  55. };
  56. //
  57. // implementation
  58. //
  59. template <class T, class Conversion ,bool has_get_pytype>
  60. to_python_converter<T,Conversion, has_get_pytype>::to_python_converter()
  61. {
  62. typedef converter::as_to_python_function<
  63. T, Conversion
  64. > normalized;
  65. converter::registry::insert(
  66. &normalized::convert
  67. , type_id<T>()
  68. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  69. , &get_pytype_impl
  70. #endif
  71. );
  72. }
  73. }} // namespace boost::python
  74. #endif // TO_PYTHON_CONVERTER_DWA200221_HPP