-
-
Notifications
You must be signed in to change notification settings - Fork 428
/
bind.hpp
607 lines (532 loc) · 19 KB
/
bind.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Copyright (c) 2011-2012 Thomas Heller
// Copyright (c) 2013-2016 Agustin Berge
//
// 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_BIND_HPP
#define HPX_UTIL_BIND_HPP
#include <hpx/config.hpp>
#include <hpx/traits/get_function_address.hpp>
#include <hpx/traits/get_function_annotation.hpp>
#include <hpx/traits/is_action.hpp>
#include <hpx/traits/is_bind_expression.hpp>
#include <hpx/traits/is_placeholder.hpp>
#include <hpx/util/assert.hpp>
#include <hpx/util/decay.hpp>
#include <hpx/util/detail/pack.hpp>
#include <hpx/util/invoke.hpp>
#include <hpx/util/invoke_fused.hpp>
#include <hpx/util/result_of.hpp>
#include <hpx/util/tuple.hpp>
#include <cstddef>
#include <type_traits>
#include <utility>
namespace hpx { namespace util
{
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <std::size_t I>
struct placeholder
{
static std::size_t const value = I;
};
template <>
struct placeholder<0>; // not a valid placeholder
}
namespace placeholders
{
HPX_STATIC_CONSTEXPR detail::placeholder<1> _1 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<2> _2 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<3> _3 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<4> _4 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<5> _5 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<6> _6 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<7> _7 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<8> _8 = {};
HPX_STATIC_CONSTEXPR detail::placeholder<9> _9 = {};
}
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename F>
class one_shot_wrapper;
///////////////////////////////////////////////////////////////////////
template <typename F, typename T>
struct bind_eval_bound_impl
{
typedef T& type;
template <typename Us>
static HPX_CONSTEXPR HPX_HOST_DEVICE
type call(T& t, Us&& /*unbound*/)
{
return t;
}
};
template <typename F, typename T>
struct bind_eval_bound_impl<one_shot_wrapper<F>, T>
{
typedef T&& type;
template <typename Us>
static HPX_CONSTEXPR HPX_HOST_DEVICE
type call(T& t, Us&& /*unbound*/)
{
return std::forward<T>(t);
}
};
template <
typename F, typename T, typename Us,
typename Enable = void
>
struct bind_eval_impl
: bind_eval_bound_impl<F, T>
{};
template <
std::size_t I, typename Us,
typename Enable = void
>
struct bind_eval_placeholder_impl
{};
template <std::size_t I, typename Us>
struct bind_eval_placeholder_impl<I, Us,
typename std::enable_if<
(I < util::tuple_size<Us>::value)
>::type
>
{
typedef typename util::tuple_element<
I, typename util::decay<Us>::type
>::type&& type;
template <typename T>
static HPX_CONSTEXPR HPX_HOST_DEVICE
type call(T&& /*t*/, Us&& unbound)
{
return util::get<I>(std::forward<Us>(unbound));
}
};
template <typename F, typename T, typename Us>
struct bind_eval_impl<F, T, Us,
typename std::enable_if<
traits::is_placeholder<T>::value != 0
>::type
> : bind_eval_placeholder_impl<
(std::size_t)traits::is_placeholder<T>::value - 1, Us
>
{};
template <typename F, typename T, typename Us>
struct bind_eval_impl<F, T, Us,
typename std::enable_if<
traits::is_bind_expression<T>::value
>::type
>
{
typedef typename util::detail::invoke_fused_result<
T&, Us
>::type type;
static HPX_CONSTEXPR HPX_HOST_DEVICE
type call(T& t, Us&& unbound)
{
return util::invoke_fused(t, std::forward<Us>(unbound));
}
};
template <typename F, typename T, typename Us>
HPX_CONSTEXPR HPX_HOST_DEVICE
typename bind_eval_impl<F, T, Us>::type
bind_eval(T& t, Us&& unbound)
{
return bind_eval_impl<F, T, Us>::call(
t, std::forward<Us>(unbound));
}
///////////////////////////////////////////////////////////////////////
template <typename Ts>
struct is_simple_bind;
template <typename ...Ts>
struct is_simple_bind<util::tuple<Ts...> >
: detail::none_of<
traits::is_placeholder<Ts>...,
traits::is_bind_expression<Ts>...
>
{};
template <typename ...Ts>
struct is_simple_bind<util::tuple<Ts...> const>
: is_simple_bind<util::tuple<Ts const...> >
{};
///////////////////////////////////////////////////////////////////////
template <typename F, typename Ts, typename Us>
struct invoke_bound_result_impl;
template <typename F, typename ...Ts, typename Us>
struct invoke_bound_result_impl<F, util::tuple<Ts...>, Us>
: util::invoke_result<
F&, typename bind_eval_impl<F, Ts, Us>::type...
>
{};
template <typename F, typename Ts>
struct invoke_bound_result_simple_impl;
template <typename F, typename ...Ts>
struct invoke_bound_result_simple_impl<F, util::tuple<Ts...> >
: util::invoke_result<F&, Ts&...>
{};
template <typename F, typename ...Ts>
struct invoke_bound_result_simple_impl<one_shot_wrapper<F>, util::tuple<Ts...> >
: util::invoke_result<F&&, Ts&&...>
{};
///////////////////////////////////////////////////////////////////////
template <typename F, typename Ts, typename Us>
struct invoke_bound_result
: std::conditional<
!detail::is_simple_bind<Ts>::value,
invoke_bound_result_impl<F, Ts, Us>,
invoke_bound_result_simple_impl<F, Ts>
>::type
{};
template <typename F, typename ...Ts, typename Us>
struct invoke_bound_result<F, util::tuple<Ts...> const, Us>
: invoke_bound_result<F, util::tuple<Ts const...>, Us>
{};
template <typename F, typename ...Ts, typename Us>
struct invoke_bound_result<
one_shot_wrapper<F> const, util::tuple<Ts...> const, Us>
{}; // one-shot wrapper is not const callable
///////////////////////////////////////////////////////////////////////
template <typename F, typename Ts, typename Us, std::size_t ...Is>
HPX_CONSTEXPR HPX_HOST_DEVICE
typename std::enable_if<
!detail::is_simple_bind<Ts>::value,
typename invoke_bound_result<F, Ts, Us>::type
>::type bound_impl(F& f, Ts& bound, Us&& unbound,
pack_c<std::size_t, Is...>)
{
return util::invoke(f,
detail::bind_eval<F, typename util::tuple_element<Is, Ts>::type>(
util::get<Is>(bound), std::forward<Us>(unbound))...);
}
template <typename F, typename Ts, typename Us, std::size_t ...Is>
HPX_CONSTEXPR HPX_HOST_DEVICE
typename std::enable_if<
detail::is_simple_bind<Ts>::value,
typename invoke_bound_result<F, Ts, Us>::type
>::type bound_impl(F& f, Ts& bound, Us&& /*unbound*/,
pack_c<std::size_t, Is...>)
{
return util::invoke(f, util::get<Is>(bound)...);
}
template <typename F, typename Ts, typename Us, std::size_t ...Is>
HPX_HOST_DEVICE
typename std::enable_if<
detail::is_simple_bind<Ts>::value,
typename invoke_bound_result<one_shot_wrapper<F>, Ts, Us>::type
>::type bound_impl(one_shot_wrapper<F>& f, Ts& bound, Us&& /*unbound*/,
pack_c<std::size_t, Is...>)
{
return util::invoke(std::move(f), std::move(util::get<Is>(bound))...);
}
template <typename F, typename ...Ts>
class bound
{
public:
bound() {} // needed for serialization
template <typename F_, typename ...Ts_, typename =
typename std::enable_if<
!std::is_same<typename std::decay<F_>::type, bound>::value
>::type>
HPX_CONSTEXPR explicit bound(F_&& f, Ts_&&... vs)
: _f(std::forward<F_>(f))
, _args(std::forward<Ts_>(vs)...)
{}
#if !defined(__NVCC__) && !defined(__CUDACC__)
bound(bound const&) = default;
bound(bound&&) = default;
#else
HPX_HOST_DEVICE bound(bound const& other)
: _f(other._f)
, _args(other._args)
{}
HPX_HOST_DEVICE bound(bound&& other)
: _f(std::move(other._f))
, _args(std::move(other._args))
{}
#endif
bound& operator=(bound const&) = delete;
template <typename ...Us>
HPX_CXX14_CONSTEXPR HPX_HOST_DEVICE
typename invoke_bound_result<
typename std::decay<F>::type,
util::tuple<typename util::decay_unwrap<Ts>::type...>,
util::tuple<Us&&...>
>::type operator()(Us&&... vs)
{
return detail::bound_impl(_f, _args,
util::forward_as_tuple(std::forward<Us>(vs)...),
typename detail::make_index_pack<sizeof...(Ts)>::type());
}
template <typename ...Us>
HPX_CONSTEXPR HPX_HOST_DEVICE
typename invoke_bound_result<
typename std::decay<F>::type const,
util::tuple<typename util::decay_unwrap<Ts>::type...> const,
util::tuple<Us&&...>
>::type operator()(Us&&... vs) const
{
return detail::bound_impl(_f, _args,
util::forward_as_tuple(std::forward<Us>(vs)...),
typename detail::make_index_pack<sizeof...(Ts)>::type());
}
template <typename Archive>
void serialize(Archive& ar, unsigned int const /*version*/)
{
ar & _f;
ar & _args;
}
std::size_t get_function_address() const
{
return traits::get_function_address<
typename std::decay<F>::type
>::call(_f);
}
char const* get_function_annotation() const
{
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
return traits::get_function_annotation<
typename std::decay<F>::type
>::call(_f);
#else
return nullptr;
#endif
}
#if HPX_HAVE_ITTNOTIFY != 0 && !defined(HPX_HAVE_APEX)
util::itt::string_handle get_function_annotation_itt() const
{
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
return traits::get_function_annotation_itt<
typename std::decay<F>::type
>::call(_f);
#else
static util::itt::string_handle sh("bound");
return sh;
#endif
}
#endif
private:
typename std::decay<F>::type _f;
util::tuple<typename util::decay_unwrap<Ts>::type...> _args;
};
}
///////////////////////////////////////////////////////////////////////////
template <typename F, typename ...Ts>
HPX_CONSTEXPR typename std::enable_if<
!traits::is_action<typename util::decay<F>::type>::value
, detail::bound<
typename std::decay<F>::type,
typename std::decay<Ts>::type...>
>::type
bind(F&& f, Ts&&... vs)
{
typedef detail::bound<
typename std::decay<F>::type,
typename std::decay<Ts>::type...
> result_type;
return result_type(std::forward<F>(f), std::forward<Ts>(vs)...);
}
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename F>
class one_shot_wrapper //-V690
{
public:
# if !defined(HPX_DISABLE_ASSERTS)
// default constructor is needed for serialization
HPX_CONSTEXPR one_shot_wrapper()
: _called(false)
{}
HPX_CONSTEXPR explicit one_shot_wrapper(F const& f)
: _f(f)
, _called(false)
{}
HPX_CONSTEXPR explicit one_shot_wrapper(F&& f)
: _f(std::move(f))
, _called(false)
{}
HPX_CXX14_CONSTEXPR one_shot_wrapper(one_shot_wrapper&& other)
: _f(std::move(other._f))
, _called(other._called)
{
other._called = true;
}
void check_call()
{
HPX_ASSERT(!_called);
_called = true;
}
# else
// default constructor is needed for serialization
HPX_CONSTEXPR one_shot_wrapper()
{}
HPX_CONSTEXPR explicit one_shot_wrapper(F const& f)
: _f(f)
{}
HPX_CONSTEXPR explicit one_shot_wrapper(F&& f)
: _f(std::move(f))
{}
HPX_CONSTEXPR one_shot_wrapper(one_shot_wrapper&& other)
: _f(std::move(other._f))
{}
void check_call()
{}
# endif
template <typename ...Ts>
HPX_CXX14_CONSTEXPR HPX_HOST_DEVICE
typename util::invoke_result<F, Ts...>::type
operator()(Ts&&... vs)
{
check_call();
return util::invoke(std::move(_f), std::forward<Ts>(vs)...);
}
template <typename Archive>
void serialize(Archive& ar, unsigned int const /*version*/)
{
ar & _f;
}
std::size_t get_function_address() const
{
return traits::get_function_address<F>::call(_f);
}
char const* get_function_annotation() const
{
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
return traits::get_function_annotation<F>::call(_f);
#else
return nullptr;
#endif
}
#if HPX_HAVE_ITTNOTIFY != 0 && !defined(HPX_HAVE_APEX)
util::itt::string_handle get_function_annotation_itt() const
{
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
return traits::get_function_annotation_itt<F>::call(_f);
#else
static util::itt::string_handle sh("one_shot_wrapper");
return sh;
#endif
}
#endif
public: // exposition-only
F _f;
# if !defined(HPX_DISABLE_ASSERTS)
bool _called;
# endif
};
}
template <typename F>
HPX_CONSTEXPR detail::one_shot_wrapper<typename util::decay<F>::type>
one_shot(F&& f)
{
typedef
detail::one_shot_wrapper<typename util::decay<F>::type>
result_type;
return result_type(std::forward<F>(f));
}
}}
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace traits
{
///////////////////////////////////////////////////////////////////////////
template <typename F, typename ...Ts>
struct is_bind_expression<util::detail::bound<F, Ts...> >
: std::true_type
{};
///////////////////////////////////////////////////////////////////////////
template <std::size_t I>
struct is_placeholder<util::detail::placeholder<I> >
: std::integral_constant<int, I>
{};
///////////////////////////////////////////////////////////////////////////
#if defined(HPX_HAVE_THREAD_DESCRIPTION)
template <typename F, typename ...Ts>
struct get_function_address<util::detail::bound<F, Ts...> >
{
static std::size_t
call(util::detail::bound<F, Ts...> const& f) noexcept
{
return f.get_function_address();
}
};
template <typename F>
struct get_function_address<util::detail::one_shot_wrapper<F> >
{
static std::size_t
call(util::detail::one_shot_wrapper<F> const& f) noexcept
{
return f.get_function_address();
}
};
///////////////////////////////////////////////////////////////////////////
template <typename F, typename ...Ts>
struct get_function_annotation<util::detail::bound<F, Ts...> >
{
static char const*
call(util::detail::bound<F, Ts...> const& f) noexcept
{
return f.get_function_annotation();
}
};
template <typename F>
struct get_function_annotation<util::detail::one_shot_wrapper<F> >
{
static char const*
call(util::detail::one_shot_wrapper<F> const& f) noexcept
{
return f.get_function_annotation();
}
};
#if HPX_HAVE_ITTNOTIFY != 0 && !defined(HPX_HAVE_APEX)
template <typename F, typename ...Ts>
struct get_function_annotation_itt<util::detail::bound<F, Ts...> >
{
static util::itt::string_handle
call(util::detail::bound<F, Ts...> const& f) noexcept
{
return f.get_function_annotation_itt();
}
};
template <typename F>
struct get_function_annotation_itt<util::detail::one_shot_wrapper<F> >
{
static util::itt::string_handle
call(util::detail::one_shot_wrapper<F> const& f) noexcept
{
return f.get_function_annotation_itt();
}
};
#endif
#endif
}}
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace serialization
{
// serialization of the bound object
template <typename Archive, typename F, typename ...Ts>
void serialize(
Archive& ar
, ::hpx::util::detail::bound<F, Ts...>& bound
, unsigned int const version = 0)
{
bound.serialize(ar, version);
}
template <typename Archive, typename F>
void serialize(
Archive& ar
, ::hpx::util::detail::one_shot_wrapper<F>& one_shot_wrapper
, unsigned int const version = 0)
{
one_shot_wrapper.serialize(ar, version);
}
// serialization of placeholders is trivial, just provide empty functions
template <typename Archive, std::size_t I>
void serialize(
Archive& ar
, ::hpx::util::detail::placeholder<I>& /*placeholder*/
, unsigned int const /*version*/ = 0)
{}
}}
#endif