1 #if !defined(BOOST_PP_IS_ITERATING)
2 
3 // Copyright David Abrahams 2002.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
8 # ifndef CALLER_DWA20021121_HPP
9 #  define CALLER_DWA20021121_HPP
10 
11 #  include <boost/python/type_id.hpp>
12 #  include <boost/python/handle.hpp>
13 
14 #  include <boost/detail/indirect_traits.hpp>
15 
16 #  include <boost/python/detail/invoke.hpp>
17 #  include <boost/python/detail/signature.hpp>
18 #  include <boost/python/detail/preprocessor.hpp>
19 #  include <boost/python/detail/type_traits.hpp>
20 
21 #  include <boost/python/arg_from_python.hpp>
22 #  include <boost/python/converter/context_result_converter.hpp>
23 #  include <boost/python/converter/builtin_converters.hpp>
24 
25 #  include <boost/preprocessor/iterate.hpp>
26 #  include <boost/preprocessor/cat.hpp>
27 #  include <boost/preprocessor/dec.hpp>
28 #  include <boost/preprocessor/if.hpp>
29 #  include <boost/preprocessor/iteration/local.hpp>
30 #  include <boost/preprocessor/repetition/enum_trailing_params.hpp>
31 #  include <boost/preprocessor/repetition/repeat.hpp>
32 
33 #  include <boost/compressed_pair.hpp>
34 
35 #  include <boost/mpl/apply.hpp>
36 #  include <boost/mpl/eval_if.hpp>
37 #  include <boost/mpl/identity.hpp>
38 #  include <boost/mpl/size.hpp>
39 #  include <boost/mpl/at.hpp>
40 #  include <boost/mpl/int.hpp>
41 #  include <boost/mpl/next.hpp>
42 
43 namespace boost { namespace python { namespace detail {
44 
45 template <int N>
get(mpl::int_<N>,PyObject * const & args_)46 inline PyObject* get(mpl::int_<N>, PyObject* const& args_)
47 {
48     return PyTuple_GET_ITEM(args_,N);
49 }
50 
arity(PyObject * const & args_)51 inline Py_ssize_t arity(PyObject* const& args_)
52 {
53     return PyTuple_GET_SIZE(args_);
54 }
55 
56 // This "result converter" is really just used as
57 // a dispatch tag to invoke(...), selecting the appropriate
58 // implementation
59 typedef int void_result_to_python;
60 
61 // Given a model of CallPolicies and a C++ result type, this
62 // metafunction selects the appropriate converter to use for
63 // converting the result to python.
64 template <class Policies, class Result>
65 struct select_result_converter
66   : mpl::eval_if<
67         is_same<Result,void>
68       , mpl::identity<void_result_to_python>
69       , mpl::apply1<typename Policies::result_converter,Result>
70     >
71 {
72 };
73 
74 template <class ArgPackage, class ResultConverter>
create_result_converter(ArgPackage const & args_,ResultConverter *,converter::context_result_converter *)75 inline ResultConverter create_result_converter(
76     ArgPackage const& args_
77   , ResultConverter*
78   , converter::context_result_converter*
79 )
80 {
81     return ResultConverter(args_);
82 }
83 
84 template <class ArgPackage, class ResultConverter>
create_result_converter(ArgPackage const &,ResultConverter *,...)85 inline ResultConverter create_result_converter(
86     ArgPackage const&
87   , ResultConverter*
88   , ...
89 )
90 {
91     return ResultConverter();
92 }
93 
94 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
95 template <class ResultConverter>
96 struct converter_target_type
97 {
get_pytypeboost::python::detail::converter_target_type98     static PyTypeObject const *get_pytype()
99     {
100         return create_result_converter((PyObject*)0, (ResultConverter *)0, (ResultConverter *)0).get_pytype();
101     }
102 };
103 
104 template < >
105 struct converter_target_type <void_result_to_python >
106 {
get_pytypeboost::python::detail::converter_target_type107     static PyTypeObject const *get_pytype()
108     {
109         return 0;
110     }
111 };
112 #endif
113 
114 
115 template <unsigned> struct caller_arity;
116 
117 template <class F, class CallPolicies, class Sig>
118 struct caller;
119 
120 #  define BOOST_PYTHON_NEXT(init,name,n)                                                        \
121     typedef BOOST_PP_IF(n,typename mpl::next< BOOST_PP_CAT(name,BOOST_PP_DEC(n)) >::type, init) name##n;
122 
123 #  define BOOST_PYTHON_ARG_CONVERTER(n)                                         \
124      BOOST_PYTHON_NEXT(typename mpl::next<first>::type, arg_iter,n)             \
125      typedef arg_from_python<BOOST_DEDUCED_TYPENAME arg_iter##n::type> c_t##n;  \
126      c_t##n c##n(get(mpl::int_<n>(), inner_args));                              \
127      if (!c##n.convertible())                                                   \
128           return 0;
129 
130 #  define BOOST_PP_ITERATION_PARAMS_1                                            \
131         (3, (0, BOOST_PYTHON_MAX_ARITY + 1, <boost/python/detail/caller.hpp>))
132 #  include BOOST_PP_ITERATE()
133 
134 #  undef BOOST_PYTHON_ARG_CONVERTER
135 #  undef BOOST_PYTHON_NEXT
136 
137 // A metafunction returning the base class used for caller<class F,
138 // class ConverterGenerators, class CallPolicies, class Sig>.
139 template <class F, class CallPolicies, class Sig>
140 struct caller_base_select
141 {
142     enum { arity = mpl::size<Sig>::value - 1 };
143     typedef typename caller_arity<arity>::template impl<F,CallPolicies,Sig> type;
144 };
145 
146 // A function object type which wraps C++ objects as Python callable
147 // objects.
148 //
149 // Template Arguments:
150 //
151 //   F -
152 //      the C++ `function object' that will be called. Might
153 //      actually be any data for which an appropriate invoke_tag() can
154 //      be generated. invoke(...) takes care of the actual invocation syntax.
155 //
156 //   CallPolicies -
157 //      The precall, postcall, and what kind of resultconverter to
158 //      generate for mpl::front<Sig>::type
159 //
160 //   Sig -
161 //      The `intended signature' of the function. An MPL sequence
162 //      beginning with a result type and continuing with a list of
163 //      argument types.
164 template <class F, class CallPolicies, class Sig>
165 struct caller
166     : caller_base_select<F,CallPolicies,Sig>::type
167 {
168     typedef typename caller_base_select<
169         F,CallPolicies,Sig
170         >::type base;
171 
172     typedef PyObject* result_type;
173 
callerboost::python::detail::caller174     caller(F f, CallPolicies p) : base(f,p) {}
175 
176 };
177 
178 }}} // namespace boost::python::detail
179 
180 # endif // CALLER_DWA20021121_HPP
181 
182 #else
183 
184 # define N BOOST_PP_ITERATION()
185 
186 template <>
187 struct caller_arity<N>
188 {
189     template <class F, class Policies, class Sig>
190     struct impl
191     {
implcaller_arity::impl192         impl(F f, Policies p) : m_data(f,p) {}
193 
operator ()caller_arity::impl194         PyObject* operator()(PyObject* args_, PyObject*) // eliminate
195                                                          // this
196                                                          // trailing
197                                                          // keyword dict
198         {
199             typedef typename mpl::begin<Sig>::type first;
200             typedef typename first::type result_t;
201             typedef typename select_result_converter<Policies, result_t>::type result_converter;
202             typedef typename Policies::argument_package argument_package;
203 
204             argument_package inner_args(args_);
205 
206 # if N
207 #  define BOOST_PP_LOCAL_MACRO(i) BOOST_PYTHON_ARG_CONVERTER(i)
208 #  define BOOST_PP_LOCAL_LIMITS (0, N-1)
209 #  include BOOST_PP_LOCAL_ITERATE()
210 # endif
211             // all converters have been checked. Now we can do the
212             // precall part of the policy
213             if (!m_data.second().precall(inner_args))
214                 return 0;
215 
216             PyObject* result = detail::invoke(
217                 detail::invoke_tag<result_t,F>()
218               , create_result_converter(args_, (result_converter*)0, (result_converter*)0)
219               , m_data.first()
220                 BOOST_PP_ENUM_TRAILING_PARAMS(N, c)
221             );
222 
223             return m_data.second().postcall(inner_args, result);
224         }
225 
min_aritycaller_arity::impl226         static unsigned min_arity() { return N; }
227 
signaturecaller_arity::impl228         static py_func_sig_info  signature()
229         {
230             const signature_element * sig = detail::signature<Sig>::elements();
231 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
232 
233             typedef BOOST_DEDUCED_TYPENAME Policies::template extract_return_type<Sig>::type rtype;
234             typedef typename select_result_converter<Policies, rtype>::type result_converter;
235 
236             static const signature_element ret = {
237                 (is_void<rtype>::value ? "void" : type_id<rtype>().name())
238                 , &detail::converter_target_type<result_converter>::get_pytype
239                 , boost::detail::indirect_traits::is_reference_to_non_const<rtype>::value
240             };
241             py_func_sig_info res = {sig, &ret };
242 #else
243             py_func_sig_info res = {sig, sig };
244 #endif
245 
246             return  res;
247         }
248      private:
249         compressed_pair<F,Policies> m_data;
250     };
251 };
252 
253 
254 
255 #endif // BOOST_PP_IS_ITERATING
256 
257 
258