composition now working

This commit is contained in:
Fischlurch 2009-07-03 13:49:12 +02:00
parent 31e9d59d80
commit b7be61c4e6
2 changed files with 104 additions and 38 deletions

View file

@ -57,7 +57,9 @@ namespace lumiera {
namespace typelist{
using std::tr1::function;
using std::tr1::bind;
//using std::tr1::bind;
//using std::tr1::placeholders::_1;
@ -381,6 +383,7 @@ namespace typelist{
/* ===== Helpers for partial function application ===== */
using std::tr1::_Placeholder; // what is the "official" way to import them?
@ -564,6 +567,82 @@ namespace typelist{
namespace _cmp {
using std::tr1::bind;
using std::tr1::placeholders::_1;
using std::tr1::placeholders::_2;
using std::tr1::placeholders::_3;
using std::tr1::placeholders::_4;
using std::tr1::placeholders::_5;
using std::tr1::placeholders::_6;
using std::tr1::placeholders::_7;
using std::tr1::placeholders::_8;
using std::tr1::placeholders::_9;
template<typename RES, typename F1, typename F2, uint n>
struct BuildComposed;
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 0 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 1 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 2 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 3 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 4 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3,_4)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 5 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3,_4,_5)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 6 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3,_4,_5,_6)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 7 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3,_4,_5,_6,_7)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 8 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3,_4,_5,_6,_7,_8)); }
};
template<typename RES, typename F1, typename F2>
struct BuildComposed<RES,F1,F2, 9 >
{
static function<RES> func(F1& f1, F2& f2) { return bind (f2, bind (f1,_1,_2,_3,_4,_5,_6,_7,_8,_9)); }
};
}
/**
* Functional composition. Create a functor, which
@ -579,32 +658,17 @@ namespace typelist{
typedef Types<Ret1> ArgsF2;
typedef typename FunctionTypedef<RET, ArgsF2>::Sig SigF2;
typedef typename FunctionTypedef<RET, Args>::Sig ChainedSig;
typedef function<ChainedSig> ChainedFunc;
typedef typename func::PlaceholderTuple<Args>::PlaceholderSeq PlaceholderSeq;
typedef Tuple<PlaceholderSeq> Placeholders;
enum { ARG_CNT = count<typename Args::List>::value };
struct ComposedFunc
: ChainedFunc
{
template<class TUP>
ComposedFunc (SIG1& f1, SigF2& f2, TUP& args)
: ChainedFunc (std::tr1::bind (f2 ,
func::Apply<ARG_CNT>::template bind<function<SIG1> > (f1, args)
))
{ }
};
public:
static ChainedFunc
static function<ChainedSig>
chain (SIG1& f1, SigF2& f2)
{
Placeholders args;
return ComposedFunc (f1,f2,args);
return _cmp::BuildComposed<ChainedSig,SIG1,SigF2, ARG_CNT>::func (f1,f2);
}
};

View file

@ -68,7 +68,7 @@ namespace test {
Num<9> _9_;
/** "Function-1" will be used at the front side, accepting a tuple of values */
template<char i>
template<uint i>
Num<i>
fun11 ( Num<i> val1
)
@ -76,7 +76,7 @@ namespace test {
return val1;
}
template<char i, char ii>
template<uint i, uint ii>
Num<i>
fun12 ( Num<i> val1
, Num<ii> val2
@ -86,7 +86,7 @@ namespace test {
return val1;
}
template<char i, char ii, char iii>
template<uint i, uint ii, uint iii>
Num<i>
fun13 ( Num<i> val1
, Num<ii> val2
@ -97,7 +97,7 @@ namespace test {
return val1;
}
template<char i, char ii, char iii, char iv>
template<uint i, uint ii, uint iii, uint iv>
Num<i>
fun14 ( Num<i> val1
, Num<ii> val2
@ -109,7 +109,7 @@ namespace test {
return val1;
}
template<char i, char ii, char iii, char iv, char v>
template<uint i, uint ii, uint iii, uint iv, uint v>
Num<i>
fun15 ( Num<i> val1
, Num<ii> val2
@ -306,27 +306,29 @@ namespace test {
void
check_functionalComposition ()
{
typedef int Sig2(Num<1>);
typedef Num<1> Sig11(Num<1>);
typedef Num<1> Sig12(Num<1>,Num<2>);
typedef int SigF21(Num<1>);
typedef Num<1> Sig13(Num<1>,Num<2>,Num<3>);
typedef Num<1> Sig14(Num<1>,Num<2>,Num<3>,Num<4>);
typedef Num<1> Sig15(Num<1>,Num<2>,Num<3>,Num<4>,Num<5>);
Sig12 &f1 = fun12<1,2>;
SigF21 &f2 = fun2<Num<1> >;
Sig2 & ff = fun2< Num<1> >;
Sig11& f1 = fun11<1>;
Sig12& f2 = fun12<1,2>;
Sig13& f3 = fun13<1,2,3>;
Sig14& f4 = fun14<1,2,3,4>;
Sig15& f5 = fun15<1,2,3,4,5>;
typedef function<int(Num<1>,Num<2>)> Chained;
Chained funCh = func::chained (f1, f2 );
ASSERT (1+2 == funCh (_1_,_2_) );
#if false
ASSERT (1 == func::chain(fun11<1> , fun2) (_1_) );
ASSERT (1+2 == func::chain(fun12<1,2> , fun2<Num<1> > ) (_1_,_2_) );
ASSERT (1+2+3 == func::chain(fun13<1,2,3> , fun2) (_1_,_2_,_3_) );
ASSERT (1+2+3+4 == func::chain(fun14<1,2,3,4> , fun2) (_1_,_2_,_3_,_4_) );
ASSERT (1+2+3+4+5 == func::chain(fun15<1,2,3,4,5>, fun2) (_1_,_2_,_3_,_4_,_5_) );
#endif
ASSERT (1 == func::chained(f1, ff) (_1_) );
ASSERT (1+2 == func::chained(f2, ff) (_1_,_2_) );
ASSERT (1+2+3 == func::chained(f3, ff) (_1_,_2_,_3_) );
ASSERT (1+2+3+4 == func::chained(f4, ff) (_1_,_2_,_3_,_4_) );
ASSERT (1+2+3+4+5 == func::chained(f5, ff) (_1_,_2_,_3_,_4_,_5_) );
}
};