xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__functional/reference_wrapper.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
10 #ifndef _LIBCPP___CXX03___FUNCTIONAL_REFERENCE_WRAPPER_H
11 #define _LIBCPP___CXX03___FUNCTIONAL_REFERENCE_WRAPPER_H
12 
13 #include <__cxx03/__config>
14 #include <__cxx03/__functional/weak_result_type.h>
15 #include <__cxx03/__memory/addressof.h>
16 #include <__cxx03/__type_traits/enable_if.h>
17 #include <__cxx03/__type_traits/invoke.h>
18 #include <__cxx03/__type_traits/is_const.h>
19 #include <__cxx03/__type_traits/remove_cvref.h>
20 #include <__cxx03/__type_traits/void_t.h>
21 #include <__cxx03/__utility/declval.h>
22 #include <__cxx03/__utility/forward.h>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 template <class _Tp>
31 class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp> {
32 public:
33   // types
34   typedef _Tp type;
35 
36 private:
37   type* __f_;
38 
39   static void __fun(_Tp&) _NOEXCEPT;
40   static void __fun(_Tp&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR54276
41 
42 public:
43   template <class _Up,
44             class = __void_t<decltype(__fun(std::declval<_Up>()))>,
45             __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, int> = 0>
reference_wrapper(_Up && __u)46   _LIBCPP_HIDE_FROM_ABI reference_wrapper(_Up&& __u) {
47     type& __f = static_cast<_Up&&>(__u);
48     __f_      = std::addressof(__f);
49   }
50 
51   // access
52   _LIBCPP_HIDE_FROM_ABI operator type&() const _NOEXCEPT { return *__f_; }
get()53   _LIBCPP_HIDE_FROM_ABI type& get() const _NOEXCEPT { return *__f_; }
54 
55   // invoke
56   template <class... _ArgTypes>
operator()57   _LIBCPP_HIDE_FROM_ABI typename __invoke_of<type&, _ArgTypes...>::type operator()(_ArgTypes&&... __args) const {
58     return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
59   }
60 };
61 
62 template <class _Tp>
ref(_Tp & __t)63 inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {
64   return reference_wrapper<_Tp>(__t);
65 }
66 
67 template <class _Tp>
ref(reference_wrapper<_Tp> __t)68 inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t) _NOEXCEPT {
69   return __t;
70 }
71 
72 template <class _Tp>
cref(const _Tp & __t)73 inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<const _Tp> cref(const _Tp& __t) _NOEXCEPT {
74   return reference_wrapper<const _Tp>(__t);
75 }
76 
77 template <class _Tp>
cref(reference_wrapper<_Tp> __t)78 inline _LIBCPP_HIDE_FROM_ABI reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t) _NOEXCEPT {
79   return __t;
80 }
81 
82 template <class _Tp>
83 void ref(const _Tp&&) = delete;
84 template <class _Tp>
85 void cref(const _Tp&&) = delete;
86 
87 _LIBCPP_END_NAMESPACE_STD
88 
89 #endif // _LIBCPP___CXX03___FUNCTIONAL_REFERENCE_WRAPPER_H
90