]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libc++/include/__functional_base
Pull in r228705 from upstream libc++ trunk (by Eric Fiselier):
[FreeBSD/stable/10.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 #include <new>
19
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
22 #endif
23
24 _LIBCPP_BEGIN_NAMESPACE_STD
25
26 template <class _Arg, class _Result>
27 struct _LIBCPP_TYPE_VIS_ONLY unary_function
28 {
29     typedef _Arg    argument_type;
30     typedef _Result result_type;
31 };
32
33 template <class _Arg1, class _Arg2, class _Result>
34 struct _LIBCPP_TYPE_VIS_ONLY binary_function
35 {
36     typedef _Arg1   first_argument_type;
37     typedef _Arg2   second_argument_type;
38     typedef _Result result_type;
39 };
40
41 template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash;
42
43 template <class _Tp>
44 struct __has_result_type
45 {
46 private:
47     struct __two {char __lx; char __lxx;};
48     template <class _Up> static __two __test(...);
49     template <class _Up> static char __test(typename _Up::result_type* = 0);
50 public:
51     static const bool value = sizeof(__test<_Tp>(0)) == 1;
52 };
53
54 #if _LIBCPP_STD_VER > 11
55 template <class _Tp = void>
56 #else
57 template <class _Tp>
58 #endif
59 struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool>
60 {
61     _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY 
62     bool operator()(const _Tp& __x, const _Tp& __y) const
63         {return __x < __y;}
64 };
65
66 #if _LIBCPP_STD_VER > 11
67 template <>
68 struct _LIBCPP_TYPE_VIS_ONLY less<void>
69 {
70     template <class _T1, class _T2> 
71     _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
72     auto operator()(_T1&& __t, _T2&& __u) const
73         { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
74     typedef void is_transparent;
75 };
76 #endif
77
78 // addressof
79
80 template <class _Tp>
81 inline _LIBCPP_INLINE_VISIBILITY
82 _Tp*
83 addressof(_Tp& __x) _NOEXCEPT
84 {
85     return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
86 }
87
88 #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
89 // Objective-C++ Automatic Reference Counting uses qualified pointers
90 // that require special addressof() signatures. When
91 // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
92 // itself is providing these definitions. Otherwise, we provide them.
93 template <class _Tp>
94 inline _LIBCPP_INLINE_VISIBILITY
95 __strong _Tp*
96 addressof(__strong _Tp& __x) _NOEXCEPT
97 {
98   return &__x;
99 }
100
101 #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
102 template <class _Tp>
103 inline _LIBCPP_INLINE_VISIBILITY
104 __weak _Tp*
105 addressof(__weak _Tp& __x) _NOEXCEPT
106 {
107   return &__x;
108 }
109 #endif
110
111 template <class _Tp>
112 inline _LIBCPP_INLINE_VISIBILITY
113 __autoreleasing _Tp*
114 addressof(__autoreleasing _Tp& __x) _NOEXCEPT
115 {
116   return &__x;
117 }
118
119 template <class _Tp>
120 inline _LIBCPP_INLINE_VISIBILITY
121 __unsafe_unretained _Tp*
122 addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
123 {
124   return &__x;
125 }
126 #endif
127
128 #ifdef _LIBCPP_HAS_NO_VARIADICS
129
130 #include <__functional_base_03>
131
132 #else  // _LIBCPP_HAS_NO_VARIADICS
133
134 // __weak_result_type
135
136 template <class _Tp>
137 struct __derives_from_unary_function
138 {
139 private:
140     struct __two {char __lx; char __lxx;};
141     static __two __test(...);
142     template <class _Ap, class _Rp>
143         static unary_function<_Ap, _Rp>
144         __test(const volatile unary_function<_Ap, _Rp>*);
145 public:
146     static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
147     typedef decltype(__test((_Tp*)0)) type;
148 };
149
150 template <class _Tp>
151 struct __derives_from_binary_function
152 {
153 private:
154     struct __two {char __lx; char __lxx;};
155     static __two __test(...);
156     template <class _A1, class _A2, class _Rp>
157         static binary_function<_A1, _A2, _Rp>
158         __test(const volatile binary_function<_A1, _A2, _Rp>*);
159 public:
160     static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
161     typedef decltype(__test((_Tp*)0)) type;
162 };
163
164 template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
165 struct __maybe_derive_from_unary_function  // bool is true
166     : public __derives_from_unary_function<_Tp>::type
167 {
168 };
169
170 template <class _Tp>
171 struct __maybe_derive_from_unary_function<_Tp, false>
172 {
173 };
174
175 template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
176 struct __maybe_derive_from_binary_function  // bool is true
177     : public __derives_from_binary_function<_Tp>::type
178 {
179 };
180
181 template <class _Tp>
182 struct __maybe_derive_from_binary_function<_Tp, false>
183 {
184 };
185
186 template <class _Tp, bool = __has_result_type<_Tp>::value>
187 struct __weak_result_type_imp // bool is true
188     : public __maybe_derive_from_unary_function<_Tp>,
189       public __maybe_derive_from_binary_function<_Tp>
190 {
191     typedef typename _Tp::result_type result_type;
192 };
193
194 template <class _Tp>
195 struct __weak_result_type_imp<_Tp, false>
196     : public __maybe_derive_from_unary_function<_Tp>,
197       public __maybe_derive_from_binary_function<_Tp>
198 {
199 };
200
201 template <class _Tp>
202 struct __weak_result_type
203     : public __weak_result_type_imp<_Tp>
204 {
205 };
206
207 // 0 argument case
208
209 template <class _Rp>
210 struct __weak_result_type<_Rp ()>
211 {
212     typedef _Rp result_type;
213 };
214
215 template <class _Rp>
216 struct __weak_result_type<_Rp (&)()>
217 {
218     typedef _Rp result_type;
219 };
220
221 template <class _Rp>
222 struct __weak_result_type<_Rp (*)()>
223 {
224     typedef _Rp result_type;
225 };
226
227 // 1 argument case
228
229 template <class _Rp, class _A1>
230 struct __weak_result_type<_Rp (_A1)>
231     : public unary_function<_A1, _Rp>
232 {
233 };
234
235 template <class _Rp, class _A1>
236 struct __weak_result_type<_Rp (&)(_A1)>
237     : public unary_function<_A1, _Rp>
238 {
239 };
240
241 template <class _Rp, class _A1>
242 struct __weak_result_type<_Rp (*)(_A1)>
243     : public unary_function<_A1, _Rp>
244 {
245 };
246
247 template <class _Rp, class _Cp>
248 struct __weak_result_type<_Rp (_Cp::*)()>
249     : public unary_function<_Cp*, _Rp>
250 {
251 };
252
253 template <class _Rp, class _Cp>
254 struct __weak_result_type<_Rp (_Cp::*)() const>
255     : public unary_function<const _Cp*, _Rp>
256 {
257 };
258
259 template <class _Rp, class _Cp>
260 struct __weak_result_type<_Rp (_Cp::*)() volatile>
261     : public unary_function<volatile _Cp*, _Rp>
262 {
263 };
264
265 template <class _Rp, class _Cp>
266 struct __weak_result_type<_Rp (_Cp::*)() const volatile>
267     : public unary_function<const volatile _Cp*, _Rp>
268 {
269 };
270
271 // 2 argument case
272
273 template <class _Rp, class _A1, class _A2>
274 struct __weak_result_type<_Rp (_A1, _A2)>
275     : public binary_function<_A1, _A2, _Rp>
276 {
277 };
278
279 template <class _Rp, class _A1, class _A2>
280 struct __weak_result_type<_Rp (*)(_A1, _A2)>
281     : public binary_function<_A1, _A2, _Rp>
282 {
283 };
284
285 template <class _Rp, class _A1, class _A2>
286 struct __weak_result_type<_Rp (&)(_A1, _A2)>
287     : public binary_function<_A1, _A2, _Rp>
288 {
289 };
290
291 template <class _Rp, class _Cp, class _A1>
292 struct __weak_result_type<_Rp (_Cp::*)(_A1)>
293     : public binary_function<_Cp*, _A1, _Rp>
294 {
295 };
296
297 template <class _Rp, class _Cp, class _A1>
298 struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
299     : public binary_function<const _Cp*, _A1, _Rp>
300 {
301 };
302
303 template <class _Rp, class _Cp, class _A1>
304 struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
305     : public binary_function<volatile _Cp*, _A1, _Rp>
306 {
307 };
308
309 template <class _Rp, class _Cp, class _A1>
310 struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
311     : public binary_function<const volatile _Cp*, _A1, _Rp>
312 {
313 };
314
315 // 3 or more arguments
316
317 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
318 struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
319 {
320     typedef _Rp result_type;
321 };
322
323 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
324 struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
325 {
326     typedef _Rp result_type;
327 };
328
329 template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
330 struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
331 {
332     typedef _Rp result_type;
333 };
334
335 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
336 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
337 {
338     typedef _Rp result_type;
339 };
340
341 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
342 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
343 {
344     typedef _Rp result_type;
345 };
346
347 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
348 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
349 {
350     typedef _Rp result_type;
351 };
352
353 template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
354 struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
355 {
356     typedef _Rp result_type;
357 };
358
359 // __invoke
360
361 // bullets 1 and 2
362
363 template <class _Fp, class _A0, class ..._Args,
364             class>
365 inline _LIBCPP_INLINE_VISIBILITY
366 auto
367 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
368     -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
369 {
370     return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
371 }
372
373 template <class _Fp, class _A0, class ..._Args,
374             class>
375 inline _LIBCPP_INLINE_VISIBILITY
376 auto
377 __invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
378     -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
379 {
380     return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
381 }
382
383 // bullets 3 and 4
384
385 template <class _Fp, class _A0,
386             class>
387 inline _LIBCPP_INLINE_VISIBILITY
388 auto
389 __invoke(_Fp&& __f, _A0&& __a0)
390     -> decltype(_VSTD::forward<_A0>(__a0).*__f)
391 {
392     return _VSTD::forward<_A0>(__a0).*__f;
393 }
394
395 template <class _Fp, class _A0,
396             class>
397 inline _LIBCPP_INLINE_VISIBILITY
398 auto
399 __invoke(_Fp&& __f, _A0&& __a0)
400     -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
401 {
402     return (*_VSTD::forward<_A0>(__a0)).*__f;
403 }
404
405 // bullet 5
406
407 template <class _Fp, class ..._Args>
408 inline _LIBCPP_INLINE_VISIBILITY
409 auto
410 __invoke(_Fp&& __f, _Args&& ...__args)
411     -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
412 {
413     return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
414 }
415
416 template <class _Tp, class ..._Args>
417 struct __invoke_return
418 {
419     typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
420 };
421
422 template <class _Ret>
423 struct __invoke_void_return_wrapper
424 {
425     template <class ..._Args>
426     static _Ret __call(_Args&&... __args)
427     {
428         return __invoke(_VSTD::forward<_Args>(__args)...);
429     }
430 };
431
432 template <>
433 struct __invoke_void_return_wrapper<void>
434 {
435     template <class ..._Args>
436     static void __call(_Args&&... __args)
437     {
438         __invoke(_VSTD::forward<_Args>(__args)...);
439     }
440 };
441
442 template <class _Tp>
443 class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
444     : public __weak_result_type<_Tp>
445 {
446 public:
447     // types
448     typedef _Tp type;
449 private:
450     type* __f_;
451
452 public:
453     // construct/copy/destroy
454     _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
455         : __f_(_VSTD::addressof(__f)) {}
456 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
457     private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
458 #endif
459
460     // access
461     _LIBCPP_INLINE_VISIBILITY operator type&    () const _NOEXCEPT {return *__f_;}
462     _LIBCPP_INLINE_VISIBILITY          type& get() const _NOEXCEPT {return *__f_;}
463
464     // invoke
465     template <class... _ArgTypes>
466        _LIBCPP_INLINE_VISIBILITY
467        typename __invoke_of<type&, _ArgTypes...>::type
468           operator() (_ArgTypes&&... __args) const
469           {
470               return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
471           }
472 };
473
474 template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
475 template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
476 template <class _Tp> struct __is_reference_wrapper
477     : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
478
479 template <class _Tp>
480 inline _LIBCPP_INLINE_VISIBILITY
481 reference_wrapper<_Tp>
482 ref(_Tp& __t) _NOEXCEPT
483 {
484     return reference_wrapper<_Tp>(__t);
485 }
486
487 template <class _Tp>
488 inline _LIBCPP_INLINE_VISIBILITY
489 reference_wrapper<_Tp>
490 ref(reference_wrapper<_Tp> __t) _NOEXCEPT
491 {
492     return ref(__t.get());
493 }
494
495 template <class _Tp>
496 inline _LIBCPP_INLINE_VISIBILITY
497 reference_wrapper<const _Tp>
498 cref(const _Tp& __t) _NOEXCEPT
499 {
500     return reference_wrapper<const _Tp>(__t);
501 }
502
503 template <class _Tp>
504 inline _LIBCPP_INLINE_VISIBILITY
505 reference_wrapper<const _Tp>
506 cref(reference_wrapper<_Tp> __t) _NOEXCEPT
507 {
508     return cref(__t.get());
509 }
510
511 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
512 #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
513
514 template <class _Tp> void ref(const _Tp&&) = delete;
515 template <class _Tp> void cref(const _Tp&&) = delete;
516
517 #else  // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
518
519 template <class _Tp> void ref(const _Tp&&);// = delete;
520 template <class _Tp> void cref(const _Tp&&);// = delete;
521
522 #endif  // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
523
524 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
525
526 #endif  // _LIBCPP_HAS_NO_VARIADICS
527
528 #if _LIBCPP_STD_VER > 11
529 template <class _Tp1, class _Tp2 = void>
530 struct __is_transparent
531 {
532 private:
533     struct __two {char __lx; char __lxx;};
534     template <class _Up> static __two __test(...);
535     template <class _Up> static char __test(typename _Up::is_transparent* = 0);
536 public:
537     static const bool value = sizeof(__test<_Tp1>(0)) == 1;
538 };
539 #endif
540
541 // allocator_arg_t
542
543 struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
544
545 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
546 extern const allocator_arg_t allocator_arg;
547 #else
548 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
549 #endif
550
551 // uses_allocator
552
553 template <class _Tp>
554 struct __has_allocator_type
555 {
556 private:
557     struct __two {char __lx; char __lxx;};
558     template <class _Up> static __two __test(...);
559     template <class _Up> static char __test(typename _Up::allocator_type* = 0);
560 public:
561     static const bool value = sizeof(__test<_Tp>(0)) == 1;
562 };
563
564 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
565 struct __uses_allocator
566     : public integral_constant<bool,
567         is_convertible<_Alloc, typename _Tp::allocator_type>::value>
568 {
569 };
570
571 template <class _Tp, class _Alloc>
572 struct __uses_allocator<_Tp, _Alloc, false>
573     : public false_type
574 {
575 };
576
577 template <class _Tp, class _Alloc>
578 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
579     : public __uses_allocator<_Tp, _Alloc>
580 {
581 };
582
583 #ifndef _LIBCPP_HAS_NO_VARIADICS
584
585 // allocator construction
586
587 template <class _Tp, class _Alloc, class ..._Args>
588 struct __uses_alloc_ctor_imp
589 {
590     static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
591     static const bool __ic =
592         is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
593     static const int value = __ua ? 2 - __ic : 0;
594 };
595
596 template <class _Tp, class _Alloc, class ..._Args>
597 struct __uses_alloc_ctor
598     : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
599     {};
600
601 template <class _Tp, class _Allocator, class... _Args>
602 inline _LIBCPP_INLINE_VISIBILITY
603 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
604 {
605     new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
606 }
607
608 template <class _Tp, class _Allocator, class... _Args>
609 inline _LIBCPP_INLINE_VISIBILITY
610 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
611 {
612     new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
613 }
614
615 template <class _Tp, class _Allocator, class... _Args>
616 inline _LIBCPP_INLINE_VISIBILITY
617 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
618 {
619     new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
620 }
621
622 template <class _Tp, class _Allocator, class... _Args>
623 inline _LIBCPP_INLINE_VISIBILITY
624 void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
625
626     __user_alloc_construct_impl( 
627              __uses_alloc_ctor<_Tp, _Allocator>(), 
628              __storage, __a, _VSTD::forward<_Args>(__args)...
629         );
630 }
631 #endif  // _LIBCPP_HAS_NO_VARIADICS
632
633 _LIBCPP_END_NAMESPACE_STD
634
635 #endif  // _LIBCPP_FUNCTIONAL_BASE