-
Notifications
You must be signed in to change notification settings - Fork 1
/
function_ref.hpp
executable file
·220 lines (186 loc) · 7.01 KB
/
function_ref.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
///
// function_ref - A low-overhead non-owning function
// Written in 2017 by Stf Kolev (@stfkolev)
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.
///
#ifndef TL_FUNCTION_REF_HPP
#define TL_FUNCTION_REF_HPP
#define TL_FUNCTION_REF_VERSION_MAJOR 0
#define TL_FUNCTION_REF_VERSION_MINOR 1
#if (defined(_MSC_VER) && _MSC_VER == 1900)
/// \exclude
#define TL_FUNCTION_REF_MSVC2015
#endif
#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \
!defined(__clang__))
/// \exclude
#define TL_FUNCTION_REF_GCC49
#endif
#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && \
!defined(__clang__))
/// \exclude
#define TL_FUNCTION_REF_GCC54
#endif
#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \
!defined(__clang__))
// GCC < 5 doesn't support overloading on const&& for member functions
/// \exclude
#define TL_FUNCTION_REF_NO_CONSTRR
#endif
#if __cplusplus > 201103L
/// \exclude
#define TL_FUNCTION_REF_CXX14
#endif
// constexpr implies const in C++11, not C++14
#if (__cplusplus == 201103L || defined(TL_FUNCTION_REF_MSVC2015) || \
defined(TL_FUNCTION_REF_GCC49)) && \
!defined(TL_FUNCTION_REF_GCC54)
/// \exclude
#define TL_FUNCTION_REF_11_CONSTEXPR
#else
/// \exclude
#define TL_FUNCTION_REF_11_CONSTEXPR constexpr
#endif
#include <functional>
#include <utility>
namespace tl {
namespace detail {
#ifndef TL_TRAITS_MUTEX
#define TL_TRAITS_MUTEX
// C++14-style aliases for brevity
template <class T> using remove_const_t = typename std::remove_const<T>::type;
template <class T>
using remove_reference_t = typename std::remove_reference<T>::type;
template <class T> using decay_t = typename std::decay<T>::type;
template <bool E, class T = void>
using enable_if_t = typename std::enable_if<E, T>::type;
template <bool B, class T, class F>
using conditional_t = typename std::conditional<B, T, F>::type;
// std::invoke from C++17
// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
template <typename Fn, typename... Args,
typename = enable_if_t<std::is_member_pointer<decay_t<Fn>>::value>,
int = 0>
constexpr auto invoke(Fn &&f, Args &&... args) noexcept(
noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
-> decltype(std::mem_fn(f)(std::forward<Args>(args)...)) {
return std::mem_fn(f)(std::forward<Args>(args)...);
}
template <typename Fn, typename... Args,
typename = enable_if_t<!std::is_member_pointer<decay_t<Fn>>{}>>
constexpr auto invoke(Fn &&f, Args &&... args) noexcept(
noexcept(std::forward<Fn>(f)(std::forward<Args>(args)...)))
-> decltype(std::forward<Fn>(f)(std::forward<Args>(args)...)) {
return std::forward<Fn>(f)(std::forward<Args>(args)...);
}
// std::invoke_result from C++17
template <class F, class, class... Us> struct invoke_result_impl;
template <class F, class... Us>
struct invoke_result_impl<
F, decltype(invoke(std::declval<F>(), std::declval<Us>()...), void()),
Us...> {
using type = decltype(invoke(std::declval<F>(), std::declval<Us>()...));
};
template <class F, class... Us>
using invoke_result = invoke_result_impl<F, void, Us...>;
template <class F, class... Us>
using invoke_result_t = typename invoke_result<F, Us...>::type;
#endif
template <class, class R, class F, class... Args>
struct is_invocable_r_impl : std::false_type {};
template <class R, class F, class... Args>
struct is_invocable_r_impl<
typename std::is_same<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
: std::true_type {};
template <class R, class F, class... Args>
using is_invocable_r = is_invocable_r_impl<std::true_type, R, F, Args...>;
} // namespace detail
/// A lightweight non-owning reference to a callable.
///
/// Example usage:
///
/// ```cpp
/// void foo (function_ref<int(int)> func) {
/// std::cout << "Result is " << func(21); //42
/// }
///
/// foo([](int i) { return i*2; });
template <class F> class function_ref;
/// Specialization for function types.
template <class R, class... Args> class function_ref<R(Args...)> {
public:
constexpr function_ref() noexcept = delete;
/// Creates a `function_ref` which refers to the same callable as `rhs`.
constexpr function_ref(const function_ref<R(Args...)> &rhs) noexcept = default;
/// Constructs a `function_ref` referring to `f`.
///
/// \synopsis template <typename F> constexpr function_ref(F &&f) noexcept
template <typename F,
detail::enable_if_t<
!std::is_same<detail::decay_t<F>, function_ref>::value &&
detail::is_invocable_r<R, F &&, Args...>::value> * = nullptr>
TL_FUNCTION_REF_11_CONSTEXPR function_ref(F &&f) noexcept
: obj_(reinterpret_cast<void *>(std::addressof(f))) {
callback_ = [](void *obj, Args... args) {
return detail::invoke(
*reinterpret_cast<typename std::add_pointer<F>::type>(obj),
std::forward<Args>(args)...);
};
}
/// Makes `*this` refer to the same callable as `rhs`.
TL_FUNCTION_REF_11_CONSTEXPR function_ref<R(Args...)> &
operator=(const function_ref<R(Args...)> &rhs) noexcept {
obj_ = rhs.obj_;
callback_ = rhs.callback_;
return *this;
}
/// Makes `*this` refer to `f`.
///
/// \synopsis template <typename F> constexpr function_ref &operator=(F &&f) noexcept;
template <typename F,
detail::enable_if_t<detail::is_invocable_r<R, F &&, Args...>::value>
* = nullptr>
TL_FUNCTION_REF_11_CONSTEXPR function_ref<R(Args...)> &operator=(F &&f) noexcept {
obj_ = reinterpret_cast<void *>(std::addressof(f));
callback_ = [](void *obj, Args... args) {
return detail::invoke(
*reinterpret_cast<typename std::add_pointer<F>::type>(obj),
std::forward<Args>(args)...);
};
return *this;
}
/// Swaps the referred callables of `*this` and `rhs`.
constexpr void swap(function_ref<R(Args...)> &rhs) noexcept {
std::swap(obj_, rhs.obj_);
std::swap(callback_, rhs.callback_);
}
/// Call the stored callable with the given arguments.
R operator()(Args... args) const {
return callback_(obj_, std::forward<Args>(args)...);
}
private:
void *obj_ = nullptr;
R (*callback_)(void *, Args...) = nullptr;
};
/// Swaps the referred callables of `lhs` and `rhs`.
template <typename R, typename... Args>
constexpr void swap(function_ref<R(Args...)> &lhs,
function_ref<R(Args...)> &rhs) noexcept {
lhs.swap(rhs);
}
#if __cplusplus >= 201703L
template <typename R, typename... Args>
function_ref(R (*)(Args...))->function_ref<R(Args...)>;
// TODO, will require some kind of callable traits
// template <typename F>
// function_ref(F) -> function_ref</* deduced if possible */>;
#endif
} // namespace tl
#endif