-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.hpp
196 lines (153 loc) · 5.1 KB
/
function.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
#ifndef FUNCTION_FUNCTION_HPP
#define FUNCTION_FUNCTION_HPP
#include <stdexcept>
#include <utility>
#if !defined(RE_FUNCTION_NO_RTTI)
#include <typeinfo>
#endif
namespace re {
class bad_function_call : public std::exception {
const char *what() const noexcept override { return "Bad function call."; }
};
template <typename> class function;
template <typename Ret, typename... Args> class function<Ret(Args...)> {
public:
using result_type = Ret;
function() noexcept : callable(nullptr) {}
function(std::nullptr_t) noexcept : callable(nullptr) {}
function(const function &other)
: callable(new callable_impl<Ret(Args...)>(other.callable)) {}
function(function &&other) noexcept { std::swap(callable, other.callable); }
template <typename Func>
function(Func &&func)
: callable(new callable_impl<Func>(std::forward<Func>(func))) {}
/** Special constructor for function pointers. */
function(Ret (*fptr)(Args...))
: callable(new callable_impl<Ret (*)(Args...)>(std::move(fptr))) {}
function &operator=(const function &other) {
if (&other != this) {
function(other).swap(*this);
}
return *this;
}
function &operator=(function &&other) {
std::swap(callable, other.callable);
return *this;
}
function &operator=(std::nullptr_t) noexcept {
delete callable;
callable = nullptr;
return *this;
}
template <class F> function &operator=(F &&f) {
function(std::forward<F>(f)).swap(*this);
return *this;
}
template <class F> function &operator=(std::reference_wrapper<F> f) noexcept {
function(f).swap(*this);
return *this;
}
result_type operator()(Args &&...args) const {
if (callable == nullptr) {
throw bad_function_call();
}
return callable->call(std::forward<Args>(args)...);
}
explicit operator bool() const noexcept { return callable != nullptr; }
void swap(function<Ret(Args...)> &other) noexcept {
std::swap(callable, other.callable);
}
#if !defined(RE_FUNCTION_NO_RTTI)
const std::type_info &target_type() const noexcept {
return callable->target_type();
}
template <typename T> T *target() noexcept {
if (typeid(T) == callable->target_type()) {
return reinterpret_cast<T *>(callable->get_ptr_to_callable());
}
return nullptr;
}
template <typename T> const T *target() const noexcept {
if (typeid(T) == callable->target_type()) {
return reinterpret_cast<const T *>(callable->get_ptr_to_callable());
}
return nullptr;
}
#endif
~function() { delete callable; }
private:
class callable_base {
public:
/** Functors can change their state, so the method is not marked as const */
virtual Ret call(Args &&...args) = 0;
#if !defined(RE_FUNCTION_NO_RTTI)
virtual const std::type_info &target_type() const noexcept = 0;
virtual void *get_ptr_to_callable() noexcept = 0;
virtual const void *get_ptr_to_callable() const noexcept = 0;
#endif
virtual ~callable_base() {}
};
template <typename Impl> class callable_impl : public callable_base {
public:
explicit callable_impl(Impl &&impl_) : impl(std::move(impl_)) {}
callable_impl(const callable_impl &other) : impl(other.impl) {}
Ret call(Args &&...args) override {
return impl(std::forward<Args>(args)...);
}
#if !defined(RE_FUNCTION_NO_RTTI)
const std::type_info &target_type() const noexcept override {
return typeid(impl);
}
void *get_ptr_to_callable() noexcept override { return &impl; }
const void *get_ptr_to_callable() const noexcept override { return &impl; }
#endif
private:
Impl impl;
};
callable_base *callable;
};
namespace {
template <typename> struct func_trait;
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...)> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) const> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) noexcept> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) const noexcept> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) &> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) &noexcept> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) const &> {
using type = Ret(Args...);
};
template <typename T, typename Ret, typename... Args>
struct func_trait<Ret (T::*)(Args...) const &noexcept> {
using type = Ret(Args...);
};
} // namespace
template <typename Ret, typename... Args>
function(Ret (*)(Args...)) -> function<Ret(Args...)>;
template <typename F>
function(F) -> function<typename func_trait<decltype(&F::operator())>::type>;
template <class R, class... Args>
void swap(function<R(Args...)> &lhs, function<R(Args...)> &rhs) noexcept {
lhs.swap(rhs);
}
} /* namespace re */
#endif /* FUNCTION_FUNCTION_HPP */