Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:40

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef DEF_DWA200292_HPP
0006 # define DEF_DWA200292_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 
0010 # include <boost/python/object_fwd.hpp>
0011 # include <boost/python/make_function.hpp>
0012 # include <boost/python/detail/def_helper.hpp>
0013 # include <boost/python/detail/overloads_fwd.hpp>
0014 # include <boost/python/scope.hpp>
0015 # include <boost/python/signature.hpp>
0016 # include <boost/python/detail/scope.hpp>
0017 
0018 namespace boost { namespace python {
0019 
0020 namespace detail
0021 {
0022   namespace error
0023   {
0024     // Compile-time error messages
0025     template <bool> struct multiple_functions_passed_to_def;
0026     template <> struct multiple_functions_passed_to_def<false> { typedef char type; };
0027   }
0028   
0029   //
0030   // def_from_helper --
0031   //
0032   // Use a def_helper to define a regular wrapped function in the current scope.
0033   template <class F, class Helper>
0034   void def_from_helper(
0035       char const* name, F const& fn, Helper const& helper)
0036   {
0037       // Must not try to use default implementations except with method definitions.
0038       typedef typename error::multiple_functions_passed_to_def<
0039           Helper::has_default_implementation
0040           >::type assertion BOOST_ATTRIBUTE_UNUSED;
0041       
0042       detail::scope_setattr_doc(
0043           name, boost::python::make_function(
0044               fn
0045               , helper.policies()
0046               , helper.keywords())
0047           , helper.doc()
0048           );
0049   }
0050 
0051   //
0052   // These two overloads discriminate between def() as applied to
0053   // regular functions and def() as applied to the result of
0054   // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
0055   // discriminate.
0056   //
0057   template <class Fn, class A1>
0058   void
0059   def_maybe_overloads(
0060       char const* name
0061       , Fn fn
0062       , A1 const& a1
0063       , ...)
0064   {
0065       detail::def_from_helper(name, fn, def_helper<A1>(a1));
0066   }
0067 
0068   template <class StubsT, class SigT>
0069   void def_maybe_overloads(
0070       char const* name
0071       , SigT sig
0072       , StubsT const& stubs
0073       , detail::overloads_base const*)
0074   {
0075       scope current;
0076       
0077       detail::define_with_defaults(
0078           name, stubs, current, detail::get_signature(sig));
0079   }
0080 
0081   template <class T>
0082   object make_function1(T fn, ...) { return make_function(fn); }
0083 
0084   inline
0085   object make_function1(object const& x, object const*) { return x; }
0086 }
0087 
0088 template <class Fn>
0089 void def(char const* name, Fn fn)
0090 {
0091     detail::scope_setattr_doc(name, detail::make_function1(fn, &fn), 0);
0092 }
0093 
0094 template <class Arg1T, class Arg2T>
0095 void def(char const* name, Arg1T arg1, Arg2T const& arg2)
0096 {
0097     detail::def_maybe_overloads(name, arg1, arg2, &arg2);
0098 }
0099 
0100 template <class F, class A1, class A2>
0101 void def(char const* name, F f, A1 const& a1, A2 const& a2)
0102 {
0103     detail::def_from_helper(name, f, detail::def_helper<A1,A2>(a1,a2));
0104 }
0105 
0106 template <class F, class A1, class A2, class A3>
0107 void def(char const* name, F f, A1 const& a1, A2 const& a2, A3 const& a3)
0108 {
0109     detail::def_from_helper(name, f, detail::def_helper<A1,A2,A3>(a1,a2,a3));
0110 }
0111 
0112 }} // namespace boost::python
0113 
0114 #endif // DEF_DWA200292_HPP