1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_BITS_FUNCTIONAL_INVOKE
|
---|
30 | #define LIBCPP_BITS_FUNCTIONAL_INVOKE
|
---|
31 |
|
---|
32 | #include <__bits/utility/declval.hpp>
|
---|
33 | #include <__bits/utility/forward_move.hpp>
|
---|
34 |
|
---|
35 | namespace std
|
---|
36 | {
|
---|
37 | template<class>
|
---|
38 | struct is_member_function_pointer;
|
---|
39 |
|
---|
40 | template<class, class>
|
---|
41 | struct is_base_of;
|
---|
42 |
|
---|
43 | template<class>
|
---|
44 | struct is_member_object_pointer;
|
---|
45 | }
|
---|
46 |
|
---|
47 | namespace std::aux
|
---|
48 | {
|
---|
49 | /**
|
---|
50 | * 20.9.2, requirements:
|
---|
51 | */
|
---|
52 |
|
---|
53 | template<class R, class T, class T1, class... Ts>
|
---|
54 | decltype(auto) invoke(R T::* f, T1&& t1, Ts&&... args)
|
---|
55 | {
|
---|
56 | if constexpr (is_member_function_pointer<decltype(f)>::value)
|
---|
57 | {
|
---|
58 | if constexpr (is_base_of<T, remove_reference_t<T1>>::value)
|
---|
59 | // (1.1)
|
---|
60 | return (t1.*f)(forward<Ts>(args)...);
|
---|
61 | else
|
---|
62 | // (1.2)
|
---|
63 | return ((*t1).*f)(forward<Ts>(args)...);
|
---|
64 | }
|
---|
65 | else if constexpr (is_member_object_pointer<decltype(f)>::value && sizeof...(args) == 0)
|
---|
66 | {
|
---|
67 | /**
|
---|
68 | * Note: Standard requires to N be equal to 1, but we take t1 directly
|
---|
69 | * so we need sizeof...(args) to be 0.
|
---|
70 | */
|
---|
71 | if constexpr (is_base_of<T, remove_reference_t<T1>>::value)
|
---|
72 | // (1.3)
|
---|
73 | return t1.*f;
|
---|
74 | else
|
---|
75 | // (1.4)
|
---|
76 | return (*t1).*f;
|
---|
77 | }
|
---|
78 | else
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Note: If this condition holds this will not be reachable,
|
---|
82 | * but a new addition to the standard (17.7 point (8.1))
|
---|
83 | * prohibits us from simply using false as the condition here,
|
---|
84 | * so we use this because we know it is false here.
|
---|
85 | */
|
---|
86 | static_assert(is_member_function_pointer<decltype(f)>::value, "invalid invoke");
|
---|
87 | }
|
---|
88 |
|
---|
89 | template<class F, class... Args>
|
---|
90 | decltype(auto) invoke(F&& f, Args&&... args)
|
---|
91 | {
|
---|
92 | // (1.5)
|
---|
93 | return f(forward<Args>(args)...);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | #endif
|
---|