bind_member_function.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*=============================================================================
  2. Copyright (c) 2016 Kohei Takahashi
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #ifndef PHOENIX_BIND_BIND_MEMBER_FUNCTION_HPP
  7. #define PHOENIX_BIND_BIND_MEMBER_FUNCTION_HPP
  8. #include <boost/phoenix/core/limits.hpp>
  9. #if defined(BOOST_PHOENIX_NO_VARIADIC_BIND)
  10. # include <boost/phoenix/bind/detail/cpp03/bind_member_function.hpp>
  11. #else
  12. #include <boost/phoenix/core/expression.hpp>
  13. #include <boost/phoenix/core/reference.hpp>
  14. #include <boost/phoenix/core/detail/function_eval.hpp>
  15. namespace boost { namespace phoenix
  16. {
  17. namespace detail
  18. {
  19. template <typename RT, typename FP>
  20. struct member_function_ptr
  21. {
  22. typedef RT result_type;
  23. member_function_ptr(FP fp_)
  24. : fp(fp_) {}
  25. template <typename Class, typename... A>
  26. result_type operator()(Class& obj, A&... a) const
  27. {
  28. BOOST_PROTO_USE_GET_POINTER();
  29. typedef typename proto::detail::class_member_traits<FP>::class_type class_type;
  30. return (BOOST_PROTO_GET_POINTER(class_type, obj)->*fp)(a...);
  31. }
  32. template <typename Class, typename... A>
  33. result_type operator()(Class* obj, A&... a) const
  34. {
  35. return (obj->*fp)(a...);
  36. }
  37. bool operator==(member_function_ptr const& rhs) const
  38. {
  39. return fp == rhs.fp;
  40. }
  41. template <int M, typename RhsRT, typename RhsFP>
  42. bool operator==(member_function_ptr<RhsRT, RhsFP> const& /*rhs*/) const
  43. {
  44. return false;
  45. }
  46. FP fp;
  47. };
  48. } // namespace boost::phoenix::detail
  49. template <typename RT, typename ClassT, typename... T, typename ClassA, typename... A>
  50. inline
  51. typename detail::expression::function_eval<
  52. detail::member_function_ptr<RT, RT(ClassT::*)(T...)>
  53. , ClassA
  54. , A...
  55. >::type const
  56. bind(RT (ClassT::*f)(T...), ClassA const & obj, A const&... a)
  57. {
  58. typedef detail::member_function_ptr<RT, RT (ClassT::*)(T...)> fp_type;
  59. return detail::expression::function_eval<fp_type, ClassA, A...>::make(fp_type(f), obj, a...);
  60. }
  61. template <typename RT, typename ClassT, typename... T, typename ClassA, typename... A>
  62. inline
  63. typename detail::expression::function_eval<
  64. detail::member_function_ptr<RT, RT (ClassT::*)(T...) const>
  65. , ClassA
  66. , A...
  67. >::type const
  68. bind(RT (ClassT::*f)(T...) const, ClassA const & obj, A const&... a)
  69. {
  70. typedef detail::member_function_ptr<RT, RT(ClassT::*)(T...) const> fp_type;
  71. return detail::expression::function_eval<fp_type, ClassA, A...>::make(fp_type(f), obj, a...);
  72. }
  73. template <typename RT, typename ClassT, typename... T, typename... A>
  74. inline
  75. typename detail::expression::function_eval<
  76. detail::member_function_ptr<RT, RT(ClassT::*)(T...)>
  77. , ClassT
  78. , A...
  79. >::type const
  80. bind(RT (ClassT::*f)(T...), ClassT & obj, A const&... a)
  81. {
  82. typedef detail::member_function_ptr<RT, RT(ClassT::*)(T...)> fp_type;
  83. return detail::expression::function_eval<fp_type, ClassT, A...>::make(fp_type(f), obj, a...);
  84. }
  85. template <typename RT, typename ClassT, typename... T, typename... A>
  86. inline
  87. typename detail::expression::function_eval<
  88. detail::member_function_ptr<RT, RT(ClassT::*)(T...) const>
  89. , ClassT
  90. , A...
  91. >::type const
  92. bind(RT (ClassT::*f)(T...) const, ClassT const& obj, A const&... a)
  93. {
  94. typedef detail::member_function_ptr<RT, RT(ClassT::*)(T...) const> fp_type;
  95. return detail::expression::function_eval<fp_type, ClassT, A...>::make(fp_type(f), obj, a...);
  96. }
  97. }} // namespace boost::phoenix
  98. #endif
  99. #endif