52 #ifndef LIB_META_FUNCTION_CLOSURE_H 53 #define LIB_META_FUNCTION_CLOSURE_H 93 template<
typename RET,
class FUN,
class TUP>
100 template<
typename RET,
class FUN,
class TUP>
104 return std::bind (f);
112 template<
typename RET,
class FUN,
class TUP>
114 invoke (FUN& f, TUP & arg)
116 return f (get<0>(arg));
119 template<
typename RET,
class FUN,
class TUP>
121 bind (FUN& f, TUP & arg)
123 return std::bind (f, get<0>(arg));
131 template<
typename RET,
class FUN,
class TUP>
133 invoke (FUN& f, TUP & arg)
135 return f ( get<0>(arg)
140 template<
typename RET,
class FUN,
class TUP>
142 bind (FUN& f, TUP & arg)
144 return std::bind (f, get<0>(arg)
154 template<
typename RET,
class FUN,
class TUP>
156 invoke (FUN& f, TUP & arg)
158 return f ( get<0>(arg)
164 template<
typename RET,
class FUN,
class TUP>
166 bind (FUN& f, TUP & arg)
168 return std::bind (f, get<0>(arg)
179 template<
typename RET,
class FUN,
class TUP>
181 invoke (FUN& f, TUP & arg)
183 return f ( get<0>(arg)
190 template<
typename RET,
class FUN,
class TUP>
192 bind (FUN& f, TUP & arg)
194 return std::bind (f, get<0>(arg)
206 template<
typename RET,
class FUN,
class TUP>
208 invoke (FUN& f, TUP & arg)
210 return f ( get<0>(arg)
218 template<
typename RET,
class FUN,
class TUP>
220 bind (FUN& f, TUP & arg)
222 return std::bind (f, get<0>(arg)
235 template<
typename RET,
class FUN,
class TUP>
237 invoke (FUN& f, TUP & arg)
239 return f ( get<0>(arg)
248 template<
typename RET,
class FUN,
class TUP>
250 bind (FUN& f, TUP & arg)
252 return std::bind (f, get<0>(arg)
266 template<
typename RET,
class FUN,
class TUP>
268 invoke (FUN& f, TUP & arg)
270 return f ( get<0>(arg)
280 template<
typename RET,
class FUN,
class TUP>
282 bind (FUN& f, TUP & arg)
284 return std::bind (f, get<0>(arg)
299 template<
typename RET,
class FUN,
class TUP>
301 invoke (FUN& f, TUP & arg)
303 return f ( get<0>(arg)
314 template<
typename RET,
class FUN,
class TUP>
316 bind (FUN& f, TUP & arg)
318 return std::bind (f, get<0>(arg)
334 template<
typename RET,
class FUN,
class TUP>
336 invoke (FUN& f, TUP & arg)
338 return f ( get<0>(arg)
350 template<
typename RET,
class FUN,
class TUP>
352 bind (FUN& f, TUP & arg)
354 return std::bind (f, get<0>(arg)
376 using std::_Placeholder;
384 template<
typename TYPES,
size_t i=1>
389 template<
typename X,
typename TAIL,
size_t i>
405 using std::tuple_element;
406 using std::tuple_size;
428 template<
typename SRC,
typename TAR,
size_t start>
432 using DestType =
typename std::tuple_element<i, TAR>::type;
440 static constexpr
bool 443 return (start <= idx)
444 and (idx < start + std::tuple_size<SRC>());
449 template<
size_t idx,
bool doPick = PartiallyInitTuple::useArg(
idx)>
454 operator DestType<idx>()
456 return std::get<idx-start> (initArgs);
465 operator DestType<idx>()
467 return DestType<idx>();
486 template<
typename SIG>
492 using BoundFunc = function<Ret()>;
521 template<
typename SIG>
527 function<Ret(void)> closure_;
537 Ret operator() () {
return closure_(); }
540 typedef void argument_type;
566 template<
typename SIG,
typename VAL>
571 typedef typename Args::List ArgsList;
572 typedef typename VAL::List ValList;
577 , ROFFSET = (VAL_CNT < ARG_CNT)? ARG_CNT-VAL_CNT : 0
590 typedef typename func::PlaceholderTuple<LeftReduced>::List TrailingPlaceholders;
591 typedef typename func::PlaceholderTuple<RightReduced>::List LeadingPlaceholders;
605 template<
class SRC,
class TAR,
size_t i>
606 using IdxSelectorL =
typename PartiallyInitTuple<SRC, TAR, 0>::template IndexMapper<i>;
608 template<
class SRC,
class TAR,
size_t i>
609 using IdxSelectorR =
typename PartiallyInitTuple<SRC, TAR, ROFFSET>::template IndexMapper<i>;
625 typedef function<typename BuildFunType<Ret,ArgsL>::Sig> LeftReducedFunc;
626 typedef function<typename BuildFunType<Ret,ArgsR>::Sig> RightReducedFunc;
637 static LeftReducedFunc
641 return func::Apply<ARG_CNT>::template bind<LeftReducedFunc> (f, params);
652 static RightReducedFunc
656 return func::Apply<ARG_CNT>::template bind<RightReducedFunc> (f, params);
666 template<
typename SIG,
typename X, u
int pos>
671 typedef typename Args::List ArgsList;
678 typedef typename func::PlaceholderTuple<RemainingFront>::List PlaceholdersBefore;
679 typedef typename func::PlaceholderTuple<RemainingBack,pos+1>::List PlaceholdersBehind;
680 typedef typename Append<
typename Append< PlaceholdersBefore
690 template<
class SRC,
class TAR,
size_t i>
691 using IdxSelector =
typename PartiallyInitTuple<SRC, TAR, pos>::template IndexMapper<i>;
698 typedef function<ReducedSig> ReducedFunc;
704 return func::Apply<ARG_CNT>::template bind<ReducedFunc> (f, params);
716 template<
typename RET,
typename ARG>
723 template<
typename SIG,
typename ARG>
727 typedef typename _Sig<Ret,ARG>::Type Signature;
731 template<
typename FUN1,
typename FUN2>
738 static auto adaptedFunType() {
return FunType{}; }
741 template<
typename F1,
typename F2
742 ,
typename RET,
typename... ARGS>
744 composedFunctions (F1&& f1, F2&& f2,
_Fun<RET(ARGS...)>)
746 tuple<F1,F2> binding{forward<F1> (f1)
749 return [binding = move(binding)]
750 (ARGS ...args) -> RET
752 auto& functor1 = get<0>(binding);
753 auto& functor2 = get<1>(binding);
755 return functor2 (functor1 (forward<ARGS> (args)...));
760 template<
typename FUN>
769 static auto adaptedFunType() {
return FunType{}; }
772 template<
typename F,
typename A
773 ,
typename RET,
typename... ARGS>
775 bindFrontArg (F&& fun, A&& arg,
_Fun<RET(ARGS...)>)
777 tuple<F,A> binding{forward<F> (fun)
780 return [binding = move(binding)]
781 (ARGS ...args) -> RET
783 auto& functor = get<0>(binding);
785 return functor ( forward<A> (unConst (get<1>(binding)))
786 , forward<ARGS> (args)...);
791 template<
typename FUN>
800 static auto adaptedFunType() {
return FunType{}; }
803 template<
typename F,
typename A
804 ,
typename RET,
typename... ARGS>
806 bindBackArg (F&& fun, A&& arg,
_Fun<RET(ARGS...)>)
808 tuple<F,A> binding{forward<F> (fun)
811 return [binding = move(binding)]
812 (ARGS ...args) -> RET
814 auto& functor = get<0>(binding);
816 return functor ( forward<ARGS> (args)...
817 , forward<A> (unConst (get<1>(binding))));
833 template<
typename...ARG>
835 typename _Sig<void,
Types<ARG...>>::Applicator
838 typedef typename _Sig<void,
Types<ARG...>>::Type Signature;
845 template<
typename SIG,
typename...ARG>
848 apply (SIG& f, std::tuple<ARG...>& args)
851 typedef typename _Sig<Ret,
Types<ARG...>>::Type Signature;
860 template<
typename SIG,
typename...ARG>
862 typename _Clo<SIG,
Types<ARG...>>::Type
865 typedef typename _Clo<SIG,
Types<ARG...>>::Type Closure;
866 return Closure (f,args);
872 template<
typename FUN,
typename ARG>
876 static_assert (
_Fun<FUN>(),
"expect something function-like");
877 return _PapS<FUN>::bindFrontArg (forward<FUN> (fun)
879 ,_PapS<FUN>::adaptedFunType());
883 template<
typename FUN,
typename ARG>
887 static_assert (
_Fun<FUN>(),
"expect something function-like");
888 return _PapE<FUN>::bindBackArg (forward<FUN> (fun)
890 ,_PapE<FUN>::adaptedFunType());
896 template<
typename SIG,
typename TERM>
898 typename _PapE<SIG>::FunType::Functor
912 template<
typename FUN1,
typename FUN2>
916 static_assert (
_Fun<FUN1>(),
"expect something function-like for function-1");
917 static_assert (
_Fun<FUN2>(),
"expect something function-like for function-2");
918 using Chain = _Chain<FUN1,FUN2>;
919 return Chain::composedFunctions (forward<FUN1> (f1)
921 ,Chain::adaptedFunType());
_Clo< SIG, Types< ARG... > >::Type closure(SIG &f, std::tuple< ARG... > &args)
close the given function over all arguments, using the values from the argument tuple.
typename BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
auto applyFirst(FUN &&fun, ARG &&arg)
close the given function over the first argument.
_PapE< SIG >::FunType::Functor bindLast(SIG &f, TERM const &arg)
bind the last function argument to an arbitrary term, which especially might be a (nested) binder...
Implementation namespace for support and library code.
Metaprogramming with tuples-of-types and the std::tuple record.
auto applyLast(FUN &&fun, ARG &&arg)
close the given function over the last argument
Metaprogramming tools for transforming functor types.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
auto chained(FUN1 &&f1, FUN2 &&f2)
build a functor chaining the given functions: feed the result of f1 into f2.
_Sig< void, Types< ARG... > >::Applicator tupleApplicator(std::tuple< ARG... > &args)
build a TupleApplicator, which embodies the given argument tuple and can be used to apply them to var...
_Fun< SIG >::Ret apply(SIG &f, std::tuple< ARG... > &args)
apply the given function to the argument tuple