/
basic_function.hpp
328 lines (273 loc) · 9.06 KB
/
basic_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
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
// Copyright (c) 2011 Thomas Heller
// Copyright (c) 2013 Hartmut Kaiser
// Copyright (c) 2014 Agustin Berge
// Copyright (c) 2017 Google
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef HPX_UTIL_DETAIL_BASIC_FUNCTION_HPP
#define HPX_UTIL_DETAIL_BASIC_FUNCTION_HPP
#include <hpx/config.hpp>
#include <hpx/runtime/serialization/serialization_fwd.hpp>
#include <hpx/traits/get_function_address.hpp>
#include <hpx/traits/get_function_annotation.hpp>
#include <hpx/traits/is_callable.hpp>
#include <hpx/util/detail/empty_function.hpp>
#include <hpx/util/detail/vtable/serializable_function_vtable.hpp>
#include <hpx/util/detail/vtable/serializable_vtable.hpp>
#include <hpx/util/detail/vtable/vtable.hpp>
#include <cstddef>
#include <cstring>
#include <string>
#include <type_traits>
#include <utility>
namespace hpx { namespace util { namespace detail
{
///////////////////////////////////////////////////////////////////////////
template <typename F>
static bool is_empty_function(F const&, std::false_type) noexcept
{
return false;
}
template <typename F>
static bool is_empty_function(F const& f, std::true_type) noexcept
{
return f == nullptr;
}
template <typename F>
static bool is_empty_function(F const& f) noexcept
{
std::integral_constant<bool,
std::is_pointer<F>::value
|| std::is_member_pointer<F>::value
> is_pointer;
return is_empty_function(f, is_pointer);
}
///////////////////////////////////////////////////////////////////////////
template <typename VTable, typename Sig>
class function_base;
template <typename VTable, typename R, typename ...Ts>
class function_base<VTable, R(Ts...)>
{
public:
function_base() noexcept
: vptr(detail::get_empty_function_vtable<VTable>())
{
std::memset(object, 0, vtable::function_storage_size);
vtable::default_construct<empty_function>(object);
}
function_base(function_base&& other) noexcept
: vptr(other.vptr)
{
// move-construct
std::memcpy(object, other.object, vtable::function_storage_size);
other.vptr = detail::get_empty_function_vtable<VTable>();
vtable::default_construct<empty_function>(other.object);
}
~function_base()
{
vptr->delete_(object);
}
function_base& operator=(function_base&& other) noexcept
{
if (this != &other)
{
swap(other);
other.reset();
}
return *this;
}
void assign(std::nullptr_t) noexcept
{
reset();
}
template <typename F>
void assign(F&& f)
{
if (!is_empty_function(f))
{
typedef typename std::decay<F>::type target_type;
VTable const* f_vptr = get_vtable<target_type>();
if (vptr == f_vptr)
{
vtable::reconstruct<target_type>(object, std::forward<F>(f));
} else {
reset();
vtable::_delete<empty_function>(object);
vptr = f_vptr;
vtable::construct<target_type>(object, std::forward<F>(f));
}
} else {
reset();
}
}
void reset() noexcept
{
if (!empty())
{
vptr->delete_(object);
vptr = detail::get_empty_function_vtable<VTable>();
vtable::default_construct<empty_function>(object);
}
}
void swap(function_base& f) noexcept
{
std::swap(vptr, f.vptr);
std::swap(object, f.object); // swap
}
bool empty() const noexcept
{
return vptr->empty;
}
explicit operator bool() const noexcept
{
return !empty();
}
template <typename T>
T* target() noexcept
{
typedef typename std::remove_cv<T>::type target_type;
static_assert(
traits::is_invocable_r<R, target_type&, Ts...>::value
, "T shall be Callable with the function signature");
VTable const* f_vptr = get_vtable<target_type>();
if (vptr != f_vptr || empty())
return nullptr;
return &vtable::get<target_type>(object);
}
template <typename T>
T const* target() const noexcept
{
typedef typename std::remove_cv<T>::type target_type;
static_assert(
traits::is_invocable_r<R, target_type&, Ts...>::value
, "T shall be Callable with the function signature");
VTable const* f_vptr = get_vtable<target_type>();
if (vptr != f_vptr || empty())
return nullptr;
return &vtable::get<target_type>(object);
}
HPX_FORCEINLINE R operator()(Ts... vs) const
{
return vptr->invoke(object, std::forward<Ts>(vs)...);
}
std::size_t get_function_address() const
{
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
return vptr->get_function_address(object);
#else
return 0;
#endif
}
char const* get_function_annotation() const
{
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
return vptr->get_function_annotation(object);
#else
return nullptr;
#endif
}
util::itt::string_handle get_function_annotation_itt() const
{
#if HPX_HAVE_ITTNOTIFY != 0 && !defined(HPX_HAVE_APEX)
return vptr->get_function_annotation_itt(object);
#else
static util::itt::string_handle sh;
return sh;
#endif
}
private:
template <typename T>
static VTable const* get_vtable() noexcept
{
return detail::get_vtable<VTable, T>();
}
protected:
VTable const *vptr;
mutable void* object[(vtable::function_storage_size / sizeof(void*))];
};
template <typename Sig, typename VTable>
static bool is_empty_function(function_base<VTable, Sig> const& f) noexcept
{
return f.empty();
}
///////////////////////////////////////////////////////////////////////////
template <typename VTable, typename Sig, bool Serializable>
class basic_function;
template <typename VTable, typename R, typename ...Ts>
class basic_function<VTable, R(Ts...), true>
: public function_base<
serializable_function_vtable<VTable>
, R(Ts...)
>
{
typedef serializable_function_vtable<VTable> vtable;
typedef function_base<vtable, R(Ts...)> base_type;
public:
typedef R result_type;
basic_function() noexcept
: base_type()
{}
basic_function(basic_function&& other) noexcept
: base_type(static_cast<base_type&&>(other))
{}
basic_function& operator=(basic_function&& other) noexcept
{
base_type::operator=(static_cast<base_type&&>(other));
return *this;
}
private:
friend class hpx::serialization::access;
void load(serialization::input_archive& ar, const unsigned version)
{
this->reset();
bool is_empty = false;
ar >> is_empty;
if (!is_empty)
{
std::string name;
ar >> name;
this->vptr = detail::get_vtable<vtable>(name);
this->vptr->load_object(this->object, ar, version);
}
}
void save(serialization::output_archive& ar, const unsigned version) const
{
bool is_empty = this->empty();
ar << is_empty;
if (!is_empty)
{
std::string function_name = this->vptr->name;
ar << function_name;
this->vptr->save_object(this->object, ar, version);
}
}
HPX_SERIALIZATION_SPLIT_MEMBER()
};
template <typename VTable, typename R, typename ...Ts>
class basic_function<VTable, R(Ts...), false>
: public function_base<VTable, R(Ts...)>
{
typedef function_base<VTable, R(Ts...)> base_type;
public:
typedef R result_type;
basic_function() noexcept
: base_type()
{}
basic_function(basic_function&& other) noexcept
: base_type(static_cast<base_type&&>(other))
{}
basic_function& operator=(basic_function&& other) noexcept
{
base_type::operator=(static_cast<base_type&&>(other));
return *this;
}
};
template <typename Sig, typename VTable, bool Serializable>
static bool is_empty_function(
basic_function<VTable, Sig, Serializable> const& f) noexcept
{
return f.empty();
}
}}}
#endif