blob: 2ec3e49596c762a27f79c925be0f97e06ef15c31 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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
Howard Hinnant08e17472011-10-17 20:05:1019#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1620#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1021#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1622
23_LIBCPP_BEGIN_NAMESPACE_STD
24
25template <class _Arg, class _Result>
Howard Hinnant99acc502010-09-21 17:32:3926struct _LIBCPP_VISIBLE unary_function
Howard Hinnantbc8d3f92010-05-11 19:42:1627{
28 typedef _Arg argument_type;
29 typedef _Result result_type;
30};
31
32template <class _Arg1, class _Arg2, class _Result>
Howard Hinnant99acc502010-09-21 17:32:3933struct _LIBCPP_VISIBLE binary_function
Howard Hinnantbc8d3f92010-05-11 19:42:1634{
35 typedef _Arg1 first_argument_type;
36 typedef _Arg2 second_argument_type;
37 typedef _Result result_type;
38};
39
Howard Hinnant99acc502010-09-21 17:32:3940template <class _Tp> struct _LIBCPP_VISIBLE hash;
Howard Hinnantbc8d3f92010-05-11 19:42:1641
42template <class _Tp>
43struct __has_result_type
44{
45private:
46 struct __two {char _; char __;};
47 template <class _Up> static __two __test(...);
48 template <class _Up> static char __test(typename _Up::result_type* = 0);
49public:
50 static const bool value = sizeof(__test<_Tp>(0)) == 1;
51};
52
53#ifdef _LIBCPP_HAS_NO_VARIADICS
54
55#include <__functional_base_03>
56
57#else // _LIBCPP_HAS_NO_VARIADICS
58
59// __weak_result_type
60
61template <class _Tp>
62struct __derives_from_unary_function
63{
64private:
65 struct __two {char _; char __;};
66 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:5067 template <class _Ap, class _Rp>
68 static unary_function<_Ap, _Rp>
69 __test(const volatile unary_function<_Ap, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:1670public:
71 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
72 typedef decltype(__test((_Tp*)0)) type;
73};
74
75template <class _Tp>
76struct __derives_from_binary_function
77{
78private:
79 struct __two {char _; char __;};
80 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:5081 template <class _A1, class _A2, class _Rp>
82 static binary_function<_A1, _A2, _Rp>
83 __test(const volatile binary_function<_A1, _A2, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:1684public:
85 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
86 typedef decltype(__test((_Tp*)0)) type;
87};
88
89template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
90struct __maybe_derive_from_unary_function // bool is true
91 : public __derives_from_unary_function<_Tp>::type
92{
93};
94
95template <class _Tp>
96struct __maybe_derive_from_unary_function<_Tp, false>
97{
98};
99
100template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
101struct __maybe_derive_from_binary_function // bool is true
102 : public __derives_from_binary_function<_Tp>::type
103{
104};
105
106template <class _Tp>
107struct __maybe_derive_from_binary_function<_Tp, false>
108{
109};
110
111template <class _Tp, bool = __has_result_type<_Tp>::value>
112struct __weak_result_type_imp // bool is true
113 : public __maybe_derive_from_unary_function<_Tp>,
114 public __maybe_derive_from_binary_function<_Tp>
115{
116 typedef typename _Tp::result_type result_type;
117};
118
119template <class _Tp>
120struct __weak_result_type_imp<_Tp, false>
121 : public __maybe_derive_from_unary_function<_Tp>,
122 public __maybe_derive_from_binary_function<_Tp>
123{
124};
125
126template <class _Tp>
127struct __weak_result_type
128 : public __weak_result_type_imp<_Tp>
129{
130};
131
132// 0 argument case
133
Howard Hinnant99968442011-11-29 18:15:50134template <class _Rp>
135struct __weak_result_type<_Rp ()>
Howard Hinnantbc8d3f92010-05-11 19:42:16136{
Howard Hinnant99968442011-11-29 18:15:50137 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16138};
139
Howard Hinnant99968442011-11-29 18:15:50140template <class _Rp>
141struct __weak_result_type<_Rp (&)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16142{
Howard Hinnant99968442011-11-29 18:15:50143 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16144};
145
Howard Hinnant99968442011-11-29 18:15:50146template <class _Rp>
147struct __weak_result_type<_Rp (*)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16148{
Howard Hinnant99968442011-11-29 18:15:50149 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16150};
151
152// 1 argument case
153
Howard Hinnant99968442011-11-29 18:15:50154template <class _Rp, class _A1>
155struct __weak_result_type<_Rp (_A1)>
156 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16157{
158};
159
Howard Hinnant99968442011-11-29 18:15:50160template <class _Rp, class _A1>
161struct __weak_result_type<_Rp (&)(_A1)>
162 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16163{
164};
165
Howard Hinnant99968442011-11-29 18:15:50166template <class _Rp, class _A1>
167struct __weak_result_type<_Rp (*)(_A1)>
168 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16169{
170};
171
Howard Hinnant99968442011-11-29 18:15:50172template <class _Rp, class _Cp>
173struct __weak_result_type<_Rp (_Cp::*)()>
174 : public unary_function<_Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16175{
176};
177
Howard Hinnant99968442011-11-29 18:15:50178template <class _Rp, class _Cp>
179struct __weak_result_type<_Rp (_Cp::*)() const>
180 : public unary_function<const _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16181{
182};
183
Howard Hinnant99968442011-11-29 18:15:50184template <class _Rp, class _Cp>
185struct __weak_result_type<_Rp (_Cp::*)() volatile>
186 : public unary_function<volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16187{
188};
189
Howard Hinnant99968442011-11-29 18:15:50190template <class _Rp, class _Cp>
191struct __weak_result_type<_Rp (_Cp::*)() const volatile>
192 : public unary_function<const volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16193{
194};
195
196// 2 argument case
197
Howard Hinnant99968442011-11-29 18:15:50198template <class _Rp, class _A1, class _A2>
199struct __weak_result_type<_Rp (_A1, _A2)>
200 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16201{
202};
203
Howard Hinnant99968442011-11-29 18:15:50204template <class _Rp, class _A1, class _A2>
205struct __weak_result_type<_Rp (*)(_A1, _A2)>
206 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16207{
208};
209
Howard Hinnant99968442011-11-29 18:15:50210template <class _Rp, class _A1, class _A2>
211struct __weak_result_type<_Rp (&)(_A1, _A2)>
212 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16213{
214};
215
Howard Hinnant99968442011-11-29 18:15:50216template <class _Rp, class _Cp, class _A1>
217struct __weak_result_type<_Rp (_Cp::*)(_A1)>
218 : public binary_function<_Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16219{
220};
221
Howard Hinnant99968442011-11-29 18:15:50222template <class _Rp, class _Cp, class _A1>
223struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
224 : public binary_function<const _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16225{
226};
227
Howard Hinnant99968442011-11-29 18:15:50228template <class _Rp, class _Cp, class _A1>
229struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
230 : public binary_function<volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16231{
232};
233
Howard Hinnant99968442011-11-29 18:15:50234template <class _Rp, class _Cp, class _A1>
235struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
236 : public binary_function<const volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16237{
238};
239
240// 3 or more arguments
241
Howard Hinnant99968442011-11-29 18:15:50242template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
243struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16244{
Howard Hinnant99968442011-11-29 18:15:50245 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16246};
247
Howard Hinnant99968442011-11-29 18:15:50248template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
249struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16250{
Howard Hinnant99968442011-11-29 18:15:50251 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16252};
253
Howard Hinnant99968442011-11-29 18:15:50254template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
255struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16256{
Howard Hinnant99968442011-11-29 18:15:50257 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16258};
259
Howard Hinnant99968442011-11-29 18:15:50260template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
261struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16262{
Howard Hinnant99968442011-11-29 18:15:50263 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16264};
265
Howard Hinnant99968442011-11-29 18:15:50266template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
267struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
Howard Hinnantbc8d3f92010-05-11 19:42:16268{
Howard Hinnant99968442011-11-29 18:15:50269 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16270};
271
Howard Hinnant99968442011-11-29 18:15:50272template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
273struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16274{
Howard Hinnant99968442011-11-29 18:15:50275 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16276};
277
Howard Hinnant99968442011-11-29 18:15:50278template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
279struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16280{
Howard Hinnant99968442011-11-29 18:15:50281 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16282};
283
284// __invoke
285
Howard Hinnantbd89e4b2011-05-20 22:02:53286// bullets 1 and 2
Howard Hinnantbc8d3f92010-05-11 19:42:16287
Howard Hinnant99968442011-11-29 18:15:50288template <class _Fp, class _A0, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16289inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53290auto
Howard Hinnant99968442011-11-29 18:15:50291__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19292 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16293{
Howard Hinnant0949eed2011-06-30 21:18:19294 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16295}
296
Howard Hinnant99968442011-11-29 18:15:50297template <class _Fp, class _A0, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16298inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53299auto
Howard Hinnant99968442011-11-29 18:15:50300__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19301 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16302{
Howard Hinnant0949eed2011-06-30 21:18:19303 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16304}
305
Howard Hinnantbd89e4b2011-05-20 22:02:53306// bullets 3 and 4
307
Howard Hinnant99968442011-11-29 18:15:50308template <class _Fp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53310auto
Howard Hinnant99968442011-11-29 18:15:50311__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19312 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16313{
Howard Hinnant0949eed2011-06-30 21:18:19314 return _VSTD::forward<_A0>(__a0).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16315}
316
Howard Hinnant99968442011-11-29 18:15:50317template <class _Fp, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53319auto
Howard Hinnant99968442011-11-29 18:15:50320__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19321 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16322{
Howard Hinnant0949eed2011-06-30 21:18:19323 return (*_VSTD::forward<_A0>(__a0)).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16324}
325
Howard Hinnantbd89e4b2011-05-20 22:02:53326// bullet 5
Howard Hinnantbc8d3f92010-05-11 19:42:16327
Howard Hinnant99968442011-11-29 18:15:50328template <class _Fp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53330auto
Howard Hinnant99968442011-11-29 18:15:50331__invoke(_Fp&& __f, _Args&& ...__args)
332 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16333{
Howard Hinnant99968442011-11-29 18:15:50334 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16335}
336
337template <class _Tp, class ..._Args>
338struct __invoke_return
339{
Howard Hinnant0949eed2011-06-30 21:18:19340 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
Howard Hinnantbc8d3f92010-05-11 19:42:16341};
342
343template <class _Tp>
Howard Hinnant99acc502010-09-21 17:32:39344class _LIBCPP_VISIBLE reference_wrapper
Howard Hinnantbc8d3f92010-05-11 19:42:16345 : public __weak_result_type<_Tp>
346{
347public:
348 // types
349 typedef _Tp type;
350private:
351 type* __f_;
352
353public:
354 // construct/copy/destroy
Howard Hinnant603d2c02011-05-28 17:59:48355 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT : __f_(&__f) {}
Howard Hinnant73d21a42010-09-04 23:28:19356#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16357 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
358#endif
359
360 // access
Howard Hinnant603d2c02011-05-28 17:59:48361 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
362 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16363
364 // invoke
365 template <class... _ArgTypes>
Howard Hinnant99acc502010-09-21 17:32:39366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04367 typename __invoke_of<type&, _ArgTypes...>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16368 operator() (_ArgTypes&&... __args) const
369 {
Howard Hinnant0949eed2011-06-30 21:18:19370 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16371 }
372};
373
374template <class _Tp> struct ____is_reference_wrapper : public false_type {};
375template <class _Tp> struct ____is_reference_wrapper<reference_wrapper<_Tp> > : public true_type {};
376template <class _Tp> struct __is_reference_wrapper
377 : public ____is_reference_wrapper<typename remove_cv<_Tp>::type> {};
378
379template <class _Tp>
380inline _LIBCPP_INLINE_VISIBILITY
381reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48382ref(_Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16383{
384 return reference_wrapper<_Tp>(__t);
385}
386
387template <class _Tp>
388inline _LIBCPP_INLINE_VISIBILITY
389reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48390ref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16391{
392 return ref(__t.get());
393}
394
395template <class _Tp>
396inline _LIBCPP_INLINE_VISIBILITY
397reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48398cref(const _Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16399{
400 return reference_wrapper<const _Tp>(__t);
401}
402
403template <class _Tp>
404inline _LIBCPP_INLINE_VISIBILITY
405reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48406cref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16407{
408 return cref(__t.get());
409}
410
Howard Hinnant73d21a42010-09-04 23:28:19411#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
412#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
413
414template <class _Tp> void ref(const _Tp&& __t) = delete;
415template <class _Tp> void cref(const _Tp&& __t) = delete;
416
417#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
418
419template <class _Tp> void ref(const _Tp&& __t);// = delete;
420template <class _Tp> void cref(const _Tp&& __t);// = delete;
421
422#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
423
424#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16425
426#endif // _LIBCPP_HAS_NO_VARIADICS
427
428_LIBCPP_END_NAMESPACE_STD
429
430#endif // _LIBCPP_FUNCTIONAL_BASE