reference_wrapper.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_REFERENCE_WRAPPER_H
  10. #define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H
  11. #include <__config>
  12. #include <__functional/weak_result_type.h>
  13. #include <__memory/addressof.h>
  14. #include <__utility/forward.h>
  15. #include <type_traits>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. template <class _Tp>
  21. class _LIBCPP_TEMPLATE_VIS reference_wrapper
  22. #if _LIBCPP_STD_VER <= 17 || !defined(_LIBCPP_ABI_NO_BINDER_BASES)
  23. : public __weak_result_type<_Tp>
  24. #endif
  25. {
  26. public:
  27. // types
  28. typedef _Tp type;
  29. private:
  30. type* __f_;
  31. static void __fun(_Tp&) _NOEXCEPT;
  32. static void __fun(_Tp&&) = delete;
  33. public:
  34. template <class _Up, class = __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, decltype(__fun(declval<_Up>())) > >
  35. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  36. reference_wrapper(_Up&& __u) _NOEXCEPT_(noexcept(__fun(declval<_Up>()))) {
  37. type& __f = static_cast<_Up&&>(__u);
  38. __f_ = _VSTD::addressof(__f);
  39. }
  40. // access
  41. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  42. operator type&() const _NOEXCEPT {return *__f_;}
  43. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  44. type& get() const _NOEXCEPT {return *__f_;}
  45. #ifndef _LIBCPP_CXX03_LANG
  46. // invoke
  47. template <class... _ArgTypes>
  48. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  49. typename __invoke_of<type&, _ArgTypes...>::type
  50. operator() (_ArgTypes&&... __args) const {
  51. return _VSTD::__invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
  52. }
  53. #else
  54. _LIBCPP_INLINE_VISIBILITY
  55. typename __invoke_return<type>::type
  56. operator() () const {
  57. return _VSTD::__invoke(get());
  58. }
  59. template <class _A0>
  60. _LIBCPP_INLINE_VISIBILITY
  61. typename __invoke_return0<type, _A0>::type
  62. operator() (_A0& __a0) const {
  63. return _VSTD::__invoke(get(), __a0);
  64. }
  65. template <class _A0>
  66. _LIBCPP_INLINE_VISIBILITY
  67. typename __invoke_return0<type, _A0 const>::type
  68. operator() (_A0 const& __a0) const {
  69. return _VSTD::__invoke(get(), __a0);
  70. }
  71. template <class _A0, class _A1>
  72. _LIBCPP_INLINE_VISIBILITY
  73. typename __invoke_return1<type, _A0, _A1>::type
  74. operator() (_A0& __a0, _A1& __a1) const {
  75. return _VSTD::__invoke(get(), __a0, __a1);
  76. }
  77. template <class _A0, class _A1>
  78. _LIBCPP_INLINE_VISIBILITY
  79. typename __invoke_return1<type, _A0 const, _A1>::type
  80. operator() (_A0 const& __a0, _A1& __a1) const {
  81. return _VSTD::__invoke(get(), __a0, __a1);
  82. }
  83. template <class _A0, class _A1>
  84. _LIBCPP_INLINE_VISIBILITY
  85. typename __invoke_return1<type, _A0, _A1 const>::type
  86. operator() (_A0& __a0, _A1 const& __a1) const {
  87. return _VSTD::__invoke(get(), __a0, __a1);
  88. }
  89. template <class _A0, class _A1>
  90. _LIBCPP_INLINE_VISIBILITY
  91. typename __invoke_return1<type, _A0 const, _A1 const>::type
  92. operator() (_A0 const& __a0, _A1 const& __a1) const {
  93. return _VSTD::__invoke(get(), __a0, __a1);
  94. }
  95. template <class _A0, class _A1, class _A2>
  96. _LIBCPP_INLINE_VISIBILITY
  97. typename __invoke_return2<type, _A0, _A1, _A2>::type
  98. operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
  99. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  100. }
  101. template <class _A0, class _A1, class _A2>
  102. _LIBCPP_INLINE_VISIBILITY
  103. typename __invoke_return2<type, _A0 const, _A1, _A2>::type
  104. operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
  105. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  106. }
  107. template <class _A0, class _A1, class _A2>
  108. _LIBCPP_INLINE_VISIBILITY
  109. typename __invoke_return2<type, _A0, _A1 const, _A2>::type
  110. operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
  111. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  112. }
  113. template <class _A0, class _A1, class _A2>
  114. _LIBCPP_INLINE_VISIBILITY
  115. typename __invoke_return2<type, _A0, _A1, _A2 const>::type
  116. operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
  117. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  118. }
  119. template <class _A0, class _A1, class _A2>
  120. _LIBCPP_INLINE_VISIBILITY
  121. typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
  122. operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
  123. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  124. }
  125. template <class _A0, class _A1, class _A2>
  126. _LIBCPP_INLINE_VISIBILITY
  127. typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
  128. operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
  129. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  130. }
  131. template <class _A0, class _A1, class _A2>
  132. _LIBCPP_INLINE_VISIBILITY
  133. typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
  134. operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
  135. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  136. }
  137. template <class _A0, class _A1, class _A2>
  138. _LIBCPP_INLINE_VISIBILITY
  139. typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
  140. operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
  141. return _VSTD::__invoke(get(), __a0, __a1, __a2);
  142. }
  143. #endif // _LIBCPP_CXX03_LANG
  144. };
  145. #if _LIBCPP_STD_VER > 14
  146. template <class _Tp>
  147. reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
  148. #endif
  149. template <class _Tp>
  150. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  151. reference_wrapper<_Tp>
  152. ref(_Tp& __t) _NOEXCEPT
  153. {
  154. return reference_wrapper<_Tp>(__t);
  155. }
  156. template <class _Tp>
  157. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  158. reference_wrapper<_Tp>
  159. ref(reference_wrapper<_Tp> __t) _NOEXCEPT
  160. {
  161. return __t;
  162. }
  163. template <class _Tp>
  164. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  165. reference_wrapper<const _Tp>
  166. cref(const _Tp& __t) _NOEXCEPT
  167. {
  168. return reference_wrapper<const _Tp>(__t);
  169. }
  170. template <class _Tp>
  171. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  172. reference_wrapper<const _Tp>
  173. cref(reference_wrapper<_Tp> __t) _NOEXCEPT
  174. {
  175. return __t;
  176. }
  177. template <class _Tp> void ref(const _Tp&&) = delete;
  178. template <class _Tp> void cref(const _Tp&&) = delete;
  179. _LIBCPP_END_NAMESPACE_STD
  180. #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H