-
Notifications
You must be signed in to change notification settings - Fork 39
/
function.h
425 lines (358 loc) · 12.2 KB
/
function.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#pragma once
#include "Utils/type_traits.h"
#include <stdexcept>
// src code from folly
namespace MiniSTL {
template<typename functionType>
class function;
namespace detail::function {
struct CoerceTag {};// 额外参数,for private ctor
// 可执行对象的操作类型
// move:移动可执行对象
// nuke:析构可执行对象
// heap:获取可执行对象大小
enum class Op { MOVE,
NUKE,
HEAP };
union Data {// store callable object
Data() = default;
void *big; // call object on heap
std::aligned_storage<6 * sizeof(void *)>::type tiny;// call object on stack
};
// 观察T是否可以由nullptr构造得到,并且支持与nullptr做比较
template<typename T>
using functionNullptrTest =
decltype(static_cast<bool>(static_cast<T const &>(T(nullptr)) == nullptr));
template<typename T, typename = void>
struct isPassFunctionNullptrTest : false_type {};
template<typename T>
struct isPassFunctionNullptrTest<T, std::void_t<functionNullptrTest<T>>>
: true_type {};
// T若不可以转换为nullptr_t类型,返回false
// 若可以,则判断t == nullptr
template<typename T>
constexpr bool isEmptyFunction(T const &t) {
if constexpr (!isPassFunctionNullptrTest<T>::value) {
return false;
} else {
return static_cast<bool>(t == nullptr);
}
}
// 获取F(Args...)的返回类型
template<typename F, typename... Args>
using CallableResult = decltype(declval<F>()(declval<Args>()...));
// 判断将From转为To的转换是否存在空悬引用
template<
typename From,
typename To,
typename = typename std::enable_if<
!is_reference<To>::value || is_reference<From>::value>::type>
using IfSafeResultImpl = decltype(void(static_cast<To>(declval<From>())));
template<typename T>
using CallArg = conditional_t<is_register_pass_v<T>, T, T &&>;
template<typename functionType>
struct functionTraits;
template<typename ReturnType, typename... Args>
struct functionTraits<ReturnType(Args...)> {
using Call = ReturnType (*)(CallArg<Args>..., Data &);
using ConstSignature = ReturnType(Args...) const;
using NonConstSignature = ReturnType(Args...);
using OtherSignature = ConstSignature;
template<typename F, typename R = CallableResult<F &, Args...>>
using IfSafeResult = IfSafeResultImpl<R, ReturnType>;
template<typename Fun>
static ReturnType callSmall(CallArg<Args>... args, Data &p) {
auto &fn = *static_cast<Fun *>(static_cast<void *>(&p.tiny));// call obj on stack
if constexpr (is_void<ReturnType>::value) {
fn(static_cast<Args &&>(args)...);
} else {
return fn(static_cast<Args &&>(args)...);
}
}
template<typename Fun>
static ReturnType callBig(CallArg<Args>... args, Data &p) {
auto &fn = *static_cast<Fun *>(p.big);// call obj on heap
if constexpr (is_void<ReturnType>::value) {
fn(static_cast<Args &&>(args)...);
} else {
return fn(static_cast<Args &&>(args)...);
}
}
static ReturnType uninitCall(CallArg<Args>..., Data &) {
throw std::bad_function_call();
}
ReturnType operator()(Args... args) {
auto &fn = *static_cast<MiniSTL::function<NonConstSignature> *>(this);
return fn.call_(static_cast<Args &&>(args)..., fn.data_);
}
};
template<typename ReturnType, typename... Args>
struct functionTraits<ReturnType(Args...) const> {
using Call = ReturnType (*)(CallArg<Args>..., Data &);
using ConstSignature = ReturnType(Args...) const;
using NonConstSignature = ReturnType(Args...);
using OtherSignature = NonConstSignature;
template<typename F, typename R = CallableResult<const F &, Args...>>
using IfSafeResult = IfSafeResultImpl<R, ReturnType>;
template<typename Fun>
static ReturnType callSmall(CallArg<Args>... args, Data &p) {
auto &fn = *static_cast<const Fun *>(static_cast<void *>(&p.tiny));
if constexpr (is_void<ReturnType>::value) {
fn(static_cast<Args &&>(args)...);
} else {
return fn(static_cast<Args &&>(args)...);
}
}
template<typename Fun>
static ReturnType callBig(CallArg<Args>... args, Data &p) {
auto &fn = *static_cast<const Fun *>(p.big);
if constexpr (is_void<ReturnType>::value) {
fn(static_cast<Args &&>(args)...);
} else {
return fn(static_cast<Args &&>(args)...);
}
}
static ReturnType uninitCall(CallArg<Args>..., Data &) {
throw std::bad_function_call();
}
ReturnType operator()(Args... args) const {// with const
auto &fn = *static_cast<const MiniSTL::function<ConstSignature> *>(this);
return fn.call_(static_cast<Args &&>(args)..., fn.data_);
}
};
// TODO::need noexpect functionTraits
// 可执行对象的操作器,操作分为三种:
// move(移动可执行对象)
// nuke(析构可执行对象)
// heap(获取可执行对象在堆上的大小)
std::size_t exec_(Op, Data *, Data *) noexcept;
using Exec = decltype(&exec_);
static_assert(noexcept(Exec(nullptr)(Op{}, nullptr, nullptr)), "exec must be noexcept");
// call obj 三类操作
struct DispatchSmallTrivial {
template<typename Fun, typename Base>
static constexpr auto call = Base::template callSmall<Fun>;
template<std::size_t Size>
static std::size_t exec_(Op o, Data *src, Data *dst) noexcept {
switch (o) {
case Op::MOVE:
std::memcpy(static_cast<void *>(dst), static_cast<void *>(src), Size);
break;
case Op::NUKE:
break;
case Op::HEAP:
break;
}
return 0U;
}
template<std::size_t size, std::size_t adjust = alignof(Data) - 1>
static constexpr std::size_t size_ = (size + adjust) & ~adjust;
template<typename Fun>
static constexpr auto exec = exec_<size_<sizeof(Fun)>>;
};
struct DispatchSmall {
template<typename Fun, typename Base>
static constexpr auto call = Base::template callSmall<Fun>;
template<typename Fun>
static std::size_t exec(Op o, Data *src, Data *dst) noexcept {
switch (o) {
case Op::MOVE:
::new (static_cast<void *>(&dst->tiny)) Fun(static_cast<Fun &&>(
*static_cast<Fun *>(static_cast<void *>(&src->tiny))));
// Here is no break, because we need to move the functor.
case Op::NUKE:
static_cast<Fun *>(static_cast<void *>(&src->tiny))->~Fun();
break;
case Op::HEAP:
break;
}
return 0U;
}
};
struct DispatchBig {
template<typename Fun, typename Base>
static constexpr auto call = Base::template callBig<Fun>;
template<typename Fun>
static std::size_t exec(Op o, Data *src, Data *dst) noexcept {
switch (o) {
case Op::MOVE:
dst->big = src->big;
src->big = nullptr;
break;
case Op::NUKE:
delete static_cast<Fun *>(src->big);
break;
case Op::HEAP:
break;
}
return sizeof(Fun);
}
};
}// namespace detail::function
template<typename functionType>
class function final : private detail::function::functionTraits<functionType> {
private:
using Data = detail::function::Data;
using Op = detail::function::Op;
using Traits = detail::function::functionTraits<functionType>;
using Call = typename Traits::Call;
using Exec = detail::function::Exec;
using CoerceTag = detail::function::CoerceTag;
// sizeof(function) == 64 bytes (on 64-bit platform)
// data:48 bytes, call_:8 bytes, exec_:8 bytes
mutable Data data_{};// call obj
Call call_{&Traits::uninitCall};
Exec exec_{nullptr};// exec on call obj
std::size_t exec(Op o, Data *src, Data *dst) const {
if (!exec_) {
return 0U;
}
return exec_(o, src, dst);
}
friend Traits;
friend class function<typename Traits::OtherSignature>;
template<typename Signature>
function(function<Signature> &&fun, CoerceTag) {
using Fun = function<Signature>;
if (fun) {
data_.big = new Fun(static_cast<Fun &&>(fun));
call_ = Traits::template callBig<Fun>;
exec_ = Exec(detail::function::DispatchBig::exec<Fun>);
}
}
function(function<typename Traits::OtherSignature> &&that, CoerceTag) noexcept
: call_(that.call_), exec_(that.exec_) {
that.call_ = &Traits::uninitCall;
that.exec_ = nullptr;
exec(Op::MOVE, &that.data_, &data_);
}
public:
function() = default;
// not copyable
function(const function &) = delete;
function(function &&that) noexcept : call_(that.call_), exec_(that.exec_) {
that.call_ = &Traits::uninitCall;
that.exec_ = nullptr;
exec(Op::MOVE, &that.data_, &data_);
}
function(std::nullptr_t) noexcept {}
template<
typename Fun,
typename =
enable_if_t<!is_similar_instantiation_v<function, Fun>>,
typename = typename Traits::template IfSafeResult<Fun>,
bool IsSmall = sizeof(Fun) <= sizeof(Data::tiny) &&noexcept(Fun(declval<Fun>()))>
function(Fun fun) noexcept(
IsSmall &&noexcept(Fun(static_cast<Fun &&>(fun)))) {
using Dispatch = conditional_t<
IsSmall && std::is_trivially_copyable_v<Fun>,
detail::function::DispatchSmallTrivial,
conditional_t<
IsSmall,
detail::function::DispatchSmall,
detail::function::DispatchBig>>;
if (detail::function::isEmptyFunction(fun)) {
return;
}
if constexpr (IsSmall) {
::new (&data_.tiny) Fun(static_cast<Fun &&>(fun));
} else {
data_.big = new Fun(static_cast<Fun &&>(fun));
}
call_ = Dispatch::template call<Fun, Traits>;
exec_ = Exec(Dispatch::template exec<Fun>);
}
template<
typename Signature,
typename Fun = function<Signature>,
typename = std::enable_if_t<!std::is_same<function, Fun>::value>,
typename = typename Traits::template IfSafeResult<Fun>>
function(function<Signature> &&that) noexcept(
noexcept(function(move(that), CoerceTag{})))
: function(move(that), CoerceTag{}) {}
template<
typename Member,
typename Class,
typename = decltype(function(std::mem_fn((Member Class::*) nullptr)))>
function(Member Class::*ptr) noexcept {
if (ptr) {
*this = std::mem_fn(ptr);
}
}
~function() { exec(Op::NUKE, &data_, nullptr); }
function &operator=(const function &) = delete;
function &operator=(function &&that) noexcept {
exec(Op::NUKE, &data_, nullptr);
that.exec(Op::MOVE, &that.data_, &data_);
exec_ = that.exec_;
call_ = that.call_;
that.exec_ = nullptr;
that.call_ = &Traits::uninitCall;
return *this;
}
template<
typename Fun,
typename...,
bool Nx = noexcept(function(declval<Fun>()))>
function &operator=(Fun fun) noexcept(Nx) {
if (Nx) {// 如果可能抛出异常,则析构this,重新构造
this->~function();
::new (this) function(static_cast<Fun &&>(fun));
} else {
// Construct a temporary and (nothrow) swap.
function(static_cast<Fun &&>(fun)).swap(*this);
}
return *this;
}
template<
typename Signature,
typename...,
typename = typename Traits::template IfSafeResult<function<Signature>>>
function &operator=(function<Signature> &&that) noexcept(
noexcept(function(move(that)))) {
*this = function(move(that));
return *this;
}
function &operator=(std::nullptr_t) noexcept {
*this = function();
return *this;
}
template<typename Member, typename Class>
function &operator=(Member Class::*ptr) noexcept {
if (ptr) {
*this = std::mem_fn(ptr);
} else {
*this = function();
}
return *this;
}
// operator()函数
using Traits::operator();
void swap(function &that) noexcept { std::swap(*this, that); }
// 根据exec_是否为空判断是否为空函数
explicit operator bool() const noexcept { return exec_ != nullptr; }
std::size_t heapAllocatedMemory() const noexcept {
return exec(Op::HEAP, nullptr, nullptr);
}
};
template<typename functionType>
void swap(function<functionType> &lhs, function<functionType> &rhs) noexcept {
lhs.swap(rhs);
}
template<typename functionType>
bool operator==(const function<functionType> &fn, std::nullptr_t) {
return !fn;
}
template<typename functionType>
bool operator==(std::nullptr_t, const function<functionType> &fn) {
return !fn;
}
template<typename functionType>
bool operator!=(const function<functionType> &fn, std::nullptr_t) {
return !(fn == nullptr);
}
template<typename functionType>
bool operator!=(std::nullptr_t, const function<functionType> &fn) {
return !(nullptr == fn);
}
}// namespace MiniSTL