1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | #ifndef _GLIBCXX_REFWRAP_H |
31 | #define _GLIBCXX_REFWRAP_H 1 |
32 | |
33 | #pragma GCC system_header |
34 | |
35 | #if __cplusplus < 201103L |
36 | # include <bits/c++0x_warning.h> |
37 | #else |
38 | |
39 | #include <bits/move.h> |
40 | #include <bits/invoke.h> |
41 | #include <bits/stl_function.h> |
42 | |
43 | namespace std _GLIBCXX_VISIBILITY(default) |
44 | { |
45 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
46 | |
47 | |
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 | |
59 | |
60 | |
61 | template<typename _Functor> |
62 | struct _Weak_result_type_impl |
63 | : _Maybe_get_result_type<_Functor> |
64 | { }; |
65 | |
66 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
172 | |
173 | |
174 | template<typename _Functor> |
175 | struct _Weak_result_type |
176 | : _Weak_result_type_impl<typename remove_cv<_Functor>::type> |
177 | { }; |
178 | |
179 | |
180 | template<typename _Tp, typename = __void_t<>> |
181 | struct _Refwrap_base_arg1 |
182 | { }; |
183 | |
184 | |
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 | |
193 | template<typename _Tp, typename = __void_t<>> |
194 | struct _Refwrap_base_arg2 |
195 | { }; |
196 | |
197 | |
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 | |
209 | |
210 | |
211 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
322 | |
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& __indata) noexcept |
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&&... __args) const |
355 | { |
356 | return std::__invoke(get(), std::forward<_Args>(__args)...); |
357 | } |
358 | }; |
359 | |
360 | |
361 | |
362 | template<typename _Tp> |
363 | inline reference_wrapper<_Tp> |
364 | ref(_Tp& __t) noexcept |
365 | { return reference_wrapper<_Tp>(__t); } |
366 | |
367 | |
368 | template<typename _Tp> |
369 | inline reference_wrapper<const _Tp> |
370 | cref(const _Tp& __t) noexcept |
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 | |
380 | template<typename _Tp> |
381 | inline reference_wrapper<_Tp> |
382 | ref(reference_wrapper<_Tp> __t) noexcept |
383 | { return ref(__t.get()); } |
384 | |
385 | |
386 | template<typename _Tp> |
387 | inline reference_wrapper<const _Tp> |
388 | cref(reference_wrapper<_Tp> __t) noexcept |
389 | { return cref(__t.get()); } |
390 | |
391 | |
392 | |
393 | _GLIBCXX_END_NAMESPACE_VERSION |
394 | } |
395 | |
396 | #endif |
397 | |
398 | #endif |
399 | |