reference_wrapper.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
  51. }
  52. };
  53. #if _LIBCPP_STD_VER > 14
  54. template <class _Tp>
  55. reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
  56. #endif
  57. template <class _Tp>
  58. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  59. reference_wrapper<_Tp>
  60. ref(_Tp& __t) _NOEXCEPT
  61. {
  62. return reference_wrapper<_Tp>(__t);
  63. }
  64. template <class _Tp>
  65. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  66. reference_wrapper<_Tp>
  67. ref(reference_wrapper<_Tp> __t) _NOEXCEPT
  68. {
  69. return __t;
  70. }
  71. template <class _Tp>
  72. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  73. reference_wrapper<const _Tp>
  74. cref(const _Tp& __t) _NOEXCEPT
  75. {
  76. return reference_wrapper<const _Tp>(__t);
  77. }
  78. template <class _Tp>
  79. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  80. reference_wrapper<const _Tp>
  81. cref(reference_wrapper<_Tp> __t) _NOEXCEPT
  82. {
  83. return __t;
  84. }
  85. template <class _Tp> void ref(const _Tp&&) = delete;
  86. template <class _Tp> void cref(const _Tp&&) = delete;
  87. _LIBCPP_END_NAMESPACE_STD
  88. #endif // _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H