xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/bits/refwrap.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Implementation of std::reference_wrapper -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2004-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file include/bits/refwrap.h
26*38fd1498Szrj  *  This is an internal header file, included by other library headers.
27*38fd1498Szrj  *  Do not attempt to use it directly. @headername{functional}
28*38fd1498Szrj  */
29*38fd1498Szrj 
30*38fd1498Szrj #ifndef _GLIBCXX_REFWRAP_H
31*38fd1498Szrj #define _GLIBCXX_REFWRAP_H 1
32*38fd1498Szrj 
33*38fd1498Szrj #pragma GCC system_header
34*38fd1498Szrj 
35*38fd1498Szrj #if __cplusplus < 201103L
36*38fd1498Szrj # include <bits/c++0x_warning.h>
37*38fd1498Szrj #else
38*38fd1498Szrj 
39*38fd1498Szrj #include <bits/move.h>
40*38fd1498Szrj #include <bits/invoke.h>
41*38fd1498Szrj #include <bits/stl_function.h> // for unary_function and binary_function
42*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)43*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
44*38fd1498Szrj {
45*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
46*38fd1498Szrj 
47*38fd1498Szrj   /**
48*38fd1498Szrj    * Derives from @c unary_function or @c binary_function, or perhaps
49*38fd1498Szrj    * nothing, depending on the number of arguments provided. The
50*38fd1498Szrj    * primary template is the basis case, which derives nothing.
51*38fd1498Szrj    */
52*38fd1498Szrj   template<typename _Res, typename... _ArgTypes>
53*38fd1498Szrj     struct _Maybe_unary_or_binary_function { };
54*38fd1498Szrj 
55*38fd1498Szrj   /// Derives from @c unary_function, as appropriate.
56*38fd1498Szrj   template<typename _Res, typename _T1>
57*38fd1498Szrj     struct _Maybe_unary_or_binary_function<_Res, _T1>
58*38fd1498Szrj     : std::unary_function<_T1, _Res> { };
59*38fd1498Szrj 
60*38fd1498Szrj   /// Derives from @c binary_function, as appropriate.
61*38fd1498Szrj   template<typename _Res, typename _T1, typename _T2>
62*38fd1498Szrj     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
63*38fd1498Szrj     : std::binary_function<_T1, _T2, _Res> { };
64*38fd1498Szrj 
65*38fd1498Szrj   template<typename _Signature>
66*38fd1498Szrj     struct _Mem_fn_traits;
67*38fd1498Szrj 
68*38fd1498Szrj   template<typename _Res, typename _Class, typename... _ArgTypes>
69*38fd1498Szrj     struct _Mem_fn_traits_base
70*38fd1498Szrj     {
71*38fd1498Szrj       using __result_type = _Res;
72*38fd1498Szrj       using __maybe_type
73*38fd1498Szrj 	= _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
74*38fd1498Szrj       using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
75*38fd1498Szrj     };
76*38fd1498Szrj 
77*38fd1498Szrj #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL)		\
78*38fd1498Szrj   template<typename _Res, typename _Class, typename... _ArgTypes>	\
79*38fd1498Szrj     struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF>	\
80*38fd1498Szrj     : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...>		\
81*38fd1498Szrj     {									\
82*38fd1498Szrj       using __vararg = false_type;					\
83*38fd1498Szrj     };									\
84*38fd1498Szrj   template<typename _Res, typename _Class, typename... _ArgTypes>	\
85*38fd1498Szrj     struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF>	\
86*38fd1498Szrj     : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...>		\
87*38fd1498Szrj     {									\
88*38fd1498Szrj       using __vararg = true_type;					\
89*38fd1498Szrj     };
90*38fd1498Szrj 
91*38fd1498Szrj #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL)		\
92*38fd1498Szrj   _GLIBCXX_MEM_FN_TRAITS2(		, _REF, _LVAL, _RVAL)	\
93*38fd1498Szrj   _GLIBCXX_MEM_FN_TRAITS2(const		, _REF, _LVAL, _RVAL)	\
94*38fd1498Szrj   _GLIBCXX_MEM_FN_TRAITS2(volatile	, _REF, _LVAL, _RVAL)	\
95*38fd1498Szrj   _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
96*38fd1498Szrj 
97*38fd1498Szrj _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
98*38fd1498Szrj _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
99*38fd1498Szrj _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
100*38fd1498Szrj 
101*38fd1498Szrj #if __cplusplus > 201402L
102*38fd1498Szrj _GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type)
103*38fd1498Szrj _GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type)
104*38fd1498Szrj _GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type)
105*38fd1498Szrj #endif
106*38fd1498Szrj 
107*38fd1498Szrj #undef _GLIBCXX_MEM_FN_TRAITS
108*38fd1498Szrj #undef _GLIBCXX_MEM_FN_TRAITS2
109*38fd1498Szrj 
110*38fd1498Szrj   /// If we have found a result_type, extract it.
111*38fd1498Szrj   template<typename _Functor, typename = __void_t<>>
112*38fd1498Szrj     struct _Maybe_get_result_type
113*38fd1498Szrj     { };
114*38fd1498Szrj 
115*38fd1498Szrj   template<typename _Functor>
116*38fd1498Szrj     struct _Maybe_get_result_type<_Functor,
117*38fd1498Szrj 				  __void_t<typename _Functor::result_type>>
118*38fd1498Szrj     { typedef typename _Functor::result_type result_type; };
119*38fd1498Szrj 
120*38fd1498Szrj   /**
121*38fd1498Szrj    *  Base class for any function object that has a weak result type, as
122*38fd1498Szrj    *  defined in 20.8.2 [func.require] of C++11.
123*38fd1498Szrj   */
124*38fd1498Szrj   template<typename _Functor>
125*38fd1498Szrj     struct _Weak_result_type_impl
126*38fd1498Szrj     : _Maybe_get_result_type<_Functor>
127*38fd1498Szrj     { };
128*38fd1498Szrj 
129*38fd1498Szrj   /// Retrieve the result type for a function type.
130*38fd1498Szrj   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
131*38fd1498Szrj     struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
132*38fd1498Szrj     { typedef _Res result_type; };
133*38fd1498Szrj 
134*38fd1498Szrj   /// Retrieve the result type for a varargs function type.
135*38fd1498Szrj   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
136*38fd1498Szrj     struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
137*38fd1498Szrj     { typedef _Res result_type; };
138*38fd1498Szrj 
139*38fd1498Szrj   /// Retrieve the result type for a function pointer.
140*38fd1498Szrj   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
141*38fd1498Szrj     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
142*38fd1498Szrj     { typedef _Res result_type; };
143*38fd1498Szrj 
144*38fd1498Szrj   /// Retrieve the result type for a varargs function pointer.
145*38fd1498Szrj   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
146*38fd1498Szrj     struct
147*38fd1498Szrj     _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
148*38fd1498Szrj     { typedef _Res result_type; };
149*38fd1498Szrj 
150*38fd1498Szrj   // Let _Weak_result_type_impl perform the real work.
151*38fd1498Szrj   template<typename _Functor,
152*38fd1498Szrj 	   bool = is_member_function_pointer<_Functor>::value>
153*38fd1498Szrj     struct _Weak_result_type_memfun
154*38fd1498Szrj     : _Weak_result_type_impl<_Functor>
155*38fd1498Szrj     { };
156*38fd1498Szrj 
157*38fd1498Szrj   // A pointer to member function has a weak result type.
158*38fd1498Szrj   template<typename _MemFunPtr>
159*38fd1498Szrj     struct _Weak_result_type_memfun<_MemFunPtr, true>
160*38fd1498Szrj     {
161*38fd1498Szrj       using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
162*38fd1498Szrj     };
163*38fd1498Szrj 
164*38fd1498Szrj   // A pointer to data member doesn't have a weak result type.
165*38fd1498Szrj   template<typename _Func, typename _Class>
166*38fd1498Szrj     struct _Weak_result_type_memfun<_Func _Class::*, false>
167*38fd1498Szrj     { };
168*38fd1498Szrj 
169*38fd1498Szrj   /**
170*38fd1498Szrj    *  Strip top-level cv-qualifiers from the function object and let
171*38fd1498Szrj    *  _Weak_result_type_memfun perform the real work.
172*38fd1498Szrj   */
173*38fd1498Szrj   template<typename _Functor>
174*38fd1498Szrj     struct _Weak_result_type
175*38fd1498Szrj     : _Weak_result_type_memfun<typename remove_cv<_Functor>::type>
176*38fd1498Szrj     { };
177*38fd1498Szrj 
178*38fd1498Szrj   // Detect nested argument_type.
179*38fd1498Szrj   template<typename _Tp, typename = __void_t<>>
180*38fd1498Szrj     struct _Refwrap_base_arg1
181*38fd1498Szrj     { };
182*38fd1498Szrj 
183*38fd1498Szrj   // Nested argument_type.
184*38fd1498Szrj   template<typename _Tp>
185*38fd1498Szrj     struct _Refwrap_base_arg1<_Tp,
186*38fd1498Szrj 			      __void_t<typename _Tp::argument_type>>
187*38fd1498Szrj     {
188*38fd1498Szrj       typedef typename _Tp::argument_type argument_type;
189*38fd1498Szrj     };
190*38fd1498Szrj 
191*38fd1498Szrj   // Detect nested first_argument_type and second_argument_type.
192*38fd1498Szrj   template<typename _Tp, typename = __void_t<>>
193*38fd1498Szrj     struct _Refwrap_base_arg2
194*38fd1498Szrj     { };
195*38fd1498Szrj 
196*38fd1498Szrj   // Nested first_argument_type and second_argument_type.
197*38fd1498Szrj   template<typename _Tp>
198*38fd1498Szrj     struct _Refwrap_base_arg2<_Tp,
199*38fd1498Szrj 			      __void_t<typename _Tp::first_argument_type,
200*38fd1498Szrj 				       typename _Tp::second_argument_type>>
201*38fd1498Szrj     {
202*38fd1498Szrj       typedef typename _Tp::first_argument_type first_argument_type;
203*38fd1498Szrj       typedef typename _Tp::second_argument_type second_argument_type;
204*38fd1498Szrj     };
205*38fd1498Szrj 
206*38fd1498Szrj   /**
207*38fd1498Szrj    *  Derives from unary_function or binary_function when it
208*38fd1498Szrj    *  can. Specializations handle all of the easy cases. The primary
209*38fd1498Szrj    *  template determines what to do with a class type, which may
210*38fd1498Szrj    *  derive from both unary_function and binary_function.
211*38fd1498Szrj   */
212*38fd1498Szrj   template<typename _Tp>
213*38fd1498Szrj     struct _Reference_wrapper_base
214*38fd1498Szrj     : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
215*38fd1498Szrj     { };
216*38fd1498Szrj 
217*38fd1498Szrj   // - a function type (unary)
218*38fd1498Szrj   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
219*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL>
220*38fd1498Szrj     : unary_function<_T1, _Res>
221*38fd1498Szrj     { };
222*38fd1498Szrj 
223*38fd1498Szrj   template<typename _Res, typename _T1>
224*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1) const>
225*38fd1498Szrj     : unary_function<_T1, _Res>
226*38fd1498Szrj     { };
227*38fd1498Szrj 
228*38fd1498Szrj   template<typename _Res, typename _T1>
229*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1) volatile>
230*38fd1498Szrj     : unary_function<_T1, _Res>
231*38fd1498Szrj     { };
232*38fd1498Szrj 
233*38fd1498Szrj   template<typename _Res, typename _T1>
234*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1) const volatile>
235*38fd1498Szrj     : unary_function<_T1, _Res>
236*38fd1498Szrj     { };
237*38fd1498Szrj 
238*38fd1498Szrj   // - a function type (binary)
239*38fd1498Szrj   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
240*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
241*38fd1498Szrj     : binary_function<_T1, _T2, _Res>
242*38fd1498Szrj     { };
243*38fd1498Szrj 
244*38fd1498Szrj   template<typename _Res, typename _T1, typename _T2>
245*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1, _T2) const>
246*38fd1498Szrj     : binary_function<_T1, _T2, _Res>
247*38fd1498Szrj     { };
248*38fd1498Szrj 
249*38fd1498Szrj   template<typename _Res, typename _T1, typename _T2>
250*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
251*38fd1498Szrj     : binary_function<_T1, _T2, _Res>
252*38fd1498Szrj     { };
253*38fd1498Szrj 
254*38fd1498Szrj   template<typename _Res, typename _T1, typename _T2>
255*38fd1498Szrj     struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
256*38fd1498Szrj     : binary_function<_T1, _T2, _Res>
257*38fd1498Szrj     { };
258*38fd1498Szrj 
259*38fd1498Szrj   // - a function pointer type (unary)
260*38fd1498Szrj   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
261*38fd1498Szrj     struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL>
262*38fd1498Szrj     : unary_function<_T1, _Res>
263*38fd1498Szrj     { };
264*38fd1498Szrj 
265*38fd1498Szrj   // - a function pointer type (binary)
266*38fd1498Szrj   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
267*38fd1498Szrj     struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
268*38fd1498Szrj     : binary_function<_T1, _T2, _Res>
269*38fd1498Szrj     { };
270*38fd1498Szrj 
271*38fd1498Szrj   template<typename _Tp, bool = is_member_function_pointer<_Tp>::value>
272*38fd1498Szrj     struct _Reference_wrapper_base_memfun
273*38fd1498Szrj     : _Reference_wrapper_base<_Tp>
274*38fd1498Szrj     { };
275*38fd1498Szrj 
276*38fd1498Szrj   template<typename _MemFunPtr>
277*38fd1498Szrj     struct _Reference_wrapper_base_memfun<_MemFunPtr, true>
278*38fd1498Szrj     : _Mem_fn_traits<_MemFunPtr>::__maybe_type
279*38fd1498Szrj     {
280*38fd1498Szrj       using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
281*38fd1498Szrj     };
282*38fd1498Szrj 
283*38fd1498Szrj   /**
284*38fd1498Szrj    *  @brief Primary class template for reference_wrapper.
285*38fd1498Szrj    *  @ingroup functors
286*38fd1498Szrj    *  @{
287*38fd1498Szrj    */
288*38fd1498Szrj   template<typename _Tp>
289*38fd1498Szrj     class reference_wrapper
290*38fd1498Szrj     : public _Reference_wrapper_base_memfun<typename remove_cv<_Tp>::type>
291*38fd1498Szrj     {
292*38fd1498Szrj       _Tp* _M_data;
293*38fd1498Szrj 
294*38fd1498Szrj     public:
295*38fd1498Szrj       typedef _Tp type;
296*38fd1498Szrj 
297*38fd1498Szrj       reference_wrapper(_Tp& __indata) noexcept
298*38fd1498Szrj       : _M_data(std::__addressof(__indata))
299*38fd1498Szrj       { }
300*38fd1498Szrj 
301*38fd1498Szrj       reference_wrapper(_Tp&&) = delete;
302*38fd1498Szrj 
303*38fd1498Szrj       reference_wrapper(const reference_wrapper&) = default;
304*38fd1498Szrj 
305*38fd1498Szrj       reference_wrapper&
306*38fd1498Szrj       operator=(const reference_wrapper&) = default;
307*38fd1498Szrj 
308*38fd1498Szrj       operator _Tp&() const noexcept
309*38fd1498Szrj       { return this->get(); }
310*38fd1498Szrj 
311*38fd1498Szrj       _Tp&
312*38fd1498Szrj       get() const noexcept
313*38fd1498Szrj       { return *_M_data; }
314*38fd1498Szrj 
315*38fd1498Szrj       template<typename... _Args>
316*38fd1498Szrj 	typename result_of<_Tp&(_Args&&...)>::type
317*38fd1498Szrj 	operator()(_Args&&... __args) const
318*38fd1498Szrj 	{
319*38fd1498Szrj 	  return std::__invoke(get(), std::forward<_Args>(__args)...);
320*38fd1498Szrj 	}
321*38fd1498Szrj     };
322*38fd1498Szrj 
323*38fd1498Szrj 
324*38fd1498Szrj   /// Denotes a reference should be taken to a variable.
325*38fd1498Szrj   template<typename _Tp>
326*38fd1498Szrj     inline reference_wrapper<_Tp>
327*38fd1498Szrj     ref(_Tp& __t) noexcept
328*38fd1498Szrj     { return reference_wrapper<_Tp>(__t); }
329*38fd1498Szrj 
330*38fd1498Szrj   /// Denotes a const reference should be taken to a variable.
331*38fd1498Szrj   template<typename _Tp>
332*38fd1498Szrj     inline reference_wrapper<const _Tp>
333*38fd1498Szrj     cref(const _Tp& __t) noexcept
334*38fd1498Szrj     { return reference_wrapper<const _Tp>(__t); }
335*38fd1498Szrj 
336*38fd1498Szrj   template<typename _Tp>
337*38fd1498Szrj     void ref(const _Tp&&) = delete;
338*38fd1498Szrj 
339*38fd1498Szrj   template<typename _Tp>
340*38fd1498Szrj     void cref(const _Tp&&) = delete;
341*38fd1498Szrj 
342*38fd1498Szrj   /// std::ref overload to prevent wrapping a reference_wrapper
343*38fd1498Szrj   template<typename _Tp>
344*38fd1498Szrj     inline reference_wrapper<_Tp>
345*38fd1498Szrj     ref(reference_wrapper<_Tp> __t) noexcept
346*38fd1498Szrj     { return __t; }
347*38fd1498Szrj 
348*38fd1498Szrj   /// std::cref overload to prevent wrapping a reference_wrapper
349*38fd1498Szrj   template<typename _Tp>
350*38fd1498Szrj     inline reference_wrapper<const _Tp>
351*38fd1498Szrj     cref(reference_wrapper<_Tp> __t) noexcept
352*38fd1498Szrj     { return { __t.get() }; }
353*38fd1498Szrj 
354*38fd1498Szrj   // @} group functors
355*38fd1498Szrj 
356*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
357*38fd1498Szrj } // namespace std
358*38fd1498Szrj 
359*38fd1498Szrj #endif // C++11
360*38fd1498Szrj 
361*38fd1498Szrj #endif // _GLIBCXX_REFWRAP_H
362