variadic_slot_invoker.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Helper class used by variadic implementation of variadic boost::signals2::signal.
  3. Author: Frank Mori Hess <fmhess@users.sourceforge.net>
  4. Begin: 2009-05-27
  5. */
  6. // Copyright Frank Mori Hess 2009
  7. // Use, modification and
  8. // distribution is subject to the Boost Software License, Version
  9. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. // For more information, see http://www.boost.org
  12. #ifndef BOOST_SIGNALS2_DETAIL_VARIADIC_SLOT_INVOKER_HPP
  13. #define BOOST_SIGNALS2_DETAIL_VARIADIC_SLOT_INVOKER_HPP
  14. #include <boost/mpl/size_t.hpp>
  15. #include <boost/signals2/detail/variadic_arg_type.hpp>
  16. // if compiler has std::tuple use it instead of boost::tuple
  17. // because boost::tuple does not have variadic template support at present.
  18. #ifdef BOOST_NO_CXX11_HDR_TUPLE
  19. #include <boost/tuple/tuple.hpp>
  20. #define BOOST_SIGNALS2_TUPLE boost::tuple
  21. #define BOOST_SIGNALS2_GET boost::get
  22. #else
  23. #include <tuple>
  24. #define BOOST_SIGNALS2_TUPLE std::tuple
  25. #define BOOST_SIGNALS2_GET std::get
  26. #endif
  27. namespace boost
  28. {
  29. namespace signals2
  30. {
  31. namespace detail
  32. {
  33. template<unsigned ... values> class unsigned_meta_array {};
  34. template<typename UnsignedMetaArray, unsigned n> class unsigned_meta_array_appender;
  35. template<unsigned n, unsigned ... Args>
  36. class unsigned_meta_array_appender<unsigned_meta_array<Args...>, n>
  37. {
  38. public:
  39. typedef unsigned_meta_array<Args..., n> type;
  40. };
  41. template<unsigned n> class make_unsigned_meta_array;
  42. template<> class make_unsigned_meta_array<0>
  43. {
  44. public:
  45. typedef unsigned_meta_array<> type;
  46. };
  47. template<> class make_unsigned_meta_array<1>
  48. {
  49. public:
  50. typedef unsigned_meta_array<0> type;
  51. };
  52. template<unsigned n> class make_unsigned_meta_array
  53. {
  54. public:
  55. typedef typename unsigned_meta_array_appender<typename make_unsigned_meta_array<n-1>::type, n - 1>::type type;
  56. };
  57. template<typename R>
  58. class call_with_tuple_args
  59. {
  60. public:
  61. typedef R result_type;
  62. template<typename Func, typename ... Args, std::size_t N>
  63. R operator()(Func &func, BOOST_SIGNALS2_TUPLE<Args...> args, mpl::size_t<N>) const
  64. {
  65. typedef typename make_unsigned_meta_array<N>::type indices_type;
  66. typename Func::result_type *resolver = 0;
  67. return m_invoke(resolver, func, indices_type(), args);
  68. }
  69. private:
  70. template<typename T, typename Func, unsigned ... indices, typename ... Args>
  71. R m_invoke(T *, Func &func, unsigned_meta_array<indices...>, BOOST_SIGNALS2_TUPLE<Args...> args) const
  72. {
  73. return func(BOOST_SIGNALS2_GET<indices>(args)...);
  74. }
  75. template<typename Func, unsigned ... indices, typename ... Args>
  76. R m_invoke(void *, Func &func, unsigned_meta_array<indices...>, BOOST_SIGNALS2_TUPLE<Args...> args) const
  77. {
  78. func(BOOST_SIGNALS2_GET<indices>(args)...);
  79. return R();
  80. }
  81. };
  82. template<typename R, typename ... Args>
  83. class variadic_slot_invoker
  84. {
  85. public:
  86. typedef R result_type;
  87. variadic_slot_invoker(Args & ... args): _args(args...)
  88. {}
  89. template<typename ConnectionBodyType>
  90. result_type operator ()(const ConnectionBodyType &connectionBody) const
  91. {
  92. result_type *resolver = 0;
  93. return m_invoke(connectionBody,
  94. resolver);
  95. }
  96. private:
  97. template<typename ConnectionBodyType>
  98. result_type m_invoke(const ConnectionBodyType &connectionBody,
  99. const void_type *) const
  100. {
  101. return call_with_tuple_args<result_type>()(connectionBody->slot.slot_function(), _args, mpl::size_t<sizeof...(Args)>());
  102. return void_type();
  103. }
  104. template<typename ConnectionBodyType>
  105. result_type m_invoke(const ConnectionBodyType &connectionBody, ...) const
  106. {
  107. return call_with_tuple_args<result_type>()(connectionBody->slot.slot_function(), _args, mpl::size_t<sizeof...(Args)>());
  108. }
  109. BOOST_SIGNALS2_TUPLE<Args& ...> _args;
  110. };
  111. } // namespace detail
  112. } // namespace signals2
  113. } // namespace boost
  114. #endif // BOOST_SIGNALS2_DETAIL_VARIADIC_SLOT_INVOKER_HPP