make_function.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright David Abrahams 2001.
  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 MAKE_FUNCTION_DWA20011221_HPP
  6. # define MAKE_FUNCTION_DWA20011221_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/default_call_policies.hpp>
  9. # include <boost/python/args.hpp>
  10. # include <boost/python/detail/caller.hpp>
  11. # include <boost/python/object/function_object.hpp>
  12. # include <boost/mpl/size.hpp>
  13. # include <boost/mpl/int.hpp>
  14. namespace boost { namespace python {
  15. namespace detail
  16. {
  17. // make_function_aux --
  18. //
  19. // These helper functions for make_function (below) do the raw work
  20. // of constructing a Python object from some invokable entity. See
  21. // <boost/python/detail/caller.hpp> for more information about how
  22. // the Sig arguments is used.
  23. template <class F, class CallPolicies, class Sig>
  24. object make_function_aux(
  25. F f // An object that can be invoked by detail::invoke()
  26. , CallPolicies const& p // CallPolicies to use in the invocation
  27. , Sig const& // An MPL sequence of argument types expected by F
  28. )
  29. {
  30. return objects::function_object(
  31. detail::caller<F,CallPolicies,Sig>(f, p)
  32. );
  33. }
  34. // As above, except that it accepts argument keywords. NumKeywords
  35. // is used only for a compile-time assertion to make sure the user
  36. // doesn't pass more keywords than the function can accept. To
  37. // disable all checking, pass mpl::int_<0> for NumKeywords.
  38. template <class F, class CallPolicies, class Sig, class NumKeywords>
  39. object make_function_aux(
  40. F f
  41. , CallPolicies const& p
  42. , Sig const&
  43. , detail::keyword_range const& kw // a [begin,end) pair of iterators over keyword names
  44. , NumKeywords // An MPL integral type wrapper: the size of kw
  45. )
  46. {
  47. enum { arity = mpl::size<Sig>::value - 1 };
  48. typedef typename detail::error::more_keywords_than_function_arguments<
  49. NumKeywords::value, arity
  50. >::too_many_keywords assertion BOOST_ATTRIBUTE_UNUSED;
  51. return objects::function_object(
  52. detail::caller<F,CallPolicies,Sig>(f, p)
  53. , kw);
  54. }
  55. // Helpers for make_function when called with 3 arguments. These
  56. // dispatch functions are used to discriminate between the cases
  57. // when the 3rd argument is keywords or when it is a signature.
  58. //
  59. // @group {
  60. template <class F, class CallPolicies, class Keywords>
  61. object make_function_dispatch(F f, CallPolicies const& policies, Keywords const& kw, mpl::true_)
  62. {
  63. return detail::make_function_aux(
  64. f
  65. , policies
  66. , detail::get_signature(f)
  67. , kw.range()
  68. , mpl::int_<Keywords::size>()
  69. );
  70. }
  71. template <class F, class CallPolicies, class Signature>
  72. object make_function_dispatch(F f, CallPolicies const& policies, Signature const& sig, mpl::false_)
  73. {
  74. return detail::make_function_aux(
  75. f
  76. , policies
  77. , sig
  78. );
  79. }
  80. // }
  81. }
  82. // These overloaded functions wrap a function or member function
  83. // pointer as a Python object, using optional CallPolicies,
  84. // Keywords, and/or Signature.
  85. //
  86. // @group {
  87. template <class F>
  88. object make_function(F f)
  89. {
  90. return detail::make_function_aux(
  91. f,default_call_policies(), detail::get_signature(f));
  92. }
  93. template <class F, class CallPolicies>
  94. object make_function(F f, CallPolicies const& policies)
  95. {
  96. return detail::make_function_aux(
  97. f, policies, detail::get_signature(f));
  98. }
  99. template <class F, class CallPolicies, class KeywordsOrSignature>
  100. object make_function(
  101. F f
  102. , CallPolicies const& policies
  103. , KeywordsOrSignature const& keywords_or_signature)
  104. {
  105. typedef typename
  106. detail::is_reference_to_keywords<KeywordsOrSignature&>::type
  107. is_kw;
  108. return detail::make_function_dispatch(
  109. f
  110. , policies
  111. , keywords_or_signature
  112. , is_kw()
  113. );
  114. }
  115. template <class F, class CallPolicies, class Keywords, class Signature>
  116. object make_function(
  117. F f
  118. , CallPolicies const& policies
  119. , Keywords const& kw
  120. , Signature const& sig
  121. )
  122. {
  123. return detail::make_function_aux(
  124. f
  125. , policies
  126. , sig
  127. , kw.range()
  128. , mpl::int_<Keywords::size>()
  129. );
  130. }
  131. // }
  132. }}
  133. #endif // MAKE_FUNCTION_DWA20011221_HPP