default_call_policies.hpp 2.2 KB

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