blob: 3ff2e3504d8356ff64af0d10b2309027d2637d14 [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>
Howard Hinnant99acc502010-09-21 17:32:39 +0000518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +0000519 typename __invoke_of<type&, _ArgTypes...>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 operator() (_ArgTypes&&... __args) const
521 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000522 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 }
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000524#else
525
526 _LIBCPP_INLINE_VISIBILITY
527 typename __invoke_return<type>::type
528 operator() () const
529 {
530 return __invoke(get());
531 }
532
533 template <class _A0>
534 _LIBCPP_INLINE_VISIBILITY
535 typename __invoke_return0<type&, _A0>::type
536 operator() (_A0& __a0) const
537 {
538 return __invoke<type&, _A0>(get(), __a0);
539 }
540
541 template <class _A0, class _A1>
542 _LIBCPP_INLINE_VISIBILITY
543 typename __invoke_return1<type&, _A0, _A1>::type
544 operator() (_A0& __a0, _A1& __a1) const
545 {
546 return __invoke<type&, _A0, _A1>(get(), __a0, __a1);
547 }
548
549 template <class _A0, class _A1, class _A2>
550 _LIBCPP_INLINE_VISIBILITY
551 typename __invoke_return2<type&, _A0, _A1, _A2>::type
552 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
553 {
554 return __invoke<type&, _A0, _A1, _A2>(get(), __a0, __a1, __a2);
555 }
556#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557};
558
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000559template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
560template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561template <class _Tp> struct __is_reference_wrapper
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000562 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563
564template <class _Tp>
565inline _LIBCPP_INLINE_VISIBILITY
566reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000567ref(_Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568{
569 return reference_wrapper<_Tp>(__t);
570}
571
572template <class _Tp>
573inline _LIBCPP_INLINE_VISIBILITY
574reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000575ref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576{
577 return ref(__t.get());
578}
579
580template <class _Tp>
581inline _LIBCPP_INLINE_VISIBILITY
582reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000583cref(const _Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584{
585 return reference_wrapper<const _Tp>(__t);
586}
587
588template <class _Tp>
589inline _LIBCPP_INLINE_VISIBILITY
590reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000591cref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592{
593 return cref(__t.get());
594}
595
Eric Fiselier5b3a4592015-07-22 22:23:49 +0000596#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000597#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
598#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
599
Howard Hinnantec3773c2011-12-01 20:21:04 +0000600template <class _Tp> void ref(const _Tp&&) = delete;
601template <class _Tp> void cref(const _Tp&&) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000602
603#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
604
Howard Hinnantec3773c2011-12-01 20:21:04 +0000605template <class _Tp> void ref(const _Tp&&);// = delete;
606template <class _Tp> void cref(const _Tp&&);// = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000607
608#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
609
610#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
612#endif // _LIBCPP_HAS_NO_VARIADICS
613
Marshall Clow4a0a9812013-08-13 01:11:06 +0000614#if _LIBCPP_STD_VER > 11
615template <class _Tp1, class _Tp2 = void>
616struct __is_transparent
617{
618private:
619 struct __two {char __lx; char __lxx;};
620 template <class _Up> static __two __test(...);
621 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
622public:
623 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
624};
625#endif
626
Marshall Clow599e60d2013-09-12 02:11:16 +0000627// allocator_arg_t
628
629struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
630
631#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
632extern const allocator_arg_t allocator_arg;
633#else
634constexpr allocator_arg_t allocator_arg = allocator_arg_t();
635#endif
636
637// uses_allocator
638
639template <class _Tp>
640struct __has_allocator_type
641{
642private:
643 struct __two {char __lx; char __lxx;};
644 template <class _Up> static __two __test(...);
645 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
646public:
647 static const bool value = sizeof(__test<_Tp>(0)) == 1;
648};
649
650template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
651struct __uses_allocator
652 : public integral_constant<bool,
653 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
654{
655};
656
657template <class _Tp, class _Alloc>
658struct __uses_allocator<_Tp, _Alloc, false>
659 : public false_type
660{
661};
662
663template <class _Tp, class _Alloc>
664struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
665 : public __uses_allocator<_Tp, _Alloc>
666{
667};
668
669#ifndef _LIBCPP_HAS_NO_VARIADICS
670
671// allocator construction
672
673template <class _Tp, class _Alloc, class ..._Args>
674struct __uses_alloc_ctor_imp
675{
676 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
677 static const bool __ic =
678 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
679 static const int value = __ua ? 2 - __ic : 0;
680};
681
682template <class _Tp, class _Alloc, class ..._Args>
683struct __uses_alloc_ctor
684 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
685 {};
686
687template <class _Tp, class _Allocator, class... _Args>
688inline _LIBCPP_INLINE_VISIBILITY
689void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
690{
691 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
692}
693
694template <class _Tp, class _Allocator, class... _Args>
695inline _LIBCPP_INLINE_VISIBILITY
696void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
697{
698 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
699}
700
701template <class _Tp, class _Allocator, class... _Args>
702inline _LIBCPP_INLINE_VISIBILITY
703void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
704{
705 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
706}
707
708template <class _Tp, class _Allocator, class... _Args>
709inline _LIBCPP_INLINE_VISIBILITY
710void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
711{
712 __user_alloc_construct_impl(
713 __uses_alloc_ctor<_Tp, _Allocator>(),
714 __storage, __a, _VSTD::forward<_Args>(__args)...
715 );
716}
717#endif // _LIBCPP_HAS_NO_VARIADICS
Marshall Clow4a0a9812013-08-13 01:11:06 +0000718
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719_LIBCPP_END_NAMESPACE_STD
720
721#endif // _LIBCPP_FUNCTIONAL_BASE