-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathforwarder.hpp
195 lines (152 loc) · 4.1 KB
/
forwarder.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
#ifndef GNR_FORWARDER_HPP
# define GNR_FORWARDER_HPP
# pragma once
#include <cassert>
// std::size_t
#include <cstddef>
#include <functional>
#include <type_traits>
#include <utility>
namespace gnr
{
namespace
{
constexpr auto const default_noexcept =
#if defined(__cpp_exceptions) && __cpp_exceptions
false;
#else
true;
#endif // __cpp_exceptions
constexpr auto const default_size = 4 * sizeof(void*);
}
template <typename F,
std::size_t N = default_size,
bool NE = default_noexcept
>
class forwarder;
template <typename R, typename ...A, std::size_t N, bool NE>
class forwarder<R (A...), N, NE>
{
R (*stub_)(void*, A&&...) noexcept(NE) {};
std::aligned_storage_t<N> store_;
template<typename T, typename ...U, std::size_t M>
friend bool operator==(forwarder<T (U...), M> const&,
std::nullptr_t) noexcept;
template<typename T, typename ...U, std::size_t M>
friend bool operator==(std::nullptr_t,
forwarder<T (U...), M> const&) noexcept;
template<typename T, typename ...U, std::size_t M>
friend bool operator!=(forwarder<T (U...), M> const&,
std::nullptr_t) noexcept;
template<typename T, typename ...U, std::size_t M>
friend bool operator!=(std::nullptr_t,
forwarder<T (U...), M> const&) noexcept;
public:
using result_type = R;
enum : std::size_t { size = N };
public:
forwarder() = default;
forwarder(forwarder const&) = default;
forwarder(forwarder&&) = default;
template <typename F,
typename = std::enable_if_t<!std::is_same<std::decay_t<F>, forwarder>{}>
>
forwarder(F&& f) noexcept
{
assign(std::forward<F>(f));
}
forwarder& operator=(forwarder const&) = default;
forwarder& operator=(forwarder&&) = default;
template <typename F,
typename = std::enable_if_t<!std::is_same<std::decay_t<F>, forwarder>{}>
>
forwarder& operator=(F&& f) noexcept
{
assign(std::forward<F>(f));
return *this;
}
explicit operator bool() const noexcept { return stub_; }
R operator()(A... args) const
noexcept(
noexcept(
stub_(const_cast<void*>(static_cast<void const*>(&store_)),
std::forward<A>(args)...
)
)
)
{
//assert(stub_);
return stub_(const_cast<void*>(static_cast<void const*>(&store_)),
std::forward<A>(args)...
);
}
void assign(std::nullptr_t) noexcept
{
reset();
}
template <typename F>
void assign(F&& f) noexcept
{
using functor_type = std::decay_t<F>;
static_assert(sizeof(functor_type) <= sizeof(store_),
"functor too large");
static_assert(std::is_trivially_copyable<functor_type>{},
"functor not trivially copyable");
::new (static_cast<void*>(&store_)) functor_type(std::forward<F>(f));
stub_ = [](void* const ptr, A&&... args) noexcept(noexcept(NE)) -> R
{
#if __cplusplus <= 201402L
return (*static_cast<functor_type*>(ptr))(
std::forward<A>(args)...);
#else
return std::invoke(*static_cast<functor_type*>(ptr),
std::forward<A>(args)...);
#endif // __cplusplus
};
}
void reset() noexcept { stub_ = nullptr; }
void swap(forwarder& other) noexcept
{
std::swap(*this, other);
}
void swap(forwarder&& other) noexcept
{
std::swap(*this, std::move(other));
}
template <typename T>
auto target() noexcept
{
return reinterpret_cast<T*>(&store_);
}
template <typename T>
auto target() const noexcept
{
return reinterpret_cast<T const*>(&store_);
}
};
template<typename R, typename ...A, std::size_t N>
bool operator==(forwarder<R (A...), N> const& f,
std::nullptr_t const) noexcept
{
return nullptr == f.stub_ ;
}
template<typename R, typename ...A, std::size_t N>
bool operator==(std::nullptr_t const,
forwarder<R (A...), N> const& f) noexcept
{
return nullptr == f.stub_;
}
template<typename R, typename ...A, std::size_t N>
bool operator!=(forwarder<R (A...), N> const& f,
std::nullptr_t const) noexcept
{
return !operator==(nullptr, f);
}
template<typename R, typename ...A, std::size_t N>
bool operator!=(std::nullptr_t const,
forwarder<R (A...), N> const& f) noexcept
{
return !operator==(nullptr, f);
}
}
#endif // GNR_FORWARDER_HPP