invoke_procedure.hpp 6.9 KB

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