reference_wrapper.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/invoke.h>
  13. #include <__functional/weak_result_type.h>
  14. #include <__memory/addressof.h>
  15. #include <__type_traits/enable_if.h>
  16. #include <__type_traits/remove_cvref.h>
  17. #include <__utility/declval.h>
  18. #include <__utility/forward.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template <class _Tp>
  24. class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp>
  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(std::declval<_Up>())) > >
  35. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  36. reference_wrapper(_Up&& __u) _NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {
  37. type& __f = static_cast<_Up&&>(__u);
  38. __f_ = _VSTD::addressof(__f);
  39. }
  40. // access
  41. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  42. operator type&() const _NOEXCEPT {return *__f_;}
  43. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  44. type& get() const _NOEXCEPT {return *__f_;}
  45. // invoke
  46. template <class... _ArgTypes>
  47. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  48. typename __invoke_of<type&, _ArgTypes...>::type
  49. operator() (_ArgTypes&&... __args) const
  50. #if _LIBCPP_STD_VER >= 17
  51. // Since is_nothrow_invocable requires C++17 LWG3764 is not backported
  52. // to earlier versions.
  53. noexcept(is_nothrow_invocable_v<_Tp&, _ArgTypes...>)
  54. #endif
  55. {
  56. return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
  57. }
  58. };
  59. #if _LIBCPP_STD_VER >= 17
  60. template <class _Tp>
  61. reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
  62. #endif
  63. template <class _Tp>
  64. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  65. reference_wrapper<_Tp>
  66. ref(_Tp& __t) _NOEXCEPT
  67. {
  68. return reference_wrapper<_Tp>(__t);
  69. }
  70. template <class _Tp>
  71. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  72. reference_wrapper<_Tp>
  73. ref(reference_wrapper<_Tp> __t) _NOEXCEPT
  74. {
  75. return __t;
  76. }
  77. template <class _Tp>
  78. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  79. reference_wrapper<const _Tp>
  80. cref(const _Tp& __t) _NOEXCEPT
  81. {
  82. return reference_wrapper<const _Tp>(__t);
  83. }
  84. template <class _Tp>
  85. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  86. reference_wrapper<const _Tp>
  87. cref(reference_wrapper<_Tp> __t) _NOEXCEPT
  88. {
  89. return __t;
  90. }
  91. template <class _Tp> void ref(const _Tp&&) = delete;
  92. template <class _Tp> void cref(const _Tp&&) = delete;
  93. _LIBCPP_END_NAMESPACE_STD
  94. #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H