1#ifndef NEFORCE_CORE_FUNCTIONAL_BIND_HPP__
2#define NEFORCE_CORE_FUNCTIONAL_BIND_HPP__
15NEFORCE_BEGIN_NAMESPACE__
20template <
typename Res,
typename... Args>
21struct unary_or_binary_function {};
23template <
typename Res,
typename T1>
24struct unary_or_binary_function<Res, T1> : _NEFORCE
unary_function<T1, Res> {};
26template <
typename Res,
typename T1,
typename T2>
27struct unary_or_binary_function<Res, T1, T2> : _NEFORCE
binary_function<T1, T2, Res> {};
34template <
typename Sign>
35struct mem_func_traits;
37template <
typename Res,
typename Class,
typename... Args>
38struct mem_func_traits_base {
39 using result_type = Res;
40 using maybe_type = unary_or_binary_function<Res, Class*, Args...>;
41 using arity = integral_constant<
size_t,
sizeof...(Args)>;
44#define __NEFORCE_MEMFUNC_TRAITS_BASE(CV, REF) \
45 template <typename Res, typename Class, typename... Args> \
46 struct mem_func_traits<Res (Class::*)(Args...) CV REF> : mem_func_traits_base<Res, CV Class, Args...> { \
47 using vararg = false_type; \
49 template <typename Res, typename Class, typename... Args> \
50 struct mem_func_traits<Res (Class::*)(Args..., ...) CV REF> : mem_func_traits_base<Res, CV Class, Args...> { \
51 using vararg = true_type; \
54#define __NEFORCE_MEMFUNC_TRAITS(REF, P) \
55 __NEFORCE_MEMFUNC_TRAITS_BASE(, REF) \
56 __NEFORCE_MEMFUNC_TRAITS_BASE(const, REF) \
57 __NEFORCE_MEMFUNC_TRAITS_BASE(volatile, REF) \
58 __NEFORCE_MEMFUNC_TRAITS_BASE(const volatile, REF)
60__NEFORCE_MEMFUNC_TRAITS(, )
61__NEFORCE_MEMFUNC_TRAITS(&, )
62__NEFORCE_MEMFUNC_TRAITS(&&, )
64#ifdef NEFORCE_STANDARD_17
65__NEFORCE_MEMFUNC_TRAITS(
noexcept, )
66__NEFORCE_MEMFUNC_TRAITS(& noexcept, )
67__NEFORCE_MEMFUNC_TRAITS(&& noexcept, )
70#undef __NEFORCE_MEMFUNC_TRAITS_BASE
71#undef __NEFORCE_MEMFUNC_TRAITS
74template <
typename Func,
typename =
void_t<>>
75struct maybe_get_result_type {};
77template <
typename Func>
78struct maybe_get_result_type<Func,
void_t<typename Func::result_type>> {
79 using result_type =
typename Func::result_type;
83template <
typename Func>
84struct __weak_result_type_impl : maybe_get_result_type<Func> {};
86template <
typename Res,
typename... Args>
87struct __weak_result_type_impl<Res(Args...)> {
88 using result_type = Res;
90template <
typename Res,
typename... Args>
91struct __weak_result_type_impl<Res(Args..., ...)> {
92 using result_type = Res;
94template <
typename Res,
typename... Args>
95struct __weak_result_type_impl<Res (*)(Args...)> {
96 using result_type = Res;
98template <
typename Res,
typename... Args>
99struct __weak_result_type_impl<Res (*)(Args..., ...)> {
100 using result_type = Res;
103#ifdef NEFORCE_STANDARD_17
104template <
typename Res,
typename... Args>
105struct __weak_result_type_impl<Res(Args...) noexcept> {
106 using result_type = Res;
108template <
typename Res,
typename... Args>
109struct __weak_result_type_impl<Res(Args..., ...) noexcept> {
110 using result_type = Res;
112template <
typename Res,
typename... Args>
113struct __weak_result_type_impl<Res (*)(Args...)
noexcept> {
114 using result_type = Res;
116template <
typename Res,
typename... Args>
117struct __weak_result_type_impl<Res (*)(Args..., ...)
noexcept> {
118 using result_type = Res;
123template <typename Func, bool = is_member_function_pointer<Func>::value>
124struct __weak_result_type_memfun : __weak_result_type_impl<Func> {};
126template <
typename MemFunPtr>
127struct __weak_result_type_memfun<MemFunPtr, true> {
128 using result_type =
typename mem_func_traits<MemFunPtr>::result_type;
131template <
typename Func,
typename Class>
132struct __weak_result_type_memfun<Func Class::*, false> {};
141template <
typename Func>
142struct weak_result_type : __weak_result_type_memfun<remove_cv_t<Func>> {};
151template <
typename MemberPtr,
bool IsMemFunc = is_member_function_po
inter_v<MemberPtr>>
152class mem_func_base :
public inner::mem_func_traits<MemberPtr>::maybe_type {
153 using Traits = inner::mem_func_traits<MemberPtr>;
154 using Arity =
typename Traits::arity;
155 using Varargs =
typename Traits::vararg;
157 template <
typename Func,
typename... BoundArgs>
158 friend struct bind_check_arity;
163 using result_type =
typename Traits::result_type;
169 explicit constexpr mem_func_base(MemberPtr pmf) noexcept :
178 template <
typename... Args>
179 NEFORCE_CONSTEXPR20
auto operator()(Args&&... args)
const
189template <
typename MemberObjPtr>
190class mem_func_base<MemberObjPtr, false> {
191 using Arity = integral_constant<size_t, 0>;
194 template <
typename Func,
typename... BoundArgs>
195 friend struct bind_check_arity;
204 explicit constexpr mem_func_base(MemberObjPtr pm) noexcept :
213 template <
typename T>
214 NEFORCE_CONSTEXPR20
auto operator()(T&& obj)
const
221template <
typename MemberPo
inter>
224template <
typename Res,
typename Class>
225struct mem_func<Res Class::*> : mem_func_base<Res Class::*> {
226 using mem_func_base<Res Class::*>::mem_func_base;
260template <u
int32_t Num>
272template <u
int32_t Num>
337template <
typename Arg,
bool IsBindExp = is_bind_expression_v<Arg>,
bool IsPlaceholder = is_placeholder_v<Arg> != 0>
338class bind_arg_mapper;
343 template <
typename CVRef,
typename Tuple>
344 NEFORCE_CONSTEXPR20 T& operator()(CVRef& arg, Tuple& )
const volatile {
349template <
typename Arg>
350class bind_arg_mapper<Arg, true, false> {
352 template <
typename CVArg,
typename... Args>
353 NEFORCE_CONSTEXPR20
auto operator()(CVArg& arg, tuple<Args...>& tuple_ref)
const volatile
356 return call(arg, tuple_ref, Indexes());
360 template <
typename CVArg,
typename... Args,
size_t... Indexes>
361 NEFORCE_CONSTEXPR20
auto call(CVArg& arg, tuple<Args...>& tuple_ref,
362 const index_tuple<Indexes...>& )
const volatile
369template <
typename Arg>
370class bind_arg_mapper<Arg, false, true> {
371 template <
size_t I,
typename Tuple>
375 template <
typename Tuple>
377 operator()(
const volatile Arg& , Tuple& tuple_ref)
const volatile {
382template <
typename Arg>
383class bind_arg_mapper<Arg, false, false> {
385 template <
typename CVArg,
typename Tuple>
386 NEFORCE_CONSTEXPR20 CVArg&& operator()(CVArg&& arg, Tuple& )
const volatile {
410template <
typename Sign>
421template <
typename Func,
typename... BoundArgs>
422class binder<Func(BoundArgs...)> :
public inner::weak_result_type<Func> {
427 tuple<BoundArgs...> bound_args_;
436 template <
typename BoundArg,
typename CallArgs>
437 struct arg_mapper_result {
438 using type =
decltype(inner::bind_arg_mapper<remove_cv_t<BoundArg>>()(_NEFORCE
declval<BoundArg&>(),
446 template <
typename BoundArg,
typename CallArgs>
447 using arg_mapper_result_t =
typename arg_mapper_result<BoundArg, CallArgs>::type;
454 template <
typename CallArgs>
462 template <
typename CallArgs>
471 template <
typename BoundArg,
typename CallArgs>
472 using arg_mapper_type =
decltype(inner::bind_arg_mapper<remove_cv_t<BoundArg>>()(_NEFORCE
declval<BoundArg&>(),
480 template <
typename CallArgs>
493 template <
typename Res,
typename... Args,
size_t... Indexes>
495 return _NEFORCE
invoke(functor_,
496 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
508 template <
typename Res,
typename... Args, _NEFORCE
size_t... Indexes>
510 return _NEFORCE
invoke(functor_,
511 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
521 template <
typename... Args>
522 explicit NEFORCE_CONSTEXPR20
binder(Func func, Args&&... args) :
523 functor_(_NEFORCE
forward<Func>(func)),
524 bound_args_(_NEFORCE
forward<Args>(args)...) {}
535 template <
typename... Args>
536 NEFORCE_CONSTEXPR20
decltype(
auto)
operator()(Args&&... args) {
537 using Res = result_type<tuple<Args&&...>>;
547 template <
typename... Args>
548 NEFORCE_CONSTEXPR20
decltype(
auto)
operator()(Args&&... args)
const {
549 using Res = result_type_const<tuple<Args&&...>>;
553 template <
typename... Args>
554 void operator()(Args&&... args)
const volatile =
delete;
566template <
typename Res,
typename Sign>
575template <
typename Res,
typename Func,
typename... BoundArgs>
581 tuple<BoundArgs...> bound_args_;
593 template <
typename Result,
typename... Args,
size_t... Indexes>
596 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
608 template <
typename Result,
typename... Args, _NEFORCE
size_t... Indexes>
611 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
623 template <
typename... Args>
624 explicit NEFORCE_CONSTEXPR20
bindrer(Func func, Args&&... args) :
625 functor_(_NEFORCE
forward<Func>(func)),
626 bound_args_(_NEFORCE
forward<Args>(args)...) {}
637 template <
typename... Args>
648 template <
typename... Args>
653 template <
typename... Args>
654 void operator()(Args&&... args)
const volatile =
delete;
660template <
typename Sign>
662template <
typename Res,
typename Sign>
665#define __NEFORCE_EXPAND_BIND_EXP(CV) \
666 template <typename Sign> \
667 struct is_bind_expression<CV binder<Sign>> : true_type {}; \
668 template <typename Res, typename Sign> \
669 struct is_bind_expression<CV bindrer<Res, Sign>> : true_type {};
672#undef __NEFORCE_EXPAND_BIND_EXP
684template <
typename Func,
typename... BoundArgs>
685struct bind_check_arity {};
687template <
typename Ret,
typename... Args,
typename... BoundArgs>
688struct bind_check_arity<Ret (*)(Args...), BoundArgs...> {
689 static_assert(
sizeof...(BoundArgs) ==
sizeof...(Args),
"Wrong number of arguments for function");
692template <
typename Ret,
typename... Args,
typename... BoundArgs>
693struct bind_check_arity<Ret (*)(Args..., ...), BoundArgs...> {
694 static_assert(
sizeof...(BoundArgs) >=
sizeof...(Args),
"Wrong number of arguments for function");
697template <
typename T,
typename Class,
typename... BoundArgs>
698struct bind_check_arity<T Class::*, BoundArgs...> {
699 using Arity =
typename mem_func<T Class::*>::Arity;
700 using Varargs =
typename mem_func<T Class::*>::Varargs;
701 static_assert(Varargs::value ?
sizeof...(BoundArgs) >= Arity::value + 1 :
sizeof...(BoundArgs) == Arity::value + 1,
702 "Wrong number of arguments for pointer-to-member");
715template <
bool IntLike,
typename Func,
typename... BoundArgs>
716struct bind_helper : inner::bind_check_arity<decay_t<Func>, BoundArgs...> {
721template <
typename Func,
typename... BoundArgs>
728template <
bool IntLike,
typename Func,
typename... BoundArgs>
742template <
typename Func,
typename... BoundArgs>
757template <
typename Res,
typename Func,
typename... BoundArgs>
758struct bindr_helper : inner::bind_check_arity<decay_t<Func>, BoundArgs...> {
766template <
typename Res,
typename Func,
typename... BoundArgs>
781template <
typename Res,
typename Func,
typename... BoundArgs>
783NEFORCE_NODISCARD constexpr
bindr_helper_t<Res, Func, BoundArgs...> bind(Func&& func, BoundArgs&&... args) {
796template <
typename Func,
typename... BoundArgs>
799#ifdef NEFORCE_STANDARD_17
807 tuple<BoundArgs...> bound_args_;
810 template <
size_t... Indices,
typename... CallArgs>
815 template <
size_t... Indices,
typename... CallArgs>
820 template <
size_t... Indices,
typename... CallArgs>
826 template <
size_t... Indices,
typename... CallArgs>
841 template <
typename Fn,
typename... Args>
844 func_(_NEFORCE
forward<Fn>(func)),
845 bound_args_(_NEFORCE
forward<Args>(args)...) {
846 static_assert(
sizeof...(Args) ==
sizeof...(BoundArgs),
"Wrong number of arguments");
862 template <
typename... CallArgs>
863 constexpr decltype(
auto)
874 template <
typename... CallArgs>
875 constexpr decltype(
auto)
operator()(CallArgs&&... call_args)
const&
noexcept(
886 template <
typename... CallArgs>
887 constexpr decltype(
auto)
898 template <
typename... CallArgs>
899 constexpr decltype(
auto)
operator()(CallArgs&&... call_args)
const&&
noexcept(
911template <
typename Func,
typename... Args>
924template <
typename Func,
typename... Args>
932NEFORCE_END_NAMESPACE__
constexpr binder(Func func, Args &&... args)
构造函数
binder(const binder &)=default
复制构造函数
binder(binder &&)=default
移动构造函数
constexpr result_type operator()(Args &&... args)
调用操作符
bindrer(bindrer &&)=default
移动构造函数
bindrer(const bindrer &)=default
复制构造函数
constexpr bindrer(Func func, Args &&... args)
构造函数
constexpr result_type operator()(Args &&... args) const
const调用操作符
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
constexpr bool is_integral_like_v
is_integral_like的便捷变量模板
constexpr uint32_t is_placeholder_v
is_placeholder的便捷变量模板
constexpr bool is_bind_expression_v
is_bind_expression的便捷变量模板
unsigned int uint32_t
32位无符号整数类型
constexpr binder_front_type< Func, Args... > bind_front(Func &&func, Args &&... args) noexcept(is_nothrow_constructible< binder_front_type< Func, Args... >, int, Func, Args... >::value)
bind_front函数
NEFORCE_DEPRECATED_FOR("use lambda or bind_front instead of bind") const expr bind_helper_t< is_integral_like_v< Func >
bind函数
typename bindr_helper< Res, Func, BoundArgs... >::type bindr_helper_t
bindr_helper的便捷别名
binder_front< decay_t< Func >, decay_t< Args >... > binder_front_type
binder_front类型的便捷别名
typename bind_helper< IntLike, Func, BoundArgs... >::type bind_helper_t
bind_helper的便捷别名
typename build_index_tuple< Num >::type build_index_tuple_t
build_index_tuple的便捷别名
integer_sequence< size_t, Values... > index_sequence
索引序列
make_index_sequence< sizeof...(Types)> index_sequence_for
根据类型参数包生成索引序列
constexpr inner::__invoke_result_aux< Callable, Args... >::type invoke(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
统一调用接口
constexpr enable_if_t< is_invocable_r< Res, Callable, Args... >::value, Res > invoke_r(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
带返回类型检查的统一调用接口
typename inner::__invoke_result_aux< F, Args... >::type invoke_result_t
invoke_result的便捷别名
constexpr bool is_nothrow_invocable_v
is_nothrow_invocable的便捷变量模板
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr size_t tuple_size_v
tuple_size的类型别名
typename tuple_element< Index, Types... >::type tuple_element_t
tuple_element的类型别名
constexpr tuple< Types &&... > forward_as_tuple(Types &&... args) noexcept
创建转发引用元组
typename decay< T >::type decay_t
decay的便捷别名
#define NEFORCE_MACRO_RANGES_CV(MAC)
cv限定符列表宏
constexpr bool is_move_constructible_v
is_move_constructible的便捷变量模板
integral_constant< uint32_t, Value > uint32_constant
32位无符号整数常量包装器
bool_constant< true > true_type
表示true的类型
bool_constant< false > false_type
表示false的类型
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr placeholder< 15 > p15
占位符15
constexpr placeholder< 12 > p12
占位符12
constexpr placeholder< 27 > p27
占位符27
constexpr placeholder< 6 > p6
占位符6
constexpr placeholder< 1 > p1
占位符1
constexpr placeholder< 14 > p14
占位符14
constexpr placeholder< 3 > p3
占位符3
constexpr placeholder< 23 > p23
占位符23
constexpr placeholder< 22 > p22
占位符22
constexpr placeholder< 5 > p5
占位符5
constexpr placeholder< 8 > p8
占位符8
constexpr placeholder< 7 > p7
占位符7
constexpr placeholder< 2 > p2
占位符2
constexpr placeholder< 20 > p20
占位符20
constexpr placeholder< 21 > p21
占位符21
constexpr placeholder< 29 > p29
占位符29
constexpr placeholder< 25 > p25
占位符25
constexpr placeholder< 24 > p24
占位符24
constexpr placeholder< 28 > p28
占位符28
constexpr placeholder< 16 > p16
占位符16
constexpr placeholder< 10 > p10
占位符10
constexpr placeholder< 26 > p26
占位符26
constexpr placeholder< 19 > p19
占位符19
constexpr placeholder< 11 > p11
占位符11
constexpr placeholder< 18 > p18
占位符18
constexpr placeholder< 13 > p13
占位符13
constexpr placeholder< 17 > p17
占位符17
constexpr placeholder< 4 > p4
占位符4
constexpr placeholder< 9 > p9
占位符9
decay_t< Func > func_type
函数类型
binder< func_type(decay_t< BoundArgs >...)> type
推导出的binder类型
binder_front(const binder_front &)=default
复制构造函数
binder_front(binder_front &&)=default
移动构造函数
binder_front & operator=(binder_front &&)=default
移动赋值运算符
~binder_front()=default
析构函数
constexpr binder_front(int p, Fn &&func, Args &&... args) noexcept(conjunction< is_nothrow_constructible< Func, Fn >, is_nothrow_constructible< BoundArgs, Args >... >::value)
构造函数
binder_front & operator=(const binder_front &)=default
复制赋值运算符
static constexpr bool value