Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / fusion / functional / invocation / invoke_procedure.hpp
1 /*=============================================================================
2     Copyright (c) 2005-2006 Joao Abecasis
3     Copyright (c) 2006-2007 Tobias Schwinger
4
5     Use modification and distribution are subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt).
8 ==============================================================================*/
9
10 #if !defined(BOOST_FUSION_FUNCTIONAL_INVOCATION_INVOKE_PROCEDURE_HPP_INCLUDED)
11 #if !defined(BOOST_PP_IS_ITERATING)
12
13 #include <boost/preprocessor/cat.hpp>
14 #include <boost/preprocessor/iteration/iterate.hpp>
15 #include <boost/preprocessor/arithmetic/dec.hpp>
16 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
17 #include <boost/preprocessor/repetition/enum.hpp>
18 #include <boost/preprocessor/repetition/enum_shifted.hpp>
19 #include <boost/preprocessor/repetition/enum_params.hpp>
20 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
21
22 #include <boost/type_traits/remove_reference.hpp>
23
24 #include <boost/mpl/front.hpp>
25
26 #include <boost/function_types/is_callable_builtin.hpp>
27 #include <boost/function_types/is_member_function_pointer.hpp>
28 #include <boost/function_types/parameter_types.hpp>
29
30 #include <boost/fusion/support/category_of.hpp>
31 #include <boost/fusion/sequence/intrinsic/at.hpp>
32 #include <boost/fusion/sequence/intrinsic/size.hpp>
33 #include <boost/fusion/sequence/intrinsic/begin.hpp>
34 #include <boost/fusion/iterator/next.hpp>
35 #include <boost/fusion/iterator/deref.hpp>
36 #include <boost/fusion/functional/invocation/limits.hpp>
37 #include <boost/fusion/functional/invocation/detail/that_ptr.hpp>
38
39 namespace boost { namespace fusion
40 {
41     namespace result_of
42     {
43         template <typename Function, class Sequence> struct invoke_procedure
44         {
45             typedef void type;
46         };
47     }
48
49     template <typename Function, class Sequence>
50     BOOST_FUSION_GPU_ENABLED
51     inline void invoke_procedure(Function, Sequence &);
52
53     template <typename Function, class Sequence>
54     BOOST_FUSION_GPU_ENABLED
55     inline void invoke_procedure(Function, Sequence const &);
56
57     //----- ---- --- -- - -  -   -
58
59     namespace detail
60     {
61         namespace ft = function_types;
62
63         template<
64             typename Function, class Sequence,
65             int N = result_of::size<Sequence>::value,
66             bool MFP = ft::is_member_function_pointer<Function>::value,
67             bool RandomAccess = traits::is_random_access<Sequence>::value
68             >
69         struct invoke_procedure_impl;
70
71         #define  BOOST_PP_FILENAME_1 \
72             <boost/fusion/functional/invocation/invoke_procedure.hpp>
73         #define  BOOST_PP_ITERATION_LIMITS \
74             (0, BOOST_FUSION_INVOKE_PROCEDURE_MAX_ARITY)
75         #include BOOST_PP_ITERATE()
76
77     }
78
79     template <typename Function, class Sequence>
80     BOOST_FUSION_GPU_ENABLED
81     inline void invoke_procedure(Function f, Sequence & s)
82     {
83         detail::invoke_procedure_impl<
84                 typename boost::remove_reference<Function>::type,Sequence
85             >::call(f,s);
86     }
87
88     template <typename Function, class Sequence>
89     BOOST_FUSION_GPU_ENABLED
90     inline void invoke_procedure(Function f, Sequence const & s)
91     {
92         detail::invoke_procedure_impl<
93                 typename boost::remove_reference<Function>::type,Sequence const
94             >::call(f,s);
95     }
96
97 }}
98
99 #define BOOST_FUSION_FUNCTIONAL_INVOCATION_INVOKE_PROCEDURE_HPP_INCLUDED
100 #else // defined(BOOST_PP_IS_ITERATING)
101 ///////////////////////////////////////////////////////////////////////////////
102 //
103 //  Preprocessor vertical repetition code
104 //
105 ///////////////////////////////////////////////////////////////////////////////
106 #define N BOOST_PP_ITERATION()
107
108 #define M(z,j,data) fusion::at_c<j>(s)
109
110         template <typename Function, class Sequence>
111         struct invoke_procedure_impl<Function,Sequence,N,false,true>
112         {
113
114 #if N > 0
115
116             BOOST_FUSION_GPU_ENABLED
117             static inline void call(Function & f, Sequence & s)
118             {
119                 f(BOOST_PP_ENUM(N,M,~));
120             }
121
122 #else
123
124             BOOST_FUSION_GPU_ENABLED
125             static inline void call(Function & f, Sequence & /*s*/)
126             {
127                 f();
128             }
129
130 #endif
131
132         };
133
134 #if N > 0
135         template <typename Function, class Sequence>
136         struct invoke_procedure_impl<Function,Sequence,N,true,true>
137         {
138             BOOST_FUSION_GPU_ENABLED
139             static inline void call(Function & f, Sequence & s)
140             {
141                 (that_ptr<typename mpl::front<
142                                 ft::parameter_types<Function> >::type
143                     >::get(fusion::at_c<0>(s))->*f)(BOOST_PP_ENUM_SHIFTED(N,M,~));
144             }
145         };
146 #endif
147
148 #undef M
149
150 #define M(z,j,data)                                                             \
151             typedef typename result_of::next< BOOST_PP_CAT(I,BOOST_PP_DEC(j))  \
152                 >::type I ## j ;                                               \
153             I##j i##j = fusion::next(BOOST_PP_CAT(i,BOOST_PP_DEC(j)));
154
155         template <typename Function, class Sequence>
156         struct invoke_procedure_impl<Function,Sequence,N,false,false>
157         {
158
159 #if N > 0
160
161             BOOST_FUSION_GPU_ENABLED
162             static inline void call(Function & f, Sequence & s)
163             {
164                 typedef typename result_of::begin<Sequence>::type I0;
165                 I0 i0 = fusion::begin(s);
166                 BOOST_PP_REPEAT_FROM_TO(1,N,M,~)
167                 f( BOOST_PP_ENUM_PARAMS(N,*i) );
168             }
169
170 #else
171             BOOST_FUSION_GPU_ENABLED
172             static inline void call(Function & f, Sequence & /*s*/)
173             {
174                 f();
175             }
176
177 #endif
178
179         };
180
181 #if N > 0
182         template <typename Function, class Sequence>
183         struct invoke_procedure_impl<Function,Sequence,N,true,false>
184         {
185             BOOST_FUSION_GPU_ENABLED
186             static inline void call(Function & f, Sequence & s)
187             {
188                 typedef typename result_of::begin<Sequence>::type I0;
189                 I0 i0 = fusion::begin(s);
190                 BOOST_PP_REPEAT_FROM_TO(1,N,M,~)
191
192                 (that_ptr<typename mpl::front<
193                                 ft::parameter_types<Function> >::type
194                     >::get(*i0)->*f)(BOOST_PP_ENUM_SHIFTED_PARAMS(N,*i));
195             }
196         };
197 #endif
198
199 #undef M
200
201 #undef N
202 #endif // defined(BOOST_PP_IS_ITERATING)
203 #endif
204