default_call_policies.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 DEFAULT_CALL_POLICIES_DWA2002131_HPP
  6. # define DEFAULT_CALL_POLICIES_DWA2002131_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/mpl/if.hpp>
  9. # include <boost/python/to_python_value.hpp>
  10. # include <boost/python/detail/value_arg.hpp>
  11. # include <boost/type_traits/transform_traits.hpp>
  12. # include <boost/type_traits/is_pointer.hpp>
  13. # include <boost/type_traits/is_reference.hpp>
  14. # include <boost/mpl/or.hpp>
  15. # include <boost/mpl/front.hpp>
  16. namespace boost { namespace python {
  17. template <class T> struct to_python_value;
  18. namespace detail
  19. {
  20. // for "readable" error messages
  21. template <class T> struct specify_a_return_value_policy_to_wrap_functions_returning
  22. # if defined(__GNUC__) || defined(__EDG__)
  23. {}
  24. # endif
  25. ;
  26. }
  27. struct default_result_converter;
  28. struct default_call_policies
  29. {
  30. // Ownership of this argument tuple will ultimately be adopted by
  31. // the caller.
  32. template <class ArgumentPackage>
  33. static bool precall(ArgumentPackage const&)
  34. {
  35. return true;
  36. }
  37. // Pass the result through
  38. template <class ArgumentPackage>
  39. static PyObject* postcall(ArgumentPackage const&, PyObject* result)
  40. {
  41. return result;
  42. }
  43. typedef default_result_converter result_converter;
  44. typedef PyObject* argument_package;
  45. template <class Sig>
  46. struct extract_return_type : mpl::front<Sig>
  47. {
  48. };
  49. };
  50. struct default_result_converter
  51. {
  52. template <class R>
  53. struct apply
  54. {
  55. typedef typename mpl::if_<
  56. mpl::or_<is_pointer<R>, is_reference<R> >
  57. , detail::specify_a_return_value_policy_to_wrap_functions_returning<R>
  58. , boost::python::to_python_value<
  59. typename detail::value_arg<R>::type
  60. >
  61. >::type type;
  62. };
  63. };
  64. // Exceptions for c strings an PyObject*s
  65. template <>
  66. struct default_result_converter::apply<char const*>
  67. {
  68. typedef boost::python::to_python_value<char const*const&> type;
  69. };
  70. template <>
  71. struct default_result_converter::apply<PyObject*>
  72. {
  73. typedef boost::python::to_python_value<PyObject*const&> type;
  74. };
  75. }} // namespace boost::python
  76. #endif // DEFAULT_CALL_POLICIES_DWA2002131_HPP