1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright David Abrahams 2002, Joel de Guzman, 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 ///////////////////////////////////////////////////////////////////////////////
9 #if !defined(BOOST_PP_IS_ITERATING)
10 
11 # ifndef SIGNATURE_JDG20020813_HPP
12 #  define SIGNATURE_JDG20020813_HPP
13 
14 # include <boost/python/detail/prefix.hpp>
15 
16 # include <boost/mpl/if.hpp>
17 # include <boost/type_traits/is_convertible.hpp>
18 
19 #  include <boost/python/detail/preprocessor.hpp>
20 #  include <boost/preprocessor/repeat.hpp>
21 #  include <boost/preprocessor/enum.hpp>
22 #  include <boost/preprocessor/enum_params.hpp>
23 #  include <boost/preprocessor/empty.hpp>
24 #  include <boost/preprocessor/arithmetic/sub.hpp>
25 #  include <boost/preprocessor/iterate.hpp>
26 #  include <boost/python/detail/type_list.hpp>
27 
28 #  include <boost/preprocessor/debug/line.hpp>
29 #  include <boost/preprocessor/arithmetic/sub.hpp>
30 #  include <boost/preprocessor/arithmetic/inc.hpp>
31 #  include <boost/preprocessor/repetition/enum_trailing_params.hpp>
32 
33 # define BOOST_PYTHON_LIST_INC(n)        \
34    BOOST_PP_CAT(mpl::vector, BOOST_PP_INC(n))
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 namespace boost { namespace python { namespace detail {
38 
39 // A metafunction returning C1 if C1 is derived from C2, and C2
40 // otherwise
41 template <class C1, class C2>
42 struct most_derived
43 {
44     typedef typename mpl::if_<
45         is_convertible<C1*,C2*>
46       , C1
47       , C2
48     >::type type;
49 };
50 
51 //  The following macros generate expansions for::
52 //
53 //      template <class RT, class T0... class TN>
54 //      inline mpl::vector<RT, T0...TN>
55 //      get_signature(RT(BOOST_PYTHON_FN_CC *)(T0...TN), void* = 0)
56 //      {
57 //          return mpl::list<RT, T0...TN>();
58 //      }
59 //
60 //    where BOOST_PYTHON_FN_CC is a calling convention keyword, can be
61 //
62 //        empty, for default calling convention
63 //        __cdecl (if BOOST_PYTHON_ENABLE_CDECL is defined)
64 //        __stdcall (if BOOST_PYTHON_ENABLE_STDCALL is defined)
65 //        __fastcall (if BOOST_PYTHON_ENABLE_FASTCALL is defined)
66 //
67 //   And, for an appropriate assortment of cv-qualifications::
68 //
69 //      template <class RT, class ClassT, class T0... class TN>
70 //      inline mpl::vector<RT, ClassT&, T0...TN>
71 //      get_signature(RT(BOOST_PYTHON_FN_CC ClassT::*)(T0...TN) cv))
72 //      {
73 //          return mpl::list<RT, ClassT&, T0...TN>();
74 //      }
75 //
76 //      template <class Target, class RT, class ClassT, class T0... class TN>
77 //      inline mpl::vector<
78 //          RT
79 //        , typename most_derived<Target, ClassT>::type&
80 //        , T0...TN
81 //      >
82 //      get_signature(RT(BOOST_PYTHON_FN_CC ClassT::*)(T0...TN) cv), Target*)
83 //      {
84 //          return mpl::list<RT, ClassT&, T0...TN>();
85 //      }
86 //
87 //  There are two forms for invoking get_signature::
88 //
89 //      get_signature(f)
90 //
91 //  and ::
92 //
93 //      get_signature(f,(Target*)0)
94 //
95 //  These functions extract the return type, class (for member
96 //  functions) and arguments of the input signature and stuff them in
97 //  an mpl type sequence (the calling convention is dropped).
98 //  Note that cv-qualification is dropped from
99 //  the "hidden this" argument of member functions; that is a
100 //  necessary sacrifice to ensure that an lvalue from_python converter
101 //  is used.  A pointer is not used so that None will be rejected for
102 //  overload resolution.
103 //
104 //  The second form of get_signature essentially downcasts the "hidden
105 //  this" argument of member functions to Target, because the function
106 //  may actually be a member of a base class which is not wrapped, and
107 //  in that case conversion from python would fail.
108 //
109 // @group {
110 
111 // 'default' calling convention
112 
113 #  define BOOST_PYTHON_FN_CC
114 
115 #  define BOOST_PP_ITERATION_PARAMS_1                                   \
116     (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/signature.hpp>))
117 
118 #  include BOOST_PP_ITERATE()
119 
120 #  undef BOOST_PYTHON_FN_CC
121 
122 // __cdecl calling convention
123 
124 #  if defined(BOOST_PYTHON_ENABLE_CDECL)
125 
126 #   define BOOST_PYTHON_FN_CC __cdecl
127 #   define BOOST_PYTHON_FN_CC_IS_CDECL
128 
129 #   define BOOST_PP_ITERATION_PARAMS_1                                   \
130      (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/signature.hpp>))
131 
132 #   include BOOST_PP_ITERATE()
133 
134 #   undef BOOST_PYTHON_FN_CC
135 #   undef BOOST_PYTHON_FN_CC_IS_CDECL
136 
137 #  endif // defined(BOOST_PYTHON_ENABLE_CDECL)
138 
139 // __stdcall calling convention
140 
141 #  if defined(BOOST_PYTHON_ENABLE_STDCALL)
142 
143 #   define BOOST_PYTHON_FN_CC __stdcall
144 
145 #   define BOOST_PP_ITERATION_PARAMS_1                                   \
146      (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/signature.hpp>))
147 
148 #   include BOOST_PP_ITERATE()
149 
150 #   undef BOOST_PYTHON_FN_CC
151 
152 #  endif // defined(BOOST_PYTHON_ENABLE_STDCALL)
153 
154 // __fastcall calling convention
155 
156 #  if defined(BOOST_PYTHON_ENABLE_FASTCALL)
157 
158 #   define BOOST_PYTHON_FN_CC __fastcall
159 
160 #   define BOOST_PP_ITERATION_PARAMS_1                                   \
161      (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/signature.hpp>))
162 
163 #   include BOOST_PP_ITERATE()
164 
165 #   undef BOOST_PYTHON_FN_CC
166 
167 #  endif // defined(BOOST_PYTHON_ENABLE_FASTCALL)
168 
169 #  undef BOOST_PYTHON_LIST_INC
170 
171 // }
172 
173 }}} // namespace boost::python::detail
174 
175 
176 # endif // SIGNATURE_JDG20020813_HPP
177 
178 // For gcc 4.4 compatability, we must include the
179 // BOOST_PP_ITERATION_DEPTH test inside an #else clause.
180 #else // BOOST_PP_IS_ITERATING
181 #if BOOST_PP_ITERATION_DEPTH() == 1 // defined(BOOST_PP_IS_ITERATING)
182 
183 # define N BOOST_PP_ITERATION()
184 
185    // as 'get_signature(RT(*)(T0...TN), void* = 0)' is the same
186    // function as 'get_signature(RT(__cdecl *)(T0...TN), void* = 0)',
187    // we don't define it twice
188 #  if !defined(BOOST_PYTHON_FN_CC_IS_CDECL)
189 
190 template <
191     class RT BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class T)>
BOOST_PYTHON_LIST_INC(N)192 inline BOOST_PYTHON_LIST_INC(N)<
193     RT BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, T)>
194 get_signature(RT(BOOST_PYTHON_FN_CC *)(BOOST_PP_ENUM_PARAMS_Z(1, N, T)), void* = 0)
195 {
196     return BOOST_PYTHON_LIST_INC(N)<
197             RT BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, T)
198         >();
199 }
200 
201 #  endif // !defined(BOOST_PYTHON_FN_CC_IS_CDECL)
202 
203 # undef N
204 
205 # define BOOST_PP_ITERATION_PARAMS_2 \
206     (3, (0, 3, <boost/python/signature.hpp>))
207 # include BOOST_PP_ITERATE()
208 
209 #else
210 
211 # define N BOOST_PP_RELATIVE_ITERATION(1)
212 # define Q BOOST_PYTHON_CV_QUALIFIER(BOOST_PP_ITERATION())
213 
214 template <
215     class RT, class ClassT BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class T)>
BOOST_PYTHON_LIST_INC(BOOST_PP_INC (N))216 inline BOOST_PYTHON_LIST_INC(BOOST_PP_INC(N))<
217     RT, ClassT& BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, T)>
218 get_signature(RT(BOOST_PYTHON_FN_CC ClassT::*)(BOOST_PP_ENUM_PARAMS_Z(1, N, T)) Q)
219 {
220     return BOOST_PYTHON_LIST_INC(BOOST_PP_INC(N))<
221             RT, ClassT& BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, T)
222         >();
223 }
224 
225 template <
226     class Target
227   , class RT
228   , class ClassT
229     BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class T)
230 >
BOOST_PYTHON_LIST_INC(BOOST_PP_INC (N))231 inline BOOST_PYTHON_LIST_INC(BOOST_PP_INC(N))<
232     RT
233   , typename most_derived<Target, ClassT>::type&
234     BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, T)
235 >
236 get_signature(
237     RT(BOOST_PYTHON_FN_CC ClassT::*)(BOOST_PP_ENUM_PARAMS_Z(1, N, T)) Q
238   , Target*
239 )
240 {
241     return BOOST_PYTHON_LIST_INC(BOOST_PP_INC(N))<
242         RT
243       , BOOST_DEDUCED_TYPENAME most_derived<Target, ClassT>::type&
244         BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, T)
245     >();
246 }
247 
248 # undef Q
249 # undef N
250 
251 #endif // BOOST_PP_ITERATION_DEPTH()
252 #endif // !defined(BOOST_PP_IS_ITERATING)
253