1#ifndef NEFORCE_CORE_FUNCTIONAL_BIND_HPP__
2#define NEFORCE_CORE_FUNCTIONAL_BIND_HPP__
12NEFORCE_BEGIN_NAMESPACE__
17template <
typename Res,
typename... Args>
18struct unary_or_binary_function {};
20template <
typename Res,
typename T1>
21struct unary_or_binary_function<Res, T1> : _NEFORCE
unary_function<T1, Res> {};
23template <
typename Res,
typename T1,
typename T2>
24struct unary_or_binary_function<Res, T1, T2> : _NEFORCE
binary_function<T1, T2, Res> {};
31template <
typename Sign>
32struct mem_func_traits;
34template <
typename Res,
typename Class,
typename... Args>
35struct mem_func_traits_base {
36 using result_type = Res;
37 using maybe_type = unary_or_binary_function<Res, Class*, Args...>;
38 using arity = integral_constant<
size_t,
sizeof...(Args)>;
41#define __NEFORCE_MEMFUNC_TRAITS_BASE(CV, REF) \
42 template <typename Res, typename Class, typename... Args> \
43 struct mem_func_traits<Res (Class::*)(Args...) CV REF> : mem_func_traits_base<Res, CV Class, Args...> { \
44 using vararg = false_type; \
46 template <typename Res, typename Class, typename... Args> \
47 struct mem_func_traits<Res (Class::*)(Args..., ...) CV REF> : mem_func_traits_base<Res, CV Class, Args...> { \
48 using vararg = true_type; \
51#define __NEFORCE_MEMFUNC_TRAITS(REF, P) \
52 __NEFORCE_MEMFUNC_TRAITS_BASE(, REF) \
53 __NEFORCE_MEMFUNC_TRAITS_BASE(const, REF) \
54 __NEFORCE_MEMFUNC_TRAITS_BASE(volatile, REF) \
55 __NEFORCE_MEMFUNC_TRAITS_BASE(const volatile, REF)
57__NEFORCE_MEMFUNC_TRAITS(, )
58__NEFORCE_MEMFUNC_TRAITS(&, )
59__NEFORCE_MEMFUNC_TRAITS(&&, )
61#ifdef NEFORCE_STANDARD_17
62__NEFORCE_MEMFUNC_TRAITS(
noexcept, )
63__NEFORCE_MEMFUNC_TRAITS(& noexcept, )
64__NEFORCE_MEMFUNC_TRAITS(&& noexcept, )
67#undef __NEFORCE_MEMFUNC_TRAITS_BASE
68#undef __NEFORCE_MEMFUNC_TRAITS
71template <
typename Func,
typename =
void_t<>>
72struct maybe_get_result_type {};
74template <
typename Func>
75struct maybe_get_result_type<Func,
void_t<typename Func::result_type>> {
76 using result_type =
typename Func::result_type;
80template <
typename Func>
81struct __weak_result_type_impl : maybe_get_result_type<Func> {};
83template <
typename Res,
typename... Args>
84struct __weak_result_type_impl<Res(Args...)> {
85 using result_type = Res;
87template <
typename Res,
typename... Args>
88struct __weak_result_type_impl<Res(Args..., ...)> {
89 using result_type = Res;
91template <
typename Res,
typename... Args>
92struct __weak_result_type_impl<Res (*)(Args...)> {
93 using result_type = Res;
95template <
typename Res,
typename... Args>
96struct __weak_result_type_impl<Res (*)(Args..., ...)> {
97 using result_type = Res;
100#ifdef NEFORCE_STANDARD_17
101template <
typename Res,
typename... Args>
102struct __weak_result_type_impl<Res(Args...) noexcept> {
103 using result_type = Res;
105template <
typename Res,
typename... Args>
106struct __weak_result_type_impl<Res(Args..., ...) noexcept> {
107 using result_type = Res;
109template <
typename Res,
typename... Args>
110struct __weak_result_type_impl<Res (*)(Args...)
noexcept> {
111 using result_type = Res;
113template <
typename Res,
typename... Args>
114struct __weak_result_type_impl<Res (*)(Args..., ...)
noexcept> {
115 using result_type = Res;
120template <typename Func, bool = is_member_function_pointer<Func>::value>
121struct __weak_result_type_memfun : __weak_result_type_impl<Func> {};
123template <
typename MemFunPtr>
124struct __weak_result_type_memfun<MemFunPtr, true> {
125 using result_type =
typename mem_func_traits<MemFunPtr>::result_type;
128template <
typename Func,
typename Class>
129struct __weak_result_type_memfun<Func Class::*, false> {};
138template <
typename Func>
139struct weak_result_type : __weak_result_type_memfun<remove_cv_t<Func>> {};
148template <
typename MemberPtr,
bool IsMemFunc = is_member_function_po
inter_v<MemberPtr>>
149class mem_func_base :
public inner::mem_func_traits<MemberPtr>::maybe_type {
150 using Traits = inner::mem_func_traits<MemberPtr>;
151 using Arity =
typename Traits::arity;
152 using Varargs =
typename Traits::vararg;
154 template <
typename Func,
typename... BoundArgs>
155 friend struct bind_check_arity;
160 using result_type =
typename Traits::result_type;
166 explicit constexpr mem_func_base(MemberPtr pmf) noexcept :
175 template <
typename... Args>
176 NEFORCE_CONSTEXPR20
auto operator()(Args&&... args)
const
186template <
typename MemberObjPtr>
187class mem_func_base<MemberObjPtr, false> {
188 using Arity = integral_constant<size_t, 0>;
191 template <
typename Func,
typename... BoundArgs>
192 friend struct bind_check_arity;
201 explicit constexpr mem_func_base(MemberObjPtr pm) noexcept :
210 template <
typename T>
211 NEFORCE_CONSTEXPR20
auto operator()(T&& obj)
const
218template <
typename MemberPo
inter>
221template <
typename Res,
typename Class>
222struct mem_func<Res Class::*> : mem_func_base<Res Class::*> {
223 using mem_func_base<Res Class::*>::mem_func_base;
257template <u
int32_t Num>
264template <u
int32_t Num>
276template <u
int32_t Num>
341template <
typename Arg,
bool IsBindExp = is_bind_expression_v<Arg>,
bool IsPlaceholder = is_placeholder_v<Arg>>
342class bind_arg_mapper;
347 template <
typename CVRef,
typename Tuple>
348 NEFORCE_CONSTEXPR20 T& operator()(CVRef& arg, Tuple&)
const volatile {
353template <
typename Arg>
354class bind_arg_mapper<Arg, true, false> {
356 template <
typename CVArg,
typename... Args>
357 NEFORCE_CONSTEXPR20
auto operator()(CVArg& arg, tuple<Args...>& tuple_ref)
const volatile
360 return call(arg, tuple_ref, Indexes());
364 template <
typename CVArg,
typename... Args,
size_t... Indexes>
365 NEFORCE_CONSTEXPR20
auto call(CVArg& arg, tuple<Args...>& tuple_ref,
const index_tuple<Indexes...>&)
const volatile
372template <
typename Arg>
373class bind_arg_mapper<Arg, false, true> {
374 template <
size_t I,
typename Tuple>
378 template <
typename Tuple>
379 NEFORCE_CONSTEXPR20 safe_tuple_element_t<(
is_placeholder_v<Arg> - 1), Tuple>&&
operator()(
const volatile Arg&,
380 Tuple& tuple_ref)
const
382 return _NEFORCE
get<(is_placeholder<Arg>::value - 1)>(_NEFORCE
move(tuple_ref));
386template <
typename Arg>
387class bind_arg_mapper<Arg, false, false> {
389 template <
typename CVArg,
typename Tuple>
390 NEFORCE_CONSTEXPR20 CVArg&& operator()(CVArg&& arg, Tuple&)
const volatile {
414template <
typename Sign>
425template <
typename Func,
typename... BoundArgs>
426class binder<Func(BoundArgs...)> :
public inner::weak_result_type<Func> {
431 tuple<BoundArgs...> bound_args_;
440 template <
typename BoundArg,
typename CallArgs>
441 struct arg_mapper_result {
442 using type =
decltype(inner::bind_arg_mapper<remove_cv_t<BoundArg>>()(_NEFORCE
declval<BoundArg&>(),
450 template <
typename BoundArg,
typename CallArgs>
451 using arg_mapper_result_t =
typename arg_mapper_result<BoundArg, CallArgs>::type;
458 template <
typename CallArgs>
466 template <
typename CallArgs>
475 template <
typename BoundArg,
typename CallArgs>
476 using arg_mapper_type =
decltype(inner::bind_arg_mapper<remove_cv_t<BoundArg>>()(_NEFORCE
declval<BoundArg&>(),
484 template <
typename CallArgs>
497 template <
typename Res,
typename... Args,
size_t... Indexes>
499 return _NEFORCE
invoke(functor_,
500 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
512 template <
typename Res,
typename... Args, _NEFORCE
size_t... Indexes>
514 return _NEFORCE
invoke(functor_,
515 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
525 template <
typename... Args>
526 explicit NEFORCE_CONSTEXPR20
binder(Func&& func, Args&&... args) :
527 functor_(_NEFORCE
forward<Func>(func)),
528 bound_args_(_NEFORCE
forward<Args>(args)...) {}
539 template <
typename... Args>
540 NEFORCE_CONSTEXPR20
auto operator()(Args&&... args) -> result_type<tuple<Args&&...>> {
541 using Res = result_type<tuple<Args&&...>>;
551 template <
typename... Args>
552 NEFORCE_CONSTEXPR20
auto operator()(Args&&... args)
const -> result_type_const<tuple<Args&&...>> {
553 using Res = result_type_const<tuple<Args&&...>>;
557 template <
typename... Args>
558 void operator()(Args&&... args)
const volatile =
delete;
570template <
typename Res,
typename Sign>
579template <
typename Res,
typename Func,
typename... BoundArgs>
585 tuple<BoundArgs...> bound_args_;
597 template <
typename Result,
typename... Args,
size_t... Indexes>
600 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
612 template <
typename Result,
typename... Args, _NEFORCE
size_t... Indexes>
615 inner::bind_arg_mapper<BoundArgs>()(_NEFORCE
get<Indexes>(bound_args_), args)...);
627 template <
typename... Args>
628 explicit NEFORCE_CONSTEXPR20
bindrer(Func&& func, Args&&... args) :
629 functor_(_NEFORCE
forward<Func>(func)),
630 bound_args_(_NEFORCE
forward<Args>(args)...) {}
641 template <
typename... Args>
652 template <
typename... Args>
657 template <
typename... Args>
658 void operator()(Args&&... args)
const volatile =
delete;
664template <
typename Sign>
666template <
typename Res,
typename Sign>
669#define __NEFORCE_EXPAND_BIND_EXP(CV) \
670 template <typename Sign> \
671 struct is_bind_expression<CV binder<Sign>> : true_type {}; \
672 template <typename Res, typename Sign> \
673 struct is_bind_expression<CV bindrer<Res, Sign>> : true_type {};
676#undef __NEFORCE_EXPAND_BIND_EXP
688template <
typename Func,
typename... BoundArgs>
689struct bind_check_arity {};
691template <
typename Ret,
typename... Args,
typename... BoundArgs>
692struct bind_check_arity<Ret (*)(Args...), BoundArgs...> {
693 static_assert(
sizeof...(BoundArgs) ==
sizeof...(Args),
"Wrong number of arguments for function");
696template <
typename Ret,
typename... Args,
typename... BoundArgs>
697struct bind_check_arity<Ret (*)(Args..., ...), BoundArgs...> {
698 static_assert(
sizeof...(BoundArgs) >=
sizeof...(Args),
"Wrong number of arguments for function");
701template <
typename T,
typename Class,
typename... BoundArgs>
702struct bind_check_arity<T Class::*, BoundArgs...> {
703 using Arity =
typename mem_func<T Class::*>::Arity;
704 using Varargs =
typename mem_func<T Class::*>::Varargs;
705 static_assert(Varargs::value ?
sizeof...(BoundArgs) >= Arity::value + 1 :
sizeof...(BoundArgs) == Arity::value + 1,
706 "Wrong number of arguments for pointer-to-member");
719template <
bool IntLike,
typename Func,
typename... BoundArgs>
720struct bind_helper : inner::bind_check_arity<decay_t<Func>, BoundArgs...> {
725template <
typename Func,
typename... BoundArgs>
732template <
bool IntLike,
typename Func,
typename... BoundArgs>
746template <
typename Func,
typename... BoundArgs>
761template <
typename Res,
typename Func,
typename... BoundArgs>
762struct bindr_helper : inner::bind_check_arity<decay_t<Func>, BoundArgs...> {
770template <
typename Res,
typename Func,
typename... BoundArgs>
785template <
typename Res,
typename Func,
typename... BoundArgs>
787NEFORCE_NODISCARD constexpr
bindr_helper_t<Res, Func, BoundArgs...> bind(Func&& func, BoundArgs&&... args) {
800template <
typename Func,
typename... BoundArgs>
803#ifdef NEFORCE_STANDARD_17
811 tuple<BoundArgs...> bound_args_;
824 template <
typename T,
size_t... Indices,
typename... CallArgs>
840 template <
typename Fn,
typename... Args>
841 explicit constexpr binder_front(
int p, Fn&& func, Args&&... args)
noexcept(
843 func_(_NEFORCE
forward<Fn>(func)),
844 bound_args_(_NEFORCE
forward<Args>(args)...) {
845 static_assert(
sizeof...(Args) ==
sizeof...(BoundArgs),
"Wrong number of arguments");
861 template <
typename... CallArgs>
864 return binder_front::call(*
this, BoundIndices(), _NEFORCE
forward<CallArgs>(call_args)...);
873 template <
typename... CallArgs>
876 return binder_front::call(*
this, BoundIndices(), _NEFORCE
forward<CallArgs>(call_args)...);
885 template <
typename... CallArgs>
888 return binder_front::call(_NEFORCE
move(*
this), BoundIndices(), _NEFORCE
forward<CallArgs>(call_args)...);
897 template <
typename... CallArgs>
900 return binder_front::call(_NEFORCE
move(*
this), BoundIndices(), _NEFORCE
forward<CallArgs>(call_args)...);
910template <
typename Func,
typename... Args>
923template <
typename Func,
typename... Args>
931NEFORCE_END_NAMESPACE__
NEFORCE_CONSTEXPR20 auto operator()(Args &&... args) const -> result_type_const< tuple< Args &&... > >
const调用操作符
NEFORCE_CONSTEXPR20 binder(Func &&func, Args &&... args)
构造函数
binder(const binder &)=default
复制构造函数
NEFORCE_CONSTEXPR20 auto operator()(Args &&... args) -> result_type< tuple< Args &&... > >
调用操作符
binder(binder &&)=default
移动构造函数
NEFORCE_CONSTEXPR20 bindrer(Func &&func, Args &&... args)
构造函数
bindrer(bindrer &&)=default
移动构造函数
bindrer(const bindrer &)=default
复制构造函数
NEFORCE_CONSTEXPR20 result_type operator()(Args &&... args)
调用操作符
NEFORCE_CONSTEXPR20 result_type operator()(Args &&... args) const
const调用操作符
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_ALWAYS_INLINE enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
NEFORCE_INLINE17 constexpr bool is_integral_like_v
is_integral_like的便捷变量模板
NEFORCE_INLINE17 constexpr uint32_t placeholder_v
placeholder值的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_placeholder_v
is_placeholder的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_bind_expression_v
is_bind_expression的便捷变量模板
unsigned int uint32_t
32位无符号整数类型
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的便捷别名
NEFORCE_DEPRECATED_FOR("use lambda or bind_front instead of bind") NEFORCE_NODISCARD const expr bind_helper_t< is_integral_like_v< Func >
bind函数
NEFORCE_NODISCARD 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函数
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
根据类型参数包生成索引序列
NEFORCE_INLINE17 constexpr bool is_nothrow_invocable_v
is_nothrow_invocable的便捷变量模板
NEFORCE_CONSTEXPR14 enable_if_t< is_invocable_r< Res, Callable, Args... >::value, Res > invoke_r(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
带返回类型检查的统一调用接口
NEFORCE_CONSTEXPR14 inner::__invoke_result_aux< Callable, Args... >::type invoke(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 Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr size_t tuple_size_v
tuple_size的类型别名
NEFORCE_NODISCARD constexpr tuple< Types &&... > forward_as_tuple(Types &&... args) noexcept
创建转发引用元组
typename tuple_element< Index, Types... >::type tuple_element_t
tuple_element的类型别名
typename decay< T >::type decay_t
decay的便捷别名
#define NEFORCE_MACRO_RANGES_CV(MAC)
cv限定符列表宏
NEFORCE_INLINE17 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的便捷别名
NEFORCE_INLINE17 constexpr placeholder< 27 > p27
占位符27
NEFORCE_INLINE17 constexpr placeholder< 16 > p16
占位符16
NEFORCE_INLINE17 constexpr placeholder< 9 > p9
占位符9
NEFORCE_INLINE17 constexpr placeholder< 6 > p6
占位符6
NEFORCE_INLINE17 constexpr placeholder< 25 > p25
占位符25
NEFORCE_INLINE17 constexpr placeholder< 19 > p19
占位符19
NEFORCE_INLINE17 constexpr placeholder< 3 > p3
占位符3
NEFORCE_INLINE17 constexpr placeholder< 4 > p4
占位符4
NEFORCE_INLINE17 constexpr placeholder< 5 > p5
占位符5
NEFORCE_INLINE17 constexpr placeholder< 20 > p20
占位符20
NEFORCE_INLINE17 constexpr placeholder< 21 > p21
占位符21
NEFORCE_INLINE17 constexpr placeholder< 29 > p29
占位符29
NEFORCE_INLINE17 constexpr placeholder< 7 > p7
占位符7
NEFORCE_INLINE17 constexpr placeholder< 24 > p24
占位符24
NEFORCE_INLINE17 constexpr placeholder< 28 > p28
占位符28
NEFORCE_INLINE17 constexpr placeholder< 18 > p18
占位符18
NEFORCE_INLINE17 constexpr placeholder< 23 > p23
占位符23
NEFORCE_INLINE17 constexpr placeholder< 1 > p1
占位符1
NEFORCE_INLINE17 constexpr placeholder< 11 > p11
占位符11
NEFORCE_INLINE17 constexpr placeholder< 12 > p12
占位符12
NEFORCE_INLINE17 constexpr placeholder< 26 > p26
占位符26
NEFORCE_INLINE17 constexpr placeholder< 17 > p17
占位符17
NEFORCE_INLINE17 constexpr placeholder< 15 > p15
占位符15
NEFORCE_INLINE17 constexpr placeholder< 2 > p2
占位符2
NEFORCE_INLINE17 constexpr placeholder< 13 > p13
占位符13
NEFORCE_INLINE17 constexpr placeholder< 8 > p8
占位符8
NEFORCE_INLINE17 constexpr placeholder< 22 > p22
占位符22
NEFORCE_INLINE17 constexpr placeholder< 10 > p10
占位符10
NEFORCE_INLINE17 constexpr placeholder< 14 > p14
占位符14
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
移动赋值运算符
constexpr invoke_result_t< const Func &, const BoundArgs &..., CallArgs... > operator()(CallArgs &&... call_args) const &noexcept(is_nothrow_invocable_v< const Func &, const BoundArgs &..., CallArgs... >)
const左值调用操作符
~binder_front()=default
析构函数
constexpr invoke_result_t< Func &, BoundArgs &..., CallArgs... > operator()(CallArgs &&... call_args) &noexcept(is_nothrow_invocable_v< Func &, BoundArgs &..., CallArgs... >)
左值调用操作符
constexpr binder_front(int p, Fn &&func, Args &&... args) noexcept(conjunction< is_nothrow_constructible< Func, Fn >, is_nothrow_constructible< BoundArgs, Args >... >::value)
构造函数
constexpr invoke_result_t< const Func, const BoundArgs..., CallArgs... > operator()(CallArgs &&... call_args) const &&noexcept(is_nothrow_invocable_v< const Func, const BoundArgs..., CallArgs... >)
const右值调用操作符
constexpr invoke_result_t< Func, BoundArgs..., CallArgs... > operator()(CallArgs &&... call_args) &&noexcept(is_nothrow_invocable_v< Func, BoundArgs..., CallArgs... >)
右值调用操作符
binder_front & operator=(const binder_front &)=default
复制赋值运算符
static constexpr bool value