forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_function.h
178 lines (151 loc) · 4.26 KB
/
unique_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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include <functional>
#ifndef Unexpected
#define Unexpected(message) std::abort()
#define UniqueFunctionUnexpected
#endif // Unexpected
namespace base {
namespace details {
template <typename Callable>
class moveable_callable_wrap {
public:
static_assert(
std::is_move_constructible_v<Callable>,
"Should be at least moveable.");
moveable_callable_wrap(Callable &&other)
: _value(std::move(other)) {
}
moveable_callable_wrap &operator=(Callable &&other) {
_value = std::move(other);
return *this;
}
moveable_callable_wrap(moveable_callable_wrap &&other)
: _value(std::move(other._value)) {
}
moveable_callable_wrap(
const moveable_callable_wrap &other)
: _value(fail_construct()) {
}
moveable_callable_wrap &operator=(
moveable_callable_wrap &&other) {
_value = std::move(other._value);
return *this;
}
moveable_callable_wrap &operator=(
const moveable_callable_wrap &other) {
return fail_assign();
}
template <typename ...Args>
decltype(auto) operator()(Args &&...args) {
return _value(std::forward<Args>(args)...);
}
private:
[[noreturn]] Callable fail_construct() {
Unexpected("Attempt to copy-construct a move-only type.");
}
[[noreturn]] moveable_callable_wrap &fail_assign() {
Unexpected("Attempt to copy-assign a move-only type.");
}
Callable _value;
};
} // namespace details
template <typename Function>
class unique_function;
template <typename Return, typename ...Args>
class unique_function<Return(Args...)> final {
public:
unique_function(std::nullptr_t = nullptr) noexcept {
}
unique_function(const unique_function &other) = delete;
unique_function &operator=(const unique_function &other) = delete;
// Move construct / assign from the same type.
unique_function(unique_function &&other)
: _impl(std::move(other._impl)) {
}
unique_function &operator=(unique_function &&other) {
_impl = std::move(other._impl);
return *this;
}
template <
typename Callable,
typename = std::enable_if_t<
std::is_convertible_v<
decltype(std::declval<Callable>()(
std::declval<Args>()...)),
Return>>>
unique_function(Callable &&other)
: unique_function(
std::forward<Callable>(other),
std::is_copy_constructible<std::decay_t<Callable>>{}) {
}
template <
typename Callable,
typename = std::enable_if_t<
std::is_convertible_v<
decltype(std::declval<Callable>()(
std::declval<Args>()...)),
Return>>>
unique_function &operator=(Callable &&other) {
using Decayed = std::decay_t<Callable>;
if constexpr (std::is_copy_constructible_v<Decayed>) {
_impl = std::forward<Callable>(other);
} else if constexpr (std::is_move_constructible_v<Decayed>) {
_impl = details::moveable_callable_wrap<Decayed>(
std::forward<Callable>(other));
} else {
static_assert(false_t(other), "Should be moveable.");
}
return *this;
}
void swap(unique_function &other) {
_impl.swap(other._impl);
}
Return operator()(Args ...args) {
return _impl(std::forward<Args>(args)...);
}
explicit operator bool() const {
return _impl.operator bool();
}
friend inline bool operator==(
const unique_function &value,
std::nullptr_t) noexcept {
return value._impl == nullptr;
}
friend inline bool operator==(
std::nullptr_t,
const unique_function &value) noexcept {
return value._impl == nullptr;
}
friend inline bool operator!=(
const unique_function &value,
std::nullptr_t) noexcept {
return value._impl != nullptr;
}
friend inline bool operator!=(
std::nullptr_t,
const unique_function &value) noexcept {
return value._impl != nullptr;
}
private:
template <typename Callable>
unique_function(Callable &&other, std::true_type)
: _impl(std::forward<Callable>(other)) {
}
template <typename Callable>
unique_function(Callable &&other, std::false_type)
: _impl(details::moveable_callable_wrap<std::decay_t<Callable>>(
std::forward<Callable>(other))) {
}
std::function<Return(Args...)> _impl;
};
} // namespace base
#ifdef UniqueFunctionUnexpected
#undef UniqueFunctionUnexpected
#undef Unexpected
#endif // UniqueFunctionUnexpectedb