invoke_procedure.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/mpl/front.hpp>
  20. #include <boost/function_types/is_callable_builtin.hpp>
  21. #include <boost/function_types/is_member_function_pointer.hpp>
  22. #include <boost/function_types/parameter_types.hpp>
  23. #include <boost/fusion/support/category_of.hpp>
  24. #include <boost/fusion/sequence/intrinsic/at.hpp>
  25. #include <boost/fusion/sequence/intrinsic/size.hpp>
  26. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  27. #include <boost/fusion/iterator/next.hpp>
  28. #include <boost/fusion/iterator/deref.hpp>
  29. #include <boost/fusion/functional/invocation/limits.hpp>
  30. #include <boost/fusion/functional/invocation/detail/that_ptr.hpp>
  31. namespace boost { namespace fusion
  32. {
  33. namespace result_of
  34. {
  35. template <typename Function, class Sequence> struct invoke_procedure
  36. {
  37. typedef void type;
  38. };
  39. }
  40. template <typename Function, class Sequence>
  41. inline void invoke_procedure(Function, Sequence &);
  42. template <typename Function, class Sequence>
  43. inline void invoke_procedure(Function, Sequence const &);
  44. //----- ---- --- -- - - - -
  45. namespace detail
  46. {
  47. namespace ft = function_types;
  48. template<
  49. typename Function, class Sequence,
  50. int N = result_of::size<Sequence>::value,
  51. bool MFP = ft::is_member_function_pointer<Function>::value,
  52. bool RandomAccess = traits::is_random_access<Sequence>::value
  53. >
  54. struct invoke_procedure_impl;
  55. #define BOOST_PP_FILENAME_1 \
  56. <boost/fusion/functional/invocation/invoke_procedure.hpp>
  57. #define BOOST_PP_ITERATION_LIMITS \
  58. (0, BOOST_FUSION_INVOKE_PROCEDURE_MAX_ARITY)
  59. #include BOOST_PP_ITERATE()
  60. }
  61. template <typename Function, class Sequence>
  62. inline void invoke_procedure(Function f, Sequence & s)
  63. {
  64. detail::invoke_procedure_impl<
  65. typename boost::remove_reference<Function>::type,Sequence
  66. >::call(f,s);
  67. }
  68. template <typename Function, class Sequence>
  69. inline void invoke_procedure(Function f, Sequence const & s)
  70. {
  71. detail::invoke_procedure_impl<
  72. typename boost::remove_reference<Function>::type,Sequence const
  73. >::call(f,s);
  74. }
  75. }}
  76. #define BOOST_FUSION_FUNCTIONAL_INVOCATION_INVOKE_PROCEDURE_HPP_INCLUDED
  77. #else // defined(BOOST_PP_IS_ITERATING)
  78. ///////////////////////////////////////////////////////////////////////////////
  79. //
  80. // Preprocessor vertical repetition code
  81. //
  82. ///////////////////////////////////////////////////////////////////////////////
  83. #define N BOOST_PP_ITERATION()
  84. #define M(z,j,data) fusion::at_c<j>(s)
  85. template <typename Function, class Sequence>
  86. struct invoke_procedure_impl<Function,Sequence,N,false,true>
  87. {
  88. #if N > 0
  89. static inline void call(Function & f, Sequence & s)
  90. {
  91. f(BOOST_PP_ENUM(N,M,~));
  92. }
  93. #else
  94. static inline void call(Function & f, Sequence & /*s*/)
  95. {
  96. f();
  97. }
  98. #endif
  99. };
  100. #if N > 0
  101. template <typename Function, class Sequence>
  102. struct invoke_procedure_impl<Function,Sequence,N,true,true>
  103. {
  104. static inline void call(Function & f, Sequence & s)
  105. {
  106. (that_ptr<typename mpl::front<
  107. ft::parameter_types<Function> >::type
  108. >::get(fusion::at_c<0>(s))->*f)(BOOST_PP_ENUM_SHIFTED(N,M,~));
  109. }
  110. };
  111. #endif
  112. #undef M
  113. #define M(z,j,data) \
  114. typedef typename result_of::next< BOOST_PP_CAT(I,BOOST_PP_DEC(j)) \
  115. >::type I ## j ; \
  116. I##j i##j = fusion::next(BOOST_PP_CAT(i,BOOST_PP_DEC(j)));
  117. template <typename Function, class Sequence>
  118. struct invoke_procedure_impl<Function,Sequence,N,false,false>
  119. {
  120. #if N > 0
  121. static inline void call(Function & f, Sequence & s)
  122. {
  123. typedef typename result_of::begin<Sequence>::type I0;
  124. I0 i0 = fusion::begin(s);
  125. BOOST_PP_REPEAT_FROM_TO(1,N,M,~)
  126. f( BOOST_PP_ENUM_PARAMS(N,*i) );
  127. }
  128. #else
  129. static inline void call(Function & f, Sequence & /*s*/)
  130. {
  131. f();
  132. }
  133. #endif
  134. };
  135. #if N > 0
  136. template <typename Function, class Sequence>
  137. struct invoke_procedure_impl<Function,Sequence,N,true,false>
  138. {
  139. static inline void call(Function & f, Sequence & s)
  140. {
  141. typedef typename result_of::begin<Sequence>::type I0;
  142. I0 i0 = fusion::begin(s);
  143. BOOST_PP_REPEAT_FROM_TO(1,N,M,~)
  144. (that_ptr<typename mpl::front<
  145. ft::parameter_types<Function> >::type
  146. >::get(*i0)->*f)(BOOST_PP_ENUM_SHIFTED_PARAMS(N,*i));
  147. }
  148. };
  149. #endif
  150. #undef M
  151. #undef N
  152. #endif // defined(BOOST_PP_IS_ITERATING)
  153. #endif