-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.h
161 lines (134 loc) · 4.17 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
#ifndef FUNCTION_H
#define FUNCTION_H
#include <memory>
#include <type_traits>
namespace my_function {
template <typename>
struct function;
template <typename T, typename... Args>
struct function<T(Args...)> {
private:
constexpr static const size_t BUFFER_SIZE = 2 * sizeof(size_t);
struct concept_ {
virtual T call(Args &&... args) const = 0;
virtual void copy_to_buffer(void *buffer) const = 0;
virtual void move_to_buffer(void *buffer) noexcept = 0;
virtual std::unique_ptr<concept_> copy() const = 0;
virtual ~concept_() = default;
};
template <typename M>
struct model : concept_ {
private:
M m;
public:
explicit model(M &&m): m(std::move(m)) {}
explicit model(M const &m) : m(m) {}
T call(Args &&... args) const override {
return m(std::forward<Args>(args)...);
}
void copy_to_buffer(void *buffer) const override {
new (buffer) model<M>(m);
}
void move_to_buffer(void *buffer) noexcept override {
new (buffer) model<M>(std::move(m));
}
std::unique_ptr<concept_> copy() const override {
return std::make_unique<model<M>>(m);
}
~model() override = default;
};
private:
bool is_small;
union {
typename std::aligned_storage<BUFFER_SIZE, alignof(size_t)>::type buffer;
std::unique_ptr<concept_> ptr;
};
void move_from(function &&other){
is_small = other.is_small;
if(is_small){
reinterpret_cast<concept_ *>(&other.buffer)->move_to_buffer(&buffer);
other = function();
} else {
new (&ptr) std::unique_ptr<concept_>(std::move(other.ptr));
}
}
public:
function() noexcept : is_small(false), ptr(nullptr) {}
function(std::nullptr_t) noexcept : is_small(false), ptr(nullptr) {}
function(const function& other) noexcept {
if(&other == this){
return;
}
is_small = other.is_small;
if(is_small) {
reinterpret_cast<const concept_*>(&other.buffer)->copy_to_buffer(&buffer);
} else {
ptr = std::move(other.ptr->copy());
}
}
function(function&& other) noexcept {
if(&other == this){
return;
}
move_from(std::move(other));
}
template<typename F>
function(F f) {
if constexpr (std::is_nothrow_move_constructible<F>::value && sizeof(model<F>) <= BUFFER_SIZE && alignof(model<F>) <=
alignof(size_t)) {
is_small = true;
new (&buffer) model<F>(std::move(f));
} else {
is_small = false;
new (&ptr) std::unique_ptr<concept_>(std::make_unique<model<F>>(std::move(f)));
}
}
~function() {
if (is_small){
(reinterpret_cast<concept_ *>(&buffer))->~concept_();
} else {
ptr.~unique_ptr();
}
}
function& operator=(const function& other) {
if(&other == this){
return *this;
}
function t(other);
swap(t);
return *this;
}
function& operator=(function&& other) noexcept {
if(&other == this){
return *this;
}
if (is_small){
(reinterpret_cast<concept_ *>(&buffer))->~concept_();
} else {
ptr.~unique_ptr();
}
move_from(std::move(other));
return *this;
}
void swap(function& other) noexcept {
function t(std::move(other));
other = std::move(*this);
*this = std::move(t);
}
explicit operator bool() const noexcept {
return is_small || ptr;
}
T operator()(Args... args) const {
if (is_small) {
return reinterpret_cast<concept_ const *>(&buffer)->call(std::forward<Args>(args)...);
} else {
return ptr->call(std::forward<Args>(args)...);
}
}
};
template <typename T, typename... Args>
void swap(function<T(Args...)> &lhs, function<T(Args...)> &rhs) noexcept {
lhs.swap(rhs);
}
}
#endif // FUNCTION_H