def.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 DEF_DWA200292_HPP
  6. # define DEF_DWA200292_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/object_fwd.hpp>
  9. # include <boost/python/make_function.hpp>
  10. # include <boost/python/detail/def_helper.hpp>
  11. # include <boost/python/detail/overloads_fwd.hpp>
  12. # include <boost/python/scope.hpp>
  13. # include <boost/python/signature.hpp>
  14. # include <boost/python/detail/scope.hpp>
  15. namespace boost { namespace python {
  16. namespace detail
  17. {
  18. namespace error
  19. {
  20. // Compile-time error messages
  21. template <bool> struct multiple_functions_passed_to_def;
  22. template <> struct multiple_functions_passed_to_def<false> { typedef char type; };
  23. }
  24. //
  25. // def_from_helper --
  26. //
  27. // Use a def_helper to define a regular wrapped function in the current scope.
  28. template <class F, class Helper>
  29. void def_from_helper(
  30. char const* name, F const& fn, Helper const& helper)
  31. {
  32. // Must not try to use default implementations except with method definitions.
  33. typedef typename error::multiple_functions_passed_to_def<
  34. Helper::has_default_implementation
  35. >::type assertion BOOST_ATTRIBUTE_UNUSED;
  36. detail::scope_setattr_doc(
  37. name, boost::python::make_function(
  38. fn
  39. , helper.policies()
  40. , helper.keywords())
  41. , helper.doc()
  42. );
  43. }
  44. //
  45. // These two overloads discriminate between def() as applied to
  46. // regular functions and def() as applied to the result of
  47. // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
  48. // discriminate.
  49. //
  50. template <class Fn, class A1>
  51. void
  52. def_maybe_overloads(
  53. char const* name
  54. , Fn fn
  55. , A1 const& a1
  56. , ...)
  57. {
  58. detail::def_from_helper(name, fn, def_helper<A1>(a1));
  59. }
  60. template <class StubsT, class SigT>
  61. void def_maybe_overloads(
  62. char const* name
  63. , SigT sig
  64. , StubsT const& stubs
  65. , detail::overloads_base const*)
  66. {
  67. scope current;
  68. detail::define_with_defaults(
  69. name, stubs, current, detail::get_signature(sig));
  70. }
  71. template <class T>
  72. object make_function1(T fn, ...) { return make_function(fn); }
  73. inline
  74. object make_function1(object const& x, object const*) { return x; }
  75. }
  76. template <class Fn>
  77. void def(char const* name, Fn fn)
  78. {
  79. detail::scope_setattr_doc(name, detail::make_function1(fn, &fn), 0);
  80. }
  81. template <class Arg1T, class Arg2T>
  82. void def(char const* name, Arg1T arg1, Arg2T const& arg2)
  83. {
  84. detail::def_maybe_overloads(name, arg1, arg2, &arg2);
  85. }
  86. template <class F, class A1, class A2>
  87. void def(char const* name, F f, A1 const& a1, A2 const& a2)
  88. {
  89. detail::def_from_helper(name, f, detail::def_helper<A1,A2>(a1,a2));
  90. }
  91. template <class F, class A1, class A2, class A3>
  92. void def(char const* name, F f, A1 const& a1, A2 const& a2, A3 const& a3)
  93. {
  94. detail::def_from_helper(name, f, detail::def_helper<A1,A2,A3>(a1,a2,a3));
  95. }
  96. }} // namespace boost::python
  97. #endif // DEF_DWA200292_HPP