Clang Project

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