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