blob: ef9cc03235b5d406e064d0df3f693f6d76312e38 [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
131// __weak_result_type
132
133template <class _Tp>
134struct __derives_from_unary_function
135{
136private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000137 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000138 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000139 template <class _Ap, class _Rp>
140 static unary_function<_Ap, _Rp>
141 __test(const volatile unary_function<_Ap, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000142public:
143 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
144 typedef decltype(__test((_Tp*)0)) type;
145};
146
147template <class _Tp>
148struct __derives_from_binary_function
149{
150private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000151 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000153 template <class _A1, class _A2, class _Rp>
154 static binary_function<_A1, _A2, _Rp>
155 __test(const volatile binary_function<_A1, _A2, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156public:
157 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
158 typedef decltype(__test((_Tp*)0)) type;
159};
160
161template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
162struct __maybe_derive_from_unary_function // bool is true
163 : public __derives_from_unary_function<_Tp>::type
164{
165};
166
167template <class _Tp>
168struct __maybe_derive_from_unary_function<_Tp, false>
169{
170};
171
172template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
173struct __maybe_derive_from_binary_function // bool is true
174 : public __derives_from_binary_function<_Tp>::type
175{
176};
177
178template <class _Tp>
179struct __maybe_derive_from_binary_function<_Tp, false>
180{
181};
182
183template <class _Tp, bool = __has_result_type<_Tp>::value>
184struct __weak_result_type_imp // bool is true
185 : public __maybe_derive_from_unary_function<_Tp>,
186 public __maybe_derive_from_binary_function<_Tp>
187{
188 typedef typename _Tp::result_type result_type;
189};
190
191template <class _Tp>
192struct __weak_result_type_imp<_Tp, false>
193 : public __maybe_derive_from_unary_function<_Tp>,
194 public __maybe_derive_from_binary_function<_Tp>
195{
196};
197
198template <class _Tp>
199struct __weak_result_type
200 : public __weak_result_type_imp<_Tp>
201{
202};
203
204// 0 argument case
205
Howard Hinnant99968442011-11-29 18:15:50 +0000206template <class _Rp>
207struct __weak_result_type<_Rp ()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208{
Howard Hinnant99968442011-11-29 18:15:50 +0000209 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210};
211
Howard Hinnant99968442011-11-29 18:15:50 +0000212template <class _Rp>
213struct __weak_result_type<_Rp (&)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214{
Howard Hinnant99968442011-11-29 18:15:50 +0000215 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216};
217
Howard Hinnant99968442011-11-29 18:15:50 +0000218template <class _Rp>
219struct __weak_result_type<_Rp (*)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220{
Howard Hinnant99968442011-11-29 18:15:50 +0000221 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222};
223
224// 1 argument case
225
Howard Hinnant99968442011-11-29 18:15:50 +0000226template <class _Rp, class _A1>
227struct __weak_result_type<_Rp (_A1)>
228 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229{
230};
231
Howard Hinnant99968442011-11-29 18:15:50 +0000232template <class _Rp, class _A1>
233struct __weak_result_type<_Rp (&)(_A1)>
234 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235{
236};
237
Howard Hinnant99968442011-11-29 18:15:50 +0000238template <class _Rp, class _A1>
239struct __weak_result_type<_Rp (*)(_A1)>
240 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241{
242};
243
Howard Hinnant99968442011-11-29 18:15:50 +0000244template <class _Rp, class _Cp>
245struct __weak_result_type<_Rp (_Cp::*)()>
246 : public unary_function<_Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247{
248};
249
Howard Hinnant99968442011-11-29 18:15:50 +0000250template <class _Rp, class _Cp>
251struct __weak_result_type<_Rp (_Cp::*)() const>
252 : public unary_function<const _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253{
254};
255
Howard Hinnant99968442011-11-29 18:15:50 +0000256template <class _Rp, class _Cp>
257struct __weak_result_type<_Rp (_Cp::*)() volatile>
258 : public unary_function<volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259{
260};
261
Howard Hinnant99968442011-11-29 18:15:50 +0000262template <class _Rp, class _Cp>
263struct __weak_result_type<_Rp (_Cp::*)() const volatile>
264 : public unary_function<const volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265{
266};
267
268// 2 argument case
269
Howard Hinnant99968442011-11-29 18:15:50 +0000270template <class _Rp, class _A1, class _A2>
271struct __weak_result_type<_Rp (_A1, _A2)>
272 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273{
274};
275
Howard Hinnant99968442011-11-29 18:15:50 +0000276template <class _Rp, class _A1, class _A2>
277struct __weak_result_type<_Rp (*)(_A1, _A2)>
278 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279{
280};
281
Howard Hinnant99968442011-11-29 18:15:50 +0000282template <class _Rp, class _A1, class _A2>
283struct __weak_result_type<_Rp (&)(_A1, _A2)>
284 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285{
286};
287
Howard Hinnant99968442011-11-29 18:15:50 +0000288template <class _Rp, class _Cp, class _A1>
289struct __weak_result_type<_Rp (_Cp::*)(_A1)>
290 : public binary_function<_Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291{
292};
293
Howard Hinnant99968442011-11-29 18:15:50 +0000294template <class _Rp, class _Cp, class _A1>
295struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
296 : public binary_function<const _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297{
298};
299
Howard Hinnant99968442011-11-29 18:15:50 +0000300template <class _Rp, class _Cp, class _A1>
301struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
302 : public binary_function<volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303{
304};
305
Howard Hinnant99968442011-11-29 18:15:50 +0000306template <class _Rp, class _Cp, class _A1>
307struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
308 : public binary_function<const volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309{
310};
311
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000312
313#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000314// 3 or more arguments
315
Howard Hinnant99968442011-11-29 18:15:50 +0000316template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
317struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318{
Howard Hinnant99968442011-11-29 18:15:50 +0000319 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000320};
321
Howard Hinnant99968442011-11-29 18:15:50 +0000322template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
323struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000324{
Howard Hinnant99968442011-11-29 18:15:50 +0000325 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326};
327
Howard Hinnant99968442011-11-29 18:15:50 +0000328template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
329struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330{
Howard Hinnant99968442011-11-29 18:15:50 +0000331 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332};
333
Howard Hinnant99968442011-11-29 18:15:50 +0000334template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
335struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336{
Howard Hinnant99968442011-11-29 18:15:50 +0000337 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000338};
339
Howard Hinnant99968442011-11-29 18:15:50 +0000340template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
341struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000342{
Howard Hinnant99968442011-11-29 18:15:50 +0000343 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344};
345
Howard Hinnant99968442011-11-29 18:15:50 +0000346template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
347struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348{
Howard Hinnant99968442011-11-29 18:15:50 +0000349 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350};
351
Howard Hinnant99968442011-11-29 18:15:50 +0000352template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
353struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354{
Howard Hinnant99968442011-11-29 18:15:50 +0000355 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356};
357
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000358#endif // _LIBCPP_HAS_NO_VARIADICS
359
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360// __invoke
361
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000362#ifndef _LIBCPP_HAS_NO_VARIADICS
363
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000364// bullets 1 and 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365
Howard Hinnantecc97422013-05-07 23:40:12 +0000366template <class _Fp, class _A0, class ..._Args,
367 class>
Eric Fiselierc254b362015-04-19 15:32:52 +0000368inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000369auto
Howard Hinnant99968442011-11-29 18:15:50 +0000370__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000371 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000373 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374}
375
Howard Hinnantecc97422013-05-07 23:40:12 +0000376template <class _Fp, class _A0, class ..._Args,
377 class>
Eric Fiselierc254b362015-04-19 15:32:52 +0000378inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000379auto
Howard Hinnant99968442011-11-29 18:15:50 +0000380__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000381 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000383 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384}
385
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000386// bullets 3 and 4
387
Howard Hinnantecc97422013-05-07 23:40:12 +0000388template <class _Fp, class _A0,
389 class>
Eric Fiselierc254b362015-04-19 15:32:52 +0000390inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000391auto
Howard Hinnant99968442011-11-29 18:15:50 +0000392__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000393 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000395 return _VSTD::forward<_A0>(__a0).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396}
397
Howard Hinnantecc97422013-05-07 23:40:12 +0000398template <class _Fp, class _A0,
399 class>
Eric Fiselierc254b362015-04-19 15:32:52 +0000400inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000401auto
Howard Hinnant99968442011-11-29 18:15:50 +0000402__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000403 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000405 return (*_VSTD::forward<_A0>(__a0)).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406}
407
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000408// bullet 5
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409
Howard Hinnant99968442011-11-29 18:15:50 +0000410template <class _Fp, class ..._Args>
Eric Fiselierc254b362015-04-19 15:32:52 +0000411inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000412auto
Howard Hinnant99968442011-11-29 18:15:50 +0000413__invoke(_Fp&& __f, _Args&& ...__args)
414 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415{
Howard Hinnant99968442011-11-29 18:15:50 +0000416 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418template <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 Fiselier5b3a4592015-07-22 22:23:49 +0000424#else // _LIBCPP_HAS_NO_VARIADICS
425
426#include <__functional_base_03>
427
428#endif // _LIBCPP_HAS_NO_VARIADICS
429
430
Eric Fiselierc3231d22015-02-10 16:48:45 +0000431template <class _Ret>
432struct __invoke_void_return_wrapper
433{
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000434#ifndef _LIBCPP_HAS_NO_VARIADICS
Eric Fiselierc3231d22015-02-10 16:48:45 +0000435 template <class ..._Args>
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000436 static _Ret __call(_Args&&... __args) {
Eric Fiselierc3231d22015-02-10 16:48:45 +0000437 return __invoke(_VSTD::forward<_Args>(__args)...);
438 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000439#else
440 template <class _Fn>
441 static _Ret __call(_Fn __f) {
442 return __invoke(__f);
443 }
444
445 template <class _Fn, class _A0>
446 static _Ret __call(_Fn __f, _A0& __a0) {
447 return __invoke(__f, __a0);
448 }
449
450 template <class _Fn, class _A0, class _A1>
451 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1) {
452 return __invoke(__f, __a0, __a1);
453 }
454
455 template <class _Fn, class _A0, class _A1, class _A2>
456 static _Ret __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2){
457 return __invoke(__f, __a0, __a1, __a2);
458 }
459#endif
Eric Fiselierc3231d22015-02-10 16:48:45 +0000460};
461
462template <>
463struct __invoke_void_return_wrapper<void>
464{
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000465#ifndef _LIBCPP_HAS_NO_VARIADICS
Eric Fiselierc3231d22015-02-10 16:48:45 +0000466 template <class ..._Args>
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000467 static void __call(_Args&&... __args) {
Eric Fiselierc3231d22015-02-10 16:48:45 +0000468 __invoke(_VSTD::forward<_Args>(__args)...);
469 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000470#else
471 template <class _Fn>
472 static void __call(_Fn __f) {
473 __invoke(__f);
474 }
475
476 template <class _Fn, class _A0>
477 static void __call(_Fn __f, _A0& __a0) {
478 __invoke(__f, __a0);
479 }
480
481 template <class _Fn, class _A0, class _A1>
482 static void __call(_Fn __f, _A0& __a0, _A1& __a1) {
483 __invoke(__f, __a0, __a1);
484 }
485
486 template <class _Fn, class _A0, class _A1, class _A2>
487 static void __call(_Fn __f, _A0& __a0, _A1& __a1, _A2& __a2) {
488 __invoke(__f, __a0, __a1, __a2);
489 }
490#endif
Eric Fiselierc3231d22015-02-10 16:48:45 +0000491};
492
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000494class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495 : public __weak_result_type<_Tp>
496{
497public:
498 // types
499 typedef _Tp type;
500private:
501 type* __f_;
502
503public:
504 // construct/copy/destroy
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000505 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
506 : __f_(_VSTD::addressof(__f)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000507#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
509#endif
510
511 // access
Howard Hinnant603d2c02011-05-28 17:59:48 +0000512 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
513 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000515#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 // invoke
517 template <class... _ArgTypes>
Eric Fiselierf1626ad2015-08-26 20:15:02 +0000518 _LIBCPP_INLINE_VISIBILITY
519 typename __invoke_of<type&, _ArgTypes...>::type
520 operator() (_ArgTypes&&... __args) const {
521 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
522 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000523#else
524
525 _LIBCPP_INLINE_VISIBILITY
526 typename __invoke_return<type>::type
Eric Fiselierf1626ad2015-08-26 20:15:02 +0000527 operator() () const {
528 return __invoke(get());
529 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000530
531 template <class _A0>
Eric Fiselierf1626ad2015-08-26 20:15:02 +0000532 _LIBCPP_INLINE_VISIBILITY
533 typename __invoke_return0<type, _A0>::type
534 operator() (_A0& __a0) const {
535 return __invoke(get(), __a0);
536 }
537
538 template <class _A0>
539 _LIBCPP_INLINE_VISIBILITY
540 typename __invoke_return0<type, _A0 const>::type
541 operator() (_A0 const& __a0) const {
542 return __invoke(get(), __a0);
543 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000544
545 template <class _A0, class _A1>
Eric Fiselierf1626ad2015-08-26 20:15:02 +0000546 _LIBCPP_INLINE_VISIBILITY
547 typename __invoke_return1<type, _A0, _A1>::type
548 operator() (_A0& __a0, _A1& __a1) const {
549 return __invoke(get(), __a0, __a1);
550 }
551
552 template <class _A0, class _A1>
553 _LIBCPP_INLINE_VISIBILITY
554 typename __invoke_return1<type, _A0 const, _A1>::type
555 operator() (_A0 const& __a0, _A1& __a1) const {
556 return __invoke(get(), __a0, __a1);
557 }
558
559 template <class _A0, class _A1>
560 _LIBCPP_INLINE_VISIBILITY
561 typename __invoke_return1<type, _A0, _A1 const>::type
562 operator() (_A0& __a0, _A1 const& __a1) const {
563 return __invoke(get(), __a0, __a1);
564 }
565
566 template <class _A0, class _A1>
567 _LIBCPP_INLINE_VISIBILITY
568 typename __invoke_return1<type, _A0 const, _A1 const>::type
569 operator() (_A0 const& __a0, _A1 const& __a1) const {
570 return __invoke(get(), __a0, __a1);
571 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000572
573 template <class _A0, class _A1, class _A2>
Eric Fiselierf1626ad2015-08-26 20:15:02 +0000574 _LIBCPP_INLINE_VISIBILITY
575 typename __invoke_return2<type, _A0, _A1, _A2>::type
576 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
577 return __invoke(get(), __a0, __a1, __a2);
578 }
579
580 template <class _A0, class _A1, class _A2>
581 _LIBCPP_INLINE_VISIBILITY
582 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
583 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
584 return __invoke(get(), __a0, __a1, __a2);
585 }
586
587 template <class _A0, class _A1, class _A2>
588 _LIBCPP_INLINE_VISIBILITY
589 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
590 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
591 return __invoke(get(), __a0, __a1, __a2);
592 }
593
594 template <class _A0, class _A1, class _A2>
595 _LIBCPP_INLINE_VISIBILITY
596 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
597 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
598 return __invoke(get(), __a0, __a1, __a2);
599 }
600
601 template <class _A0, class _A1, class _A2>
602 _LIBCPP_INLINE_VISIBILITY
603 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
604 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
605 return __invoke(get(), __a0, __a1, __a2);
606 }
607
608 template <class _A0, class _A1, class _A2>
609 _LIBCPP_INLINE_VISIBILITY
610 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
611 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
612 return __invoke(get(), __a0, __a1, __a2);
613 }
614
615 template <class _A0, class _A1, class _A2>
616 _LIBCPP_INLINE_VISIBILITY
617 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
618 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
619 return __invoke(get(), __a0, __a1, __a2);
620 }
621
622 template <class _A0, class _A1, class _A2>
623 _LIBCPP_INLINE_VISIBILITY
624 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
625 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
626 return __invoke(get(), __a0, __a1, __a2);
627 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000628#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629};
630
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000631template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
632template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633template <class _Tp> struct __is_reference_wrapper
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000634 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635
636template <class _Tp>
637inline _LIBCPP_INLINE_VISIBILITY
638reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000639ref(_Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640{
641 return reference_wrapper<_Tp>(__t);
642}
643
644template <class _Tp>
645inline _LIBCPP_INLINE_VISIBILITY
646reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000647ref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648{
649 return ref(__t.get());
650}
651
652template <class _Tp>
653inline _LIBCPP_INLINE_VISIBILITY
654reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000655cref(const _Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656{
657 return reference_wrapper<const _Tp>(__t);
658}
659
660template <class _Tp>
661inline _LIBCPP_INLINE_VISIBILITY
662reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000663cref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664{
665 return cref(__t.get());
666}
667
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000668#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000669#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
670#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
671
Howard Hinnantec3773c2011-12-01 20:21:04 +0000672template <class _Tp> void ref(const _Tp&&) = delete;
673template <class _Tp> void cref(const _Tp&&) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000674
675#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
676
Howard Hinnantec3773c2011-12-01 20:21:04 +0000677template <class _Tp> void ref(const _Tp&&);// = delete;
678template <class _Tp> void cref(const _Tp&&);// = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000679
680#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
681
682#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683
684#endif // _LIBCPP_HAS_NO_VARIADICS
685
Marshall Clow4a0a9812013-08-13 01:11:06 +0000686#if _LIBCPP_STD_VER > 11
687template <class _Tp1, class _Tp2 = void>
688struct __is_transparent
689{
690private:
691 struct __two {char __lx; char __lxx;};
692 template <class _Up> static __two __test(...);
693 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
694public:
695 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
696};
697#endif
698
Marshall Clow599e60d2013-09-12 02:11:16 +0000699// allocator_arg_t
700
701struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
702
703#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
704extern const allocator_arg_t allocator_arg;
705#else
706constexpr allocator_arg_t allocator_arg = allocator_arg_t();
707#endif
708
709// uses_allocator
710
711template <class _Tp>
712struct __has_allocator_type
713{
714private:
715 struct __two {char __lx; char __lxx;};
716 template <class _Up> static __two __test(...);
717 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
718public:
719 static const bool value = sizeof(__test<_Tp>(0)) == 1;
720};
721
722template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
723struct __uses_allocator
724 : public integral_constant<bool,
725 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
726{
727};
728
729template <class _Tp, class _Alloc>
730struct __uses_allocator<_Tp, _Alloc, false>
731 : public false_type
732{
733};
734
735template <class _Tp, class _Alloc>
736struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
737 : public __uses_allocator<_Tp, _Alloc>
738{
739};
740
741#ifndef _LIBCPP_HAS_NO_VARIADICS
742
743// allocator construction
744
745template <class _Tp, class _Alloc, class ..._Args>
746struct __uses_alloc_ctor_imp
747{
748 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
749 static const bool __ic =
750 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
751 static const int value = __ua ? 2 - __ic : 0;
752};
753
754template <class _Tp, class _Alloc, class ..._Args>
755struct __uses_alloc_ctor
756 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
757 {};
758
759template <class _Tp, class _Allocator, class... _Args>
760inline _LIBCPP_INLINE_VISIBILITY
761void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
762{
763 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
764}
765
766template <class _Tp, class _Allocator, class... _Args>
767inline _LIBCPP_INLINE_VISIBILITY
768void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
769{
770 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
771}
772
773template <class _Tp, class _Allocator, class... _Args>
774inline _LIBCPP_INLINE_VISIBILITY
775void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
776{
777 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
778}
779
780template <class _Tp, class _Allocator, class... _Args>
781inline _LIBCPP_INLINE_VISIBILITY
782void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
783{
784 __user_alloc_construct_impl(
785 __uses_alloc_ctor<_Tp, _Allocator>(),
786 __storage, __a, _VSTD::forward<_Args>(__args)...
787 );
788}
789#endif // _LIBCPP_HAS_NO_VARIADICS
Marshall Clow4a0a9812013-08-13 01:11:06 +0000790
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791_LIBCPP_END_NAMESPACE_STD
792
793#endif // _LIBCPP_FUNCTIONAL_BASE