]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libc++/include/__functional_base
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libc++ / include / __functional_base
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_FUNCTIONAL_BASE
12 #define _LIBCPP_FUNCTIONAL_BASE
13
14 #include <__config>
15 #include <type_traits>
16 #include <typeinfo>
17 #include <exception>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
21 #endif
22
23 _LIBCPP_BEGIN_NAMESPACE_STD
24
25 template <class _Arg, class _Result>
26 struct _LIBCPP_TYPE_VIS unary_function
27 {
28     typedef _Arg    argument_type;
29     typedef _Result result_type;
30 };
31
32 template <class _Arg1, class _Arg2, class _Result>
33 struct _LIBCPP_TYPE_VIS binary_function
34 {
35     typedef _Arg1   first_argument_type;
36     typedef _Arg2   second_argument_type;
37     typedef _Result result_type;
38 };
39
40 template <class _Tp> struct _LIBCPP_TYPE_VIS hash;
41
42 template <class _Tp>
43 struct __has_result_type
44 {
45 private:
46     struct __two {char __lx; char __lxx;};
47     template <class _Up> static __two __test(...);
48     template <class _Up> static char __test(typename _Up::result_type* = 0);
49 public:
50     static const bool value = sizeof(__test<_Tp>(0)) == 1;
51 };
52
53 template <class _Tp>
54 struct _LIBCPP_TYPE_VIS less : binary_function<_Tp, _Tp, bool>
55 {
56     _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
57         {return __x < __y;}
58 };
59
60 #ifdef _LIBCPP_HAS_NO_VARIADICS
61
62 #include <__functional_base_03>
63
64 #else  // _LIBCPP_HAS_NO_VARIADICS
65
66 // __weak_result_type
67
68 template <class _Tp>
69 struct __derives_from_unary_function
70 {
71 private:
72     struct __two {char __lx; char __lxx;};
73     static __two __test(...);
74     template <class _Ap, class _Rp>
75         static unary_function<_Ap, _Rp>
76         __test(const volatile unary_function<_Ap, _Rp>*);
77 public:
78     static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
79     typedef decltype(__test((_Tp*)0)) type;
80 };
81
82 template <class _Tp>
83 struct __derives_from_binary_function
84 {
85 private:
86     struct __two {char __lx; char __lxx;};
87     static __two __test(...);
88     template <class _A1, class _A2, class _Rp>
89         static binary_function<_A1, _A2, _Rp>
90         __test(const volatile binary_function<_A1, _A2, _Rp>*);
91 public:
92     static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
93     typedef decltype(__test((_Tp*)0)) type;
94 };
95
96 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
97 struct __maybe_derive_from_unary_function  // bool is true
98     : public __derives_from_unary_function<_Tp>::type
99 {
100 };
101
102 template <class _Tp>
103 struct __maybe_derive_from_unary_function<_Tp, false>
104 {
105 };
106
107 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
108 struct __maybe_derive_from_binary_function  // bool is true
109     : public __derives_from_binary_function<_Tp>::type
110 {
111 };
112
113 template <class _Tp>
114 struct __maybe_derive_from_binary_function<_Tp, false>
115 {
116 };
117
118 template <class _Tp, bool = __has_result_type<_Tp>::value>
119 struct __weak_result_type_imp // bool is true
120     : public __maybe_derive_from_unary_function<_Tp>,
121       public __maybe_derive_from_binary_function<_Tp>
122 {
123     typedef typename _Tp::result_type result_type;
124 };
125
126 template <class _Tp>
127 struct __weak_result_type_imp<_Tp, false>
128     : public __maybe_derive_from_unary_function<_Tp>,
129       public __maybe_derive_from_binary_function<_Tp>
130 {
131 };
132
133 template <class _Tp>
134 struct __weak_result_type
135     : public __weak_result_type_imp<_Tp>
136 {
137 };
138
139 // 0 argument case
140
141 template <class _Rp>
142 struct __weak_result_type<_Rp ()>
143 {
144     typedef _Rp result_type;
145 };
146
147 template <class _Rp>
148 struct __weak_result_type<_Rp (&)()>
149 {
150     typedef _Rp result_type;
151 };
152
153 template <class _Rp>
154 struct __weak_result_type<_Rp (*)()>
155 {
156     typedef _Rp result_type;
157 };
158
159 // 1 argument case
160
161 template <class _Rp, class _A1>
162 struct __weak_result_type<_Rp (_A1)>
163     : public unary_function<_A1, _Rp>
164 {
165 };
166
167 template <class _Rp, class _A1>
168 struct __weak_result_type<_Rp (&)(_A1)>
169     : public unary_function<_A1, _Rp>
170 {
171 };
172
173 template <class _Rp, class _A1>
174 struct __weak_result_type<_Rp (*)(_A1)>
175     : public unary_function<_A1, _Rp>
176 {
177 };
178
179 template <class _Rp, class _Cp>
180 struct __weak_result_type<_Rp (_Cp::*)()>
181     : public unary_function<_Cp*, _Rp>
182 {
183 };
184
185 template <class _Rp, class _Cp>
186 struct __weak_result_type<_Rp (_Cp::*)() const>
187     : public unary_function<const _Cp*, _Rp>
188 {
189 };
190
191 template <class _Rp, class _Cp>
192 struct __weak_result_type<_Rp (_Cp::*)() volatile>
193     : public unary_function<volatile _Cp*, _Rp>
194 {
195 };
196
197 template <class _Rp, class _Cp>
198 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
199     : public unary_function<const volatile _Cp*, _Rp>
200 {
201 };
202
203 // 2 argument case
204
205 template <class _Rp, class _A1, class _A2>
206 struct __weak_result_type<_Rp (_A1, _A2)>
207     : public binary_function<_A1, _A2, _Rp>
208 {
209 };
210
211 template <class _Rp, class _A1, class _A2>
212 struct __weak_result_type<_Rp (*)(_A1, _A2)>
213     : public binary_function<_A1, _A2, _Rp>
214 {
215 };
216
217 template <class _Rp, class _A1, class _A2>
218 struct __weak_result_type<_Rp (&)(_A1, _A2)>
219     : public binary_function<_A1, _A2, _Rp>
220 {
221 };
222
223 template <class _Rp, class _Cp, class _A1>
224 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
225     : public binary_function<_Cp*, _A1, _Rp>
226 {
227 };
228
229 template <class _Rp, class _Cp, class _A1>
230 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
231     : public binary_function<const _Cp*, _A1, _Rp>
232 {
233 };
234
235 template <class _Rp, class _Cp, class _A1>
236 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
237     : public binary_function<volatile _Cp*, _A1, _Rp>
238 {
239 };
240
241 template <class _Rp, class _Cp, class _A1>
242 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
243     : public binary_function<const volatile _Cp*, _A1, _Rp>
244 {
245 };
246
247 // 3 or more arguments
248
249 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
250 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
251 {
252     typedef _Rp result_type;
253 };
254
255 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
256 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
257 {
258     typedef _Rp result_type;
259 };
260
261 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
262 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
263 {
264     typedef _Rp result_type;
265 };
266
267 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
268 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
269 {
270     typedef _Rp result_type;
271 };
272
273 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
274 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
275 {
276     typedef _Rp result_type;
277 };
278
279 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
280 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
281 {
282     typedef _Rp result_type;
283 };
284
285 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
286 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
287 {
288     typedef _Rp result_type;
289 };
290
291 // __invoke
292
293 // bullets 1 and 2
294
295 template <class _Fp, class _A0, class ..._Args,
296             class>
297 inline _LIBCPP_INLINE_VISIBILITY
298 auto
299 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
300     -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
301 {
302     return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
303 }
304
305 template <class _Fp, class _A0, class ..._Args,
306             class>
307 inline _LIBCPP_INLINE_VISIBILITY
308 auto
309 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
310     -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
311 {
312     return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
313 }
314
315 // bullets 3 and 4
316
317 template <class _Fp, class _A0,
318             class>
319 inline _LIBCPP_INLINE_VISIBILITY
320 auto
321 __invoke(_Fp&& __f, _A0&& __a0)
322     -> decltype(_VSTD::forward<_A0>(__a0).*__f)
323 {
324     return _VSTD::forward<_A0>(__a0).*__f;
325 }
326
327 template <class _Fp, class _A0,
328             class>
329 inline _LIBCPP_INLINE_VISIBILITY
330 auto
331 __invoke(_Fp&& __f, _A0&& __a0)
332     -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
333 {
334     return (*_VSTD::forward<_A0>(__a0)).*__f;
335 }
336
337 // bullet 5
338
339 template <class _Fp, class ..._Args>
340 inline _LIBCPP_INLINE_VISIBILITY
341 auto
342 __invoke(_Fp&& __f, _Args&& ...__args)
343     -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
344 {
345     return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
346 }
347
348 template <class _Tp, class ..._Args>
349 struct __invoke_return
350 {
351     typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
352 };
353
354 template <class _Tp>
355 class _LIBCPP_TYPE_VIS reference_wrapper
356     : public __weak_result_type<_Tp>
357 {
358 public:
359     // types
360     typedef _Tp type;
361 private:
362     type* __f_;
363
364 public:
365     // construct/copy/destroy
366     _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT : __f_(&__f) {}
367 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
368     private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
369 #endif
370
371     // access
372     _LIBCPP_INLINE_VISIBILITY operator type&    () const _NOEXCEPT {return *__f_;}
373     _LIBCPP_INLINE_VISIBILITY          type& get() const _NOEXCEPT {return *__f_;}
374
375     // invoke
376     template <class... _ArgTypes>
377        _LIBCPP_INLINE_VISIBILITY
378        typename __invoke_of<type&, _ArgTypes...>::type
379           operator() (_ArgTypes&&... __args) const
380           {
381               return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
382           }
383 };
384
385 template <class _Tp> struct ____is_reference_wrapper : public false_type {};
386 template <class _Tp> struct ____is_reference_wrapper<reference_wrapper<_Tp> > : public true_type {};
387 template <class _Tp> struct __is_reference_wrapper
388     : public ____is_reference_wrapper<typename remove_cv<_Tp>::type> {};
389
390 template <class _Tp>
391 inline _LIBCPP_INLINE_VISIBILITY
392 reference_wrapper<_Tp>
393 ref(_Tp& __t) _NOEXCEPT
394 {
395     return reference_wrapper<_Tp>(__t);
396 }
397
398 template <class _Tp>
399 inline _LIBCPP_INLINE_VISIBILITY
400 reference_wrapper<_Tp>
401 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
402 {
403     return ref(__t.get());
404 }
405
406 template <class _Tp>
407 inline _LIBCPP_INLINE_VISIBILITY
408 reference_wrapper<const _Tp>
409 cref(const _Tp& __t) _NOEXCEPT
410 {
411     return reference_wrapper<const _Tp>(__t);
412 }
413
414 template <class _Tp>
415 inline _LIBCPP_INLINE_VISIBILITY
416 reference_wrapper<const _Tp>
417 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
418 {
419     return cref(__t.get());
420 }
421
422 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
423 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
424
425 template <class _Tp> void ref(const _Tp&&) = delete;
426 template <class _Tp> void cref(const _Tp&&) = delete;
427
428 #else  // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
429
430 template <class _Tp> void ref(const _Tp&&);// = delete;
431 template <class _Tp> void cref(const _Tp&&);// = delete;
432
433 #endif  // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
434
435 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
436
437 #endif  // _LIBCPP_HAS_NO_VARIADICS
438
439 _LIBCPP_END_NAMESPACE_STD
440
441 #endif  // _LIBCPP_FUNCTIONAL_BASE