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