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