mem_fn.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FUNCTIONAL_MEM_FN_H
  10. #define _LIBCPP___FUNCTIONAL_MEM_FN_H
  11. #include <__config>
  12. #include <__functional/binary_function.h>
  13. #include <__functional/invoke.h>
  14. #include <__functional/weak_result_type.h>
  15. #include <__utility/forward.h>
  16. #include <type_traits>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class _Tp>
  22. class __mem_fn
  23. #if _LIBCPP_STD_VER <= 17 || !defined(_LIBCPP_ABI_NO_BINDER_BASES)
  24. : public __weak_result_type<_Tp>
  25. #endif
  26. {
  27. public:
  28. // types
  29. typedef _Tp type;
  30. private:
  31. type __f_;
  32. public:
  33. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  34. __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
  35. #ifndef _LIBCPP_CXX03_LANG
  36. // invoke
  37. template <class... _ArgTypes>
  38. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  39. typename __invoke_return<type, _ArgTypes...>::type
  40. operator() (_ArgTypes&&... __args) const {
  41. return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
  42. }
  43. #else
  44. template <class _A0>
  45. _LIBCPP_INLINE_VISIBILITY
  46. typename __invoke_return0<type, _A0>::type
  47. operator() (_A0& __a0) const {
  48. return _VSTD::__invoke(__f_, __a0);
  49. }
  50. template <class _A0>
  51. _LIBCPP_INLINE_VISIBILITY
  52. typename __invoke_return0<type, _A0 const>::type
  53. operator() (_A0 const& __a0) const {
  54. return _VSTD::__invoke(__f_, __a0);
  55. }
  56. template <class _A0, class _A1>
  57. _LIBCPP_INLINE_VISIBILITY
  58. typename __invoke_return1<type, _A0, _A1>::type
  59. operator() (_A0& __a0, _A1& __a1) const {
  60. return _VSTD::__invoke(__f_, __a0, __a1);
  61. }
  62. template <class _A0, class _A1>
  63. _LIBCPP_INLINE_VISIBILITY
  64. typename __invoke_return1<type, _A0 const, _A1>::type
  65. operator() (_A0 const& __a0, _A1& __a1) const {
  66. return _VSTD::__invoke(__f_, __a0, __a1);
  67. }
  68. template <class _A0, class _A1>
  69. _LIBCPP_INLINE_VISIBILITY
  70. typename __invoke_return1<type, _A0, _A1 const>::type
  71. operator() (_A0& __a0, _A1 const& __a1) const {
  72. return _VSTD::__invoke(__f_, __a0, __a1);
  73. }
  74. template <class _A0, class _A1>
  75. _LIBCPP_INLINE_VISIBILITY
  76. typename __invoke_return1<type, _A0 const, _A1 const>::type
  77. operator() (_A0 const& __a0, _A1 const& __a1) const {
  78. return _VSTD::__invoke(__f_, __a0, __a1);
  79. }
  80. template <class _A0, class _A1, class _A2>
  81. _LIBCPP_INLINE_VISIBILITY
  82. typename __invoke_return2<type, _A0, _A1, _A2>::type
  83. operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
  84. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  85. }
  86. template <class _A0, class _A1, class _A2>
  87. _LIBCPP_INLINE_VISIBILITY
  88. typename __invoke_return2<type, _A0 const, _A1, _A2>::type
  89. operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
  90. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  91. }
  92. template <class _A0, class _A1, class _A2>
  93. _LIBCPP_INLINE_VISIBILITY
  94. typename __invoke_return2<type, _A0, _A1 const, _A2>::type
  95. operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
  96. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  97. }
  98. template <class _A0, class _A1, class _A2>
  99. _LIBCPP_INLINE_VISIBILITY
  100. typename __invoke_return2<type, _A0, _A1, _A2 const>::type
  101. operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
  102. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  103. }
  104. template <class _A0, class _A1, class _A2>
  105. _LIBCPP_INLINE_VISIBILITY
  106. typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
  107. operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
  108. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  109. }
  110. template <class _A0, class _A1, class _A2>
  111. _LIBCPP_INLINE_VISIBILITY
  112. typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
  113. operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
  114. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  115. }
  116. template <class _A0, class _A1, class _A2>
  117. _LIBCPP_INLINE_VISIBILITY
  118. typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
  119. operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
  120. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  121. }
  122. template <class _A0, class _A1, class _A2>
  123. _LIBCPP_INLINE_VISIBILITY
  124. typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
  125. operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
  126. return _VSTD::__invoke(__f_, __a0, __a1, __a2);
  127. }
  128. #endif
  129. };
  130. template<class _Rp, class _Tp>
  131. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  132. __mem_fn<_Rp _Tp::*>
  133. mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
  134. {
  135. return __mem_fn<_Rp _Tp::*>(__pm);
  136. }
  137. _LIBCPP_END_NAMESPACE_STD
  138. #endif // _LIBCPP___FUNCTIONAL_MEM_FN_H