blob: 64bc93116f0893542acbce5df3f4bfafab149334 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
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>
Marshall Clow599e60d2013-09-12 02:11:16 +000018#include <new>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
Howard Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26template <class _Arg, class _Result>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000027struct _LIBCPP_TYPE_VIS_ONLY unary_function
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028{
29 typedef _Arg argument_type;
30 typedef _Result result_type;
31};
32
33template <class _Arg1, class _Arg2, class _Result>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000034struct _LIBCPP_TYPE_VIS_ONLY binary_function
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035{
36 typedef _Arg1 first_argument_type;
37 typedef _Arg2 second_argument_type;
38 typedef _Result result_type;
39};
40
Howard Hinnant0f678bd2013-08-12 18:38:34 +000041template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042
43template <class _Tp>
44struct __has_result_type
45{
46private:
Howard Hinnant9c0df142012-10-30 19:06:59 +000047 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 template <class _Up> static __two __test(...);
49 template <class _Up> static char __test(typename _Up::result_type* = 0);
50public:
51 static const bool value = sizeof(__test<_Tp>(0)) == 1;
52};
53
Marshall Clowff464092013-07-29 14:21:53 +000054#if _LIBCPP_STD_VER > 11
55template <class _Tp = void>
56#else
Howard Hinnant3fadda32012-02-21 21:02:58 +000057template <class _Tp>
Marshall Clowff464092013-07-29 14:21:53 +000058#endif
Howard Hinnant0f678bd2013-08-12 18:38:34 +000059struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool>
Howard Hinnant3fadda32012-02-21 21:02:58 +000060{
Marshall Clow9738caf2013-09-28 19:06:12 +000061 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
62 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnant3fadda32012-02-21 21:02:58 +000063 {return __x < __y;}
64};
65
Marshall Clowff464092013-07-29 14:21:53 +000066#if _LIBCPP_STD_VER > 11
67template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000068struct _LIBCPP_TYPE_VIS_ONLY less<void>
Marshall Clowff464092013-07-29 14:21:53 +000069{
Marshall Clow9738caf2013-09-28 19:06:12 +000070 template <class _T1, class _T2>
71 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clowff464092013-07-29 14:21:53 +000072 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow59ac38c2015-02-25 12:20:52 +000073 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u)))
74 -> decltype (_VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u))
75 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
Marshall Clow4a0a9812013-08-13 01:11:06 +000076 typedef void is_transparent;
Marshall Clowff464092013-07-29 14:21:53 +000077};
78#endif
79
Howard Hinnanta4e87ab2013-08-08 18:38:55 +000080// addressof
81
82template <class _Tp>
83inline _LIBCPP_INLINE_VISIBILITY
84_Tp*
85addressof(_Tp& __x) _NOEXCEPT
86{
87 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
88}
89
90#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
91// Objective-C++ Automatic Reference Counting uses qualified pointers
92// that require special addressof() signatures. When
93// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
94// itself is providing these definitions. Otherwise, we provide them.
95template <class _Tp>
96inline _LIBCPP_INLINE_VISIBILITY
97__strong _Tp*
98addressof(__strong _Tp& __x) _NOEXCEPT
99{
100 return &__x;
101}
102
103#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
104template <class _Tp>
105inline _LIBCPP_INLINE_VISIBILITY
106__weak _Tp*
107addressof(__weak _Tp& __x) _NOEXCEPT
108{
109 return &__x;
110}
111#endif
112
113template <class _Tp>
114inline _LIBCPP_INLINE_VISIBILITY
115__autoreleasing _Tp*
116addressof(__autoreleasing _Tp& __x) _NOEXCEPT
117{
118 return &__x;
119}
120
121template <class _Tp>
122inline _LIBCPP_INLINE_VISIBILITY
123__unsafe_unretained _Tp*
124addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
125{
126 return &__x;
127}
128#endif
129
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130#ifdef _LIBCPP_HAS_NO_VARIADICS
131
132#include <__functional_base_03>
133
134#else // _LIBCPP_HAS_NO_VARIADICS
135
136// __weak_result_type
137
138template <class _Tp>
139struct __derives_from_unary_function
140{
141private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000142 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000144 template <class _Ap, class _Rp>
145 static unary_function<_Ap, _Rp>
146 __test(const volatile unary_function<_Ap, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147public:
148 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
149 typedef decltype(__test((_Tp*)0)) type;
150};
151
152template <class _Tp>
153struct __derives_from_binary_function
154{
155private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000156 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000158 template <class _A1, class _A2, class _Rp>
159 static binary_function<_A1, _A2, _Rp>
160 __test(const volatile binary_function<_A1, _A2, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161public:
162 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
163 typedef decltype(__test((_Tp*)0)) type;
164};
165
166template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
167struct __maybe_derive_from_unary_function // bool is true
168 : public __derives_from_unary_function<_Tp>::type
169{
170};
171
172template <class _Tp>
173struct __maybe_derive_from_unary_function<_Tp, false>
174{
175};
176
177template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
178struct __maybe_derive_from_binary_function // bool is true
179 : public __derives_from_binary_function<_Tp>::type
180{
181};
182
183template <class _Tp>
184struct __maybe_derive_from_binary_function<_Tp, false>
185{
186};
187
188template <class _Tp, bool = __has_result_type<_Tp>::value>
189struct __weak_result_type_imp // bool is true
190 : public __maybe_derive_from_unary_function<_Tp>,
191 public __maybe_derive_from_binary_function<_Tp>
192{
193 typedef typename _Tp::result_type result_type;
194};
195
196template <class _Tp>
197struct __weak_result_type_imp<_Tp, false>
198 : public __maybe_derive_from_unary_function<_Tp>,
199 public __maybe_derive_from_binary_function<_Tp>
200{
201};
202
203template <class _Tp>
204struct __weak_result_type
205 : public __weak_result_type_imp<_Tp>
206{
207};
208
209// 0 argument case
210
Howard Hinnant99968442011-11-29 18:15:50 +0000211template <class _Rp>
212struct __weak_result_type<_Rp ()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213{
Howard Hinnant99968442011-11-29 18:15:50 +0000214 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215};
216
Howard Hinnant99968442011-11-29 18:15:50 +0000217template <class _Rp>
218struct __weak_result_type<_Rp (&)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
Howard Hinnant99968442011-11-29 18:15:50 +0000220 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221};
222
Howard Hinnant99968442011-11-29 18:15:50 +0000223template <class _Rp>
224struct __weak_result_type<_Rp (*)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225{
Howard Hinnant99968442011-11-29 18:15:50 +0000226 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227};
228
229// 1 argument case
230
Howard Hinnant99968442011-11-29 18:15:50 +0000231template <class _Rp, class _A1>
232struct __weak_result_type<_Rp (_A1)>
233 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000234{
235};
236
Howard Hinnant99968442011-11-29 18:15:50 +0000237template <class _Rp, class _A1>
238struct __weak_result_type<_Rp (&)(_A1)>
239 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240{
241};
242
Howard Hinnant99968442011-11-29 18:15:50 +0000243template <class _Rp, class _A1>
244struct __weak_result_type<_Rp (*)(_A1)>
245 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246{
247};
248
Howard Hinnant99968442011-11-29 18:15:50 +0000249template <class _Rp, class _Cp>
250struct __weak_result_type<_Rp (_Cp::*)()>
251 : public unary_function<_Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252{
253};
254
Howard Hinnant99968442011-11-29 18:15:50 +0000255template <class _Rp, class _Cp>
256struct __weak_result_type<_Rp (_Cp::*)() const>
257 : public unary_function<const _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258{
259};
260
Howard Hinnant99968442011-11-29 18:15:50 +0000261template <class _Rp, class _Cp>
262struct __weak_result_type<_Rp (_Cp::*)() volatile>
263 : public unary_function<volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264{
265};
266
Howard Hinnant99968442011-11-29 18:15:50 +0000267template <class _Rp, class _Cp>
268struct __weak_result_type<_Rp (_Cp::*)() const volatile>
269 : public unary_function<const volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270{
271};
272
273// 2 argument case
274
Howard Hinnant99968442011-11-29 18:15:50 +0000275template <class _Rp, class _A1, class _A2>
276struct __weak_result_type<_Rp (_A1, _A2)>
277 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278{
279};
280
Howard Hinnant99968442011-11-29 18:15:50 +0000281template <class _Rp, class _A1, class _A2>
282struct __weak_result_type<_Rp (*)(_A1, _A2)>
283 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284{
285};
286
Howard Hinnant99968442011-11-29 18:15:50 +0000287template <class _Rp, class _A1, class _A2>
288struct __weak_result_type<_Rp (&)(_A1, _A2)>
289 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290{
291};
292
Howard Hinnant99968442011-11-29 18:15:50 +0000293template <class _Rp, class _Cp, class _A1>
294struct __weak_result_type<_Rp (_Cp::*)(_A1)>
295 : public binary_function<_Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296{
297};
298
Howard Hinnant99968442011-11-29 18:15:50 +0000299template <class _Rp, class _Cp, class _A1>
300struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
301 : public binary_function<const _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302{
303};
304
Howard Hinnant99968442011-11-29 18:15:50 +0000305template <class _Rp, class _Cp, class _A1>
306struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
307 : public binary_function<volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308{
309};
310
Howard Hinnant99968442011-11-29 18:15:50 +0000311template <class _Rp, class _Cp, class _A1>
312struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
313 : public binary_function<const volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000314{
315};
316
317// 3 or more arguments
318
Howard Hinnant99968442011-11-29 18:15:50 +0000319template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
320struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321{
Howard Hinnant99968442011-11-29 18:15:50 +0000322 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323};
324
Howard Hinnant99968442011-11-29 18:15:50 +0000325template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
326struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327{
Howard Hinnant99968442011-11-29 18:15:50 +0000328 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329};
330
Howard Hinnant99968442011-11-29 18:15:50 +0000331template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
332struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333{
Howard Hinnant99968442011-11-29 18:15:50 +0000334 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335};
336
Howard Hinnant99968442011-11-29 18:15:50 +0000337template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
338struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339{
Howard Hinnant99968442011-11-29 18:15:50 +0000340 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341};
342
Howard Hinnant99968442011-11-29 18:15:50 +0000343template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
344struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345{
Howard Hinnant99968442011-11-29 18:15:50 +0000346 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347};
348
Howard Hinnant99968442011-11-29 18:15:50 +0000349template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
350struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351{
Howard Hinnant99968442011-11-29 18:15:50 +0000352 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353};
354
Howard Hinnant99968442011-11-29 18:15:50 +0000355template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
356struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357{
Howard Hinnant99968442011-11-29 18:15:50 +0000358 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359};
360
361// __invoke
362
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000363// bullets 1 and 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
Howard Hinnantecc97422013-05-07 23:40:12 +0000365template <class _Fp, class _A0, class ..._Args,
366 class>
Eric Fiselier13858ee2015-03-17 18:28:14 +0000367inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000368auto
Howard Hinnant99968442011-11-29 18:15:50 +0000369__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000370 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000372 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373}
374
Howard Hinnantecc97422013-05-07 23:40:12 +0000375template <class _Fp, class _A0, class ..._Args,
376 class>
Eric Fiselier13858ee2015-03-17 18:28:14 +0000377inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000378auto
Howard Hinnant99968442011-11-29 18:15:50 +0000379__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000380 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000382 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383}
384
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000385// bullets 3 and 4
386
Howard Hinnantecc97422013-05-07 23:40:12 +0000387template <class _Fp, class _A0,
388 class>
Eric Fiselier13858ee2015-03-17 18:28:14 +0000389inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000390auto
Howard Hinnant99968442011-11-29 18:15:50 +0000391__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000392 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000394 return _VSTD::forward<_A0>(__a0).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395}
396
Howard Hinnantecc97422013-05-07 23:40:12 +0000397template <class _Fp, class _A0,
398 class>
Eric Fiselier13858ee2015-03-17 18:28:14 +0000399inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000400auto
Howard Hinnant99968442011-11-29 18:15:50 +0000401__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000402 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000404 return (*_VSTD::forward<_A0>(__a0)).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405}
406
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000407// bullet 5
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408
Howard Hinnant99968442011-11-29 18:15:50 +0000409template <class _Fp, class ..._Args>
Eric Fiselier13858ee2015-03-17 18:28:14 +0000410inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000411auto
Howard Hinnant99968442011-11-29 18:15:50 +0000412__invoke(_Fp&& __f, _Args&& ...__args)
413 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414{
Howard Hinnant99968442011-11-29 18:15:50 +0000415 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416}
417
418template <class _Tp, class ..._Args>
419struct __invoke_return
420{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000421 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422};
423
Eric Fiselierc3231d22015-02-10 16:48:45 +0000424template <class _Ret>
425struct __invoke_void_return_wrapper
426{
427 template <class ..._Args>
428 static _Ret __call(_Args&&... __args)
429 {
430 return __invoke(_VSTD::forward<_Args>(__args)...);
431 }
432};
433
434template <>
435struct __invoke_void_return_wrapper<void>
436{
437 template <class ..._Args>
438 static void __call(_Args&&... __args)
439 {
440 __invoke(_VSTD::forward<_Args>(__args)...);
441 }
442};
443
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000445class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446 : public __weak_result_type<_Tp>
447{
448public:
449 // types
450 typedef _Tp type;
451private:
452 type* __f_;
453
454public:
455 // construct/copy/destroy
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000456 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
457 : __f_(_VSTD::addressof(__f)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000458#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
460#endif
461
462 // access
Howard Hinnant603d2c02011-05-28 17:59:48 +0000463 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
464 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465
466 // invoke
467 template <class... _ArgTypes>
Howard Hinnant99acc502010-09-21 17:32:39 +0000468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +0000469 typename __invoke_of<type&, _ArgTypes...>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 operator() (_ArgTypes&&... __args) const
471 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000472 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473 }
474};
475
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000476template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
477template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478template <class _Tp> struct __is_reference_wrapper
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000479 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480
481template <class _Tp>
482inline _LIBCPP_INLINE_VISIBILITY
483reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000484ref(_Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485{
486 return reference_wrapper<_Tp>(__t);
487}
488
489template <class _Tp>
490inline _LIBCPP_INLINE_VISIBILITY
491reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000492ref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493{
494 return ref(__t.get());
495}
496
497template <class _Tp>
498inline _LIBCPP_INLINE_VISIBILITY
499reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000500cref(const _Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501{
502 return reference_wrapper<const _Tp>(__t);
503}
504
505template <class _Tp>
506inline _LIBCPP_INLINE_VISIBILITY
507reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000508cref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509{
510 return cref(__t.get());
511}
512
Howard Hinnant73d21a42010-09-04 23:28:19 +0000513#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
514#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
515
Howard Hinnantec3773c2011-12-01 20:21:04 +0000516template <class _Tp> void ref(const _Tp&&) = delete;
517template <class _Tp> void cref(const _Tp&&) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000518
519#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
520
Howard Hinnantec3773c2011-12-01 20:21:04 +0000521template <class _Tp> void ref(const _Tp&&);// = delete;
522template <class _Tp> void cref(const _Tp&&);// = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000523
524#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
525
526#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527
528#endif // _LIBCPP_HAS_NO_VARIADICS
529
Marshall Clow4a0a9812013-08-13 01:11:06 +0000530#if _LIBCPP_STD_VER > 11
531template <class _Tp1, class _Tp2 = void>
532struct __is_transparent
533{
534private:
535 struct __two {char __lx; char __lxx;};
536 template <class _Up> static __two __test(...);
537 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
538public:
539 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
540};
541#endif
542
Marshall Clow599e60d2013-09-12 02:11:16 +0000543// allocator_arg_t
544
545struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
546
547#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
548extern const allocator_arg_t allocator_arg;
549#else
550constexpr allocator_arg_t allocator_arg = allocator_arg_t();
551#endif
552
553// uses_allocator
554
555template <class _Tp>
556struct __has_allocator_type
557{
558private:
559 struct __two {char __lx; char __lxx;};
560 template <class _Up> static __two __test(...);
561 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
562public:
563 static const bool value = sizeof(__test<_Tp>(0)) == 1;
564};
565
566template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
567struct __uses_allocator
568 : public integral_constant<bool,
569 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
570{
571};
572
573template <class _Tp, class _Alloc>
574struct __uses_allocator<_Tp, _Alloc, false>
575 : public false_type
576{
577};
578
579template <class _Tp, class _Alloc>
580struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
581 : public __uses_allocator<_Tp, _Alloc>
582{
583};
584
585#ifndef _LIBCPP_HAS_NO_VARIADICS
586
587// allocator construction
588
589template <class _Tp, class _Alloc, class ..._Args>
590struct __uses_alloc_ctor_imp
591{
592 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
593 static const bool __ic =
594 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
595 static const int value = __ua ? 2 - __ic : 0;
596};
597
598template <class _Tp, class _Alloc, class ..._Args>
599struct __uses_alloc_ctor
600 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
601 {};
602
603template <class _Tp, class _Allocator, class... _Args>
604inline _LIBCPP_INLINE_VISIBILITY
605void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
606{
607 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
608}
609
610template <class _Tp, class _Allocator, class... _Args>
611inline _LIBCPP_INLINE_VISIBILITY
612void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
613{
614 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
615}
616
617template <class _Tp, class _Allocator, class... _Args>
618inline _LIBCPP_INLINE_VISIBILITY
619void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
620{
621 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
622}
623
624template <class _Tp, class _Allocator, class... _Args>
625inline _LIBCPP_INLINE_VISIBILITY
626void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
627{
628 __user_alloc_construct_impl(
629 __uses_alloc_ctor<_Tp, _Allocator>(),
630 __storage, __a, _VSTD::forward<_Args>(__args)...
631 );
632}
633#endif // _LIBCPP_HAS_NO_VARIADICS
Marshall Clow4a0a9812013-08-13 01:11:06 +0000634
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635_LIBCPP_END_NAMESPACE_STD
636
637#endif // _LIBCPP_FUNCTIONAL_BASE