-
Notifications
You must be signed in to change notification settings - Fork 6
/
functional.hpp
174 lines (149 loc) · 5.48 KB
/
functional.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include <stdx/type_traits.hpp>
#include <functional>
#include <type_traits>
#if __cpp_lib_bind_front < 202306L or __cpp_lib_bind_back < 202306L
#if __cplusplus >= 202002L
#include <stdx/tuple.hpp>
#else
#include <tuple>
#endif
#endif
namespace stdx {
inline namespace v1 {
template <typename F> struct with_result_of : F {
using R = std::invoke_result_t<F>;
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr operator R() const
noexcept(noexcept(static_cast<F const &>(*this)())) {
return static_cast<F const &>(*this)();
}
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr operator R() noexcept(noexcept(static_cast<F &>(*this)())) {
return static_cast<F &>(*this)();
}
};
#if __cpp_deduction_guides < 201907L
template <typename F> with_result_of(F) -> with_result_of<F>;
#endif
namespace detail {
#if __cpp_lib_bind_front < 202306L or __cpp_lib_bind_back < 202306L
#if __cplusplus >= 202002L
template <typename... Ts> using bind_tuple_t = stdx::tuple<Ts...>;
using stdx::get;
#else
template <typename... Ts> using bind_tuple_t = std::tuple<Ts...>;
using std::get;
#endif
#endif
} // namespace detail
#if __cpp_lib_bind_front >= 201907L
using std::bind_front;
#else
namespace detail {
template <typename...> struct bind_front_t;
template <typename F, std::size_t... Is, typename... BoundArgs>
struct bind_front_t<F, std::index_sequence<Is...>, BoundArgs...> {
F f{};
bind_tuple_t<BoundArgs...> t{};
template <typename... Args>
constexpr auto operator()(Args &&...args) const & {
return f(get<Is>(t)..., std::forward<Args>(args)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) & {
return f(get<Is>(t)..., std::forward<Args>(args)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) && {
return std::move(f)(get<Is>(std::move(t))...,
std::forward<Args>(args)...);
}
};
} // namespace detail
template <typename F, typename... Args>
constexpr auto bind_front(F &&f, Args &&...args) {
return detail::bind_front_t<stdx::remove_cvref_t<F>,
std::make_index_sequence<sizeof...(Args)>,
std::decay_t<Args>...>{
std::forward<F>(f), {std::forward<Args>(args)...}};
}
#endif
#if __cpp_lib_bind_front < 202306L
namespace detail {
template <auto, typename...> struct bind_front_value_t;
template <auto F, std::size_t... Is, typename... BoundArgs>
struct bind_front_value_t<F, std::index_sequence<Is...>, BoundArgs...> {
bind_tuple_t<BoundArgs...> t{};
template <typename... Args>
constexpr auto operator()(Args &&...args) const & {
return F(get<Is>(t)..., std::forward<Args>(args)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) & {
return F(get<Is>(t)..., std::forward<Args>(args)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) && {
return F(get<Is>(std::move(t))..., std::forward<Args>(args)...);
}
};
} // namespace detail
template <auto F, typename... Args> constexpr auto bind_front(Args &&...args) {
return detail::bind_front_value_t<
F, std::make_index_sequence<sizeof...(Args)>, std::decay_t<Args>...>{
{std::forward<Args>(args)...}};
}
#endif
#if __cpp_lib_bind_back >= 202202L
using std::bind_back;
#else
namespace detail {
template <typename...> struct bind_back_t;
template <typename F, std::size_t... Is, typename... BoundArgs>
struct bind_back_t<F, std::index_sequence<Is...>, BoundArgs...> {
F f{};
bind_tuple_t<BoundArgs...> t{};
template <typename... Args>
constexpr auto operator()(Args &&...args) const & {
return f(std::forward<Args>(args)..., get<Is>(t)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) & {
return f(std::forward<Args>(args)..., get<Is>(t)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) && {
return std::move(f)(std::forward<Args>(args)...,
get<Is>(std::move(t))...);
}
};
} // namespace detail
template <typename F, typename... Args>
constexpr auto bind_back(F &&f, Args &&...args) {
return detail::bind_back_t<stdx::remove_cvref_t<F>,
std::make_index_sequence<sizeof...(Args)>,
std::decay_t<Args>...>{
std::forward<F>(f), {std::forward<Args>(args)...}};
}
#endif
#if __cpp_lib_bind_back < 202306L
namespace detail {
template <auto, typename...> struct bind_back_value_t;
template <auto F, std::size_t... Is, typename... BoundArgs>
struct bind_back_value_t<F, std::index_sequence<Is...>, BoundArgs...> {
bind_tuple_t<BoundArgs...> t{};
template <typename... Args>
constexpr auto operator()(Args &&...args) const & {
return F(std::forward<Args>(args)..., get<Is>(t)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) & {
return F(std::forward<Args>(args)..., get<Is>(t)...);
}
template <typename... Args> constexpr auto operator()(Args &&...args) && {
return F(std::forward<Args>(args)..., get<Is>(std::move(t))...);
}
};
} // namespace detail
template <auto F, typename... Args> constexpr auto bind_back(Args &&...args) {
return detail::bind_back_value_t<
F, std::make_index_sequence<sizeof...(Args)>, std::decay_t<Args>...>{
{std::forward<Args>(args)...}};
}
#endif
} // namespace v1
} // namespace stdx