source: NonGTP/Boost/boost/python/def.hpp @ 857

Revision 857, 3.2 KB checked in by igarcia, 18 years ago (diff)
Line 
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
18namespace boost { namespace python {
19
20namespace 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>
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;
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
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>
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>
82  object make_function1(T fn, ...) { return make_function(fn); }
83
84  inline
85  object make_function1(object const& x, object const*) { return x; }
86}
87
88template <class Fn>
89void def(char const* name, Fn fn)
90{
91    detail::scope_setattr_doc(name, detail::make_function1(fn, &fn), 0);
92}
93
94template <class Arg1T, class Arg2T>
95void def(char const* name, Arg1T arg1, Arg2T const& arg2)
96{
97    detail::def_maybe_overloads(name, arg1, arg2, &arg2);
98}
99
100template <class F, class A1, class A2>
101void 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
106template <class F, class A1, class A2, class A3>
107void 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
Note: See TracBrowser for help on using the repository browser.