43 #ifndef LIB_META_FUNCTION_CLOSURE_H 44 #define LIB_META_FUNCTION_CLOSURE_H 84 template<
typename RET,
class FUN,
class TUP>
91 template<
typename RET,
class FUN,
class TUP>
103 template<
typename RET,
class FUN,
class TUP>
105 invoke (FUN& f, TUP & arg)
107 return f (get<0>(arg));
110 template<
typename RET,
class FUN,
class TUP>
112 bind (FUN& f, TUP & arg)
114 return std::bind (f, get<0>(arg));
122 template<
typename RET,
class FUN,
class TUP>
124 invoke (FUN& f, TUP & arg)
126 return f ( get<0>(arg)
131 template<
typename RET,
class FUN,
class TUP>
133 bind (FUN& f, TUP & arg)
135 return std::bind (f, get<0>(arg)
145 template<
typename RET,
class FUN,
class TUP>
147 invoke (FUN& f, TUP & arg)
149 return f ( get<0>(arg)
155 template<
typename RET,
class FUN,
class TUP>
157 bind (FUN& f, TUP & arg)
159 return std::bind (f, get<0>(arg)
170 template<
typename RET,
class FUN,
class TUP>
172 invoke (FUN& f, TUP & arg)
174 return f ( get<0>(arg)
181 template<
typename RET,
class FUN,
class TUP>
183 bind (FUN& f, TUP & arg)
185 return std::bind (f, get<0>(arg)
197 template<
typename RET,
class FUN,
class TUP>
199 invoke (FUN& f, TUP & arg)
201 return f ( get<0>(arg)
209 template<
typename RET,
class FUN,
class TUP>
211 bind (FUN& f, TUP & arg)
213 return std::bind (f, get<0>(arg)
226 template<
typename RET,
class FUN,
class TUP>
228 invoke (FUN& f, TUP & arg)
230 return f ( get<0>(arg)
239 template<
typename RET,
class FUN,
class TUP>
241 bind (FUN& f, TUP & arg)
243 return std::bind (f, get<0>(arg)
257 template<
typename RET,
class FUN,
class TUP>
259 invoke (FUN& f, TUP & arg)
261 return f ( get<0>(arg)
271 template<
typename RET,
class FUN,
class TUP>
273 bind (FUN& f, TUP & arg)
275 return std::bind (f, get<0>(arg)
290 template<
typename RET,
class FUN,
class TUP>
292 invoke (FUN& f, TUP & arg)
294 return f ( get<0>(arg)
305 template<
typename RET,
class FUN,
class TUP>
307 bind (FUN& f, TUP & arg)
309 return std::bind (f, get<0>(arg)
325 template<
typename RET,
class FUN,
class TUP>
327 invoke (FUN& f, TUP & arg)
329 return f ( get<0>(arg)
341 template<
typename RET,
class FUN,
class TUP>
343 bind (FUN& f, TUP & arg)
345 return std::bind (f, get<0>(arg)
367 using std::_Placeholder;
375 template<
typename TYPES,
size_t i=1>
380 template<
typename X,
typename TAIL,
size_t i>
396 using std::tuple_element;
397 using std::tuple_size;
419 template<
typename SRC,
typename TAR,
size_t start>
423 using DestType =
typename std::tuple_element<i, TAR>::type;
431 static constexpr
bool 434 return (start <= idx)
435 and (idx < start + std::tuple_size<SRC>());
440 template<
size_t idx,
bool doPick = PartiallyInitTuple::useArg(
idx)>
445 operator DestType<idx>()
447 return std::get<idx-start> (initArgs);
456 operator DestType<idx>()
458 return DestType<idx>();
477 template<
typename SIG>
483 using BoundFunc = function<Ret()>;
512 template<
typename SIG>
518 function<Ret(void)> closure_;
528 Ret operator() () {
return closure_(); }
531 typedef void argument_type;
557 template<
typename SIG,
typename VAL>
562 typedef typename Args::List ArgsList;
563 typedef typename VAL::List ValList;
568 , ROFFSET = (VAL_CNT < ARG_CNT)? ARG_CNT-VAL_CNT : 0
581 typedef typename func::PlaceholderTuple<LeftReduced>::List TrailingPlaceholders;
582 typedef typename func::PlaceholderTuple<RightReduced>::List LeadingPlaceholders;
596 template<
class SRC,
class TAR,
size_t i>
597 using IdxSelectorL =
typename PartiallyInitTuple<SRC, TAR, 0>::template IndexMapper<i>;
599 template<
class SRC,
class TAR,
size_t i>
600 using IdxSelectorR =
typename PartiallyInitTuple<SRC, TAR, ROFFSET>::template IndexMapper<i>;
616 typedef function<typename BuildFunType<Ret,ArgsL>::Sig> LeftReducedFunc;
617 typedef function<typename BuildFunType<Ret,ArgsR>::Sig> RightReducedFunc;
628 static LeftReducedFunc
632 return func::Apply<ARG_CNT>::template bind<LeftReducedFunc> (f, params);
643 static RightReducedFunc
647 return func::Apply<ARG_CNT>::template bind<RightReducedFunc> (f, params);
657 template<
typename SIG,
typename X, u
int pos>
662 typedef typename Args::List ArgsList;
669 typedef typename func::PlaceholderTuple<RemainingFront>::List PlaceholdersBefore;
670 typedef typename func::PlaceholderTuple<RemainingBack,pos+1>::List PlaceholdersBehind;
671 typedef typename Append<
typename Append< PlaceholdersBefore
681 template<
class SRC,
class TAR,
size_t i>
682 using IdxSelector =
typename PartiallyInitTuple<SRC, TAR, pos>::template IndexMapper<i>;
689 typedef function<ReducedSig> ReducedFunc;
695 return func::Apply<ARG_CNT>::template bind<ReducedFunc> (f, params);
707 template<
typename RET,
typename ARG>
714 template<
typename SIG,
typename ARG>
718 typedef typename _Sig<Ret,ARG>::Type Signature;
722 template<
typename FUN1,
typename FUN2>
729 static auto adaptedFunType() {
return FunType{}; }
732 template<
typename F1,
typename F2
733 ,
typename RET,
typename... ARGS>
735 composedFunctions (F1&& f1, F2&& f2,
_Fun<RET(ARGS...)>)
737 tuple<F1,F2> binding{forward<F1> (f1)
740 return [binding = move(binding)]
741 (ARGS ...args) -> RET
743 auto& functor1 = get<0>(binding);
744 auto& functor2 = get<1>(binding);
746 return functor2 (functor1 (forward<ARGS> (args)...));
751 template<
typename FUN>
760 static auto adaptedFunType() {
return FunType{}; }
763 template<
typename F,
typename A
764 ,
typename RET,
typename... ARGS>
766 bindFrontArg (F&& fun, A&& arg,
_Fun<RET(ARGS...)>)
768 tuple<F,A> binding{forward<F> (fun)
771 return [binding = move(binding)]
772 (ARGS ...args) -> RET
774 auto& functor = get<0>(binding);
776 return functor ( forward<A> (unConst (get<1>(binding)))
777 , forward<ARGS> (args)...);
782 template<
typename FUN>
791 static auto adaptedFunType() {
return FunType{}; }
794 template<
typename F,
typename A
795 ,
typename RET,
typename... ARGS>
797 bindBackArg (F&& fun, A&& arg,
_Fun<RET(ARGS...)>)
799 tuple<F,A> binding{forward<F> (fun)
802 return [binding = move(binding)]
803 (ARGS ...args) -> RET
805 auto& functor = get<0>(binding);
807 return functor ( forward<ARGS> (args)...
808 , forward<A> (unConst (get<1>(binding))));
824 template<
typename...ARG>
826 typename _Sig<void,
Types<ARG...>>::Applicator
829 typedef typename _Sig<void,
Types<ARG...>>::Type Signature;
836 template<
typename SIG,
typename...ARG>
839 apply (SIG& f, std::tuple<ARG...>& args)
842 typedef typename _Sig<Ret,
Types<ARG...>>::Type Signature;
851 template<
typename SIG,
typename...ARG>
853 typename _Clo<SIG,
Types<ARG...>>::Type
856 typedef typename _Clo<SIG,
Types<ARG...>>::Type Closure;
857 return Closure (f,args);
863 template<
typename FUN,
typename ARG>
867 static_assert (
_Fun<FUN>(),
"expect something function-like");
868 return _PapS<FUN>::bindFrontArg (forward<FUN> (fun)
870 ,_PapS<FUN>::adaptedFunType());
874 template<
typename FUN,
typename ARG>
878 static_assert (
_Fun<FUN>(),
"expect something function-like");
879 return _PapE<FUN>::bindBackArg (forward<FUN> (fun)
881 ,_PapE<FUN>::adaptedFunType());
887 template<
typename SIG,
typename TERM>
889 typename _PapE<SIG>::FunType::Functor
903 template<
typename FUN1,
typename FUN2>
907 static_assert (
_Fun<FUN1>(),
"expect something function-like for function-1");
908 static_assert (
_Fun<FUN2>(),
"expect something function-like for function-2");
909 using Chain = _Chain<FUN1,FUN2>;
910 return Chain::composedFunctions (forward<FUN1> (f1)
912 ,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