diff --git a/src/lib/meta/function-closure.hpp b/src/lib/meta/function-closure.hpp index c86958ba7..59348fd8e 100644 --- a/src/lib/meta/function-closure.hpp +++ b/src/lib/meta/function-closure.hpp @@ -56,7 +56,27 @@ namespace typelist{ using tuple::element; - /** + template + struct _Fun + { + typedef typename FunctionSignature >::Ret Ret; + typedef typename FunctionSignature >::Args Args; + }; + template + struct _Fun > + { + typedef typename FunctionSignature >::Ret Ret; + typedef typename FunctionSignature >::Args Args; + }; + + template + struct is_Functor { static const bool value = false; }; + template + struct is_Functor > { static const bool value = true; }; + + + + /** * this Helper with repetitive specialisations for up to nine arguments * is used either to apply a function to arguments given as a tuple, or * to create the actual closure (functor) over all function arguments. @@ -91,14 +111,14 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f (element<1>(arg)); + return f (element<0>(arg)); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg)); + return std::tr1::bind (f, element<0>(arg)); } }; @@ -110,17 +130,17 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) - , element<2>(arg) - ); + return f ( element<0>(arg) + , element<1>(arg) + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) - , element<2>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) ); } }; @@ -133,19 +153,19 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) - , element<3>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) - , element<3>(arg) ); } }; @@ -158,21 +178,21 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) - , element<4>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) - , element<4>(arg) ); } }; @@ -185,23 +205,23 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) - , element<5>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) - , element<5>(arg) ); } }; @@ -214,25 +234,25 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) , element<5>(arg) - , element<6>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) , element<5>(arg) - , element<6>(arg) ); } }; @@ -245,27 +265,27 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) , element<5>(arg) , element<6>(arg) - , element<7>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) , element<5>(arg) , element<6>(arg) - , element<7>(arg) ); } }; @@ -278,29 +298,29 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) , element<5>(arg) , element<6>(arg) , element<7>(arg) - , element<8>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) , element<5>(arg) , element<6>(arg) , element<7>(arg) - , element<8>(arg) ); } }; @@ -313,7 +333,8 @@ namespace typelist{ static RET invoke (FUN& f, TUP & arg) { - return f ( element<1>(arg) + return f ( element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) @@ -321,15 +342,15 @@ namespace typelist{ , element<6>(arg) , element<7>(arg) , element<8>(arg) - , element<9>(arg) - ); + ); } template static RET bind (FUN& f, TUP & arg) { - return std::tr1::bind (f, element<1>(arg) + return std::tr1::bind (f, element<0>(arg) + , element<1>(arg) , element<2>(arg) , element<3>(arg) , element<4>(arg) @@ -337,7 +358,6 @@ namespace typelist{ , element<6>(arg) , element<7>(arg) , element<8>(arg) - , element<9>(arg) ); } }; @@ -348,6 +368,7 @@ namespace typelist{ /** * Closure-creating template. + * @note taking functor objects \em and parameters per reference */ template class TupleApplicator @@ -368,11 +389,11 @@ namespace typelist{ : params_(args) { } - BoundFunc bind (SIG& f) { return func::Apply::template bind (f, params_); } - BoundFunc bind (function& f) { return func::Apply::template bind (f, params_); } + BoundFunc bind (SIG& f) { return func::Apply::template bind (f, params_); } + BoundFunc bind (function const& f) { return func::Apply::template bind (f, params_); } - Ret operator() (SIG& f) { return func::Apply::template invoke (f, params_); } - Ret operator() (function& f) { return func::Apply::template invoke (f, params_); } + Ret operator() (SIG& f) { return func::Apply::template invoke (f, params_); } + Ret operator() (function& f) { return func::Apply::template invoke (f, params_); } }; @@ -386,8 +407,8 @@ namespace typelist{ template class FunctionClosure { - typedef typename FunctionSignature< function >::Args Args; - typedef typename FunctionSignature< function >::Ret Ret; + typedef typename func::_Fun::Args Args; + typedef typename func::_Fun::Ret Ret; function closure_; @@ -407,5 +428,64 @@ namespace typelist{ + namespace func { // ...some convenience shortcuts + + template + struct _Sig + { + typedef typename FunctionTypedef::Sig Type; + typedef TupleApplicator Applicator; + }; + + template + struct _Clo + { + typedef typename _Fun::Ret Ret; + typedef typename _Sig::Type Signature; + typedef FunctionClosure Type; + }; + + + /** build a TupleApplicator, which embodies the given + * argument tuple and can be used to apply various + * functions to them. + */ + template + typename _Sig::Applicator + tupleApplicator (Tuple& args) + { + typedef typename _Sig::Type Signature; + return TupleApplicator (args); + } + + + /** apply the given function to the argument tuple */ + template + typename _Fun::Ret + apply (SIG& f, Tuple& args) + { + typedef typename _Fun::Ret Ret; // + typedef typename _Sig::Type Signature; // Note: deliberately re-building the Signature Type + return TupleApplicator (args) (f); // in order to get better error messages here + } + + /** close the given function over all arguments, + * using the values from the argument tuple. + * @return a closure object, which can be + * invoked later to yield the + * function result. */ + template + typename _Clo::Type + closure (SIG& f, Tuple& args) + { + typedef typename _Fun::Ret Ret; + typedef typename _Sig::Type Signature; + typedef typename _Clo::Type Closure; + return Closure (f,args); + } + } + + + }} // namespace lumiera::typelist #endif diff --git a/src/lib/meta/tuple.hpp b/src/lib/meta/tuple.hpp index 0f534b8c1..6b031adb5 100644 --- a/src/lib/meta/tuple.hpp +++ b/src/lib/meta/tuple.hpp @@ -161,15 +161,15 @@ namespace typelist{ { typedef typename Split::Tail Tail; public: - typedef typename Shifted::Types Types; - typedef typename Split::Head Head; + typedef typename Shifted::Type Type; + typedef typename Split::Head Head; }; template struct Shifted { - typedef TYPES Types; - typedef typename Types::List::Head Head; + typedef TYPES Type; + typedef typename Split::Head Head; }; @@ -280,7 +280,7 @@ namespace typelist{ class ShiftedTuple { typedef typename Tuple::Type OurType_; - typedef typename Shifted::Types ShiftedTypes_; + typedef typename Shifted::Type ShiftedTypes_; public: typedef Tuple Type; }; @@ -321,6 +321,11 @@ namespace typelist{ ) { } ///< end recursion of chained ctor calls + /** shortcut: allow copy construction from a tuple + * which is rather defined by a list type */ + Tuple (Tuple const&) + { } + template struct ShiftedTuple { typedef TupleNull Type; }; @@ -330,10 +335,27 @@ namespace typelist{ + + /** specialisation to shift plain tuple types */ + template + struct Shifted,i> + { + typedef typename Shifted::Type Type; + typedef typename Shifted::Head Head; + }; + template + struct Shifted, 0> + { + typedef typename Tuple::Type Type; + typedef typename Tuple::HeadType Head; + }; + + + namespace tuple { // some convenience access functions template - typename TUP::template ShiftedTuple::Type::Head& + typename Shifted::Head& element (TUP& tup) { return tup.template getAt(); diff --git a/src/proc/control/command-closure.hpp b/src/proc/control/command-closure.hpp index 56efd3302..8b626b6b2 100644 --- a/src/proc/control/command-closure.hpp +++ b/src/proc/control/command-closure.hpp @@ -144,9 +144,8 @@ namespace control { ////////////////////TODO the recursion-end of the access operations goes here - protected: string - dump (string const& prefix) const + dump (string const& prefix ="(") const { if (1 < prefix.length()) // remove trailing comma... diff --git a/tests/40components.tests b/tests/40components.tests index 85bfb6c2d..7e48db913 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -213,7 +213,19 @@ out: dtor ~TargetObj\(12\) successful END -PLANNED "FunctionClosure_test" FunctionClosure_test <-<2>-<3>- +out: List2 :-<5>-<6>-<7>- +out: Args :-<5>-<9>- +out: NewArgs :-<1>-<5>-<9>- +out: :$ +out: : ---Apply--- +out: tup0 :...Tuple\(\) +out: tup1 :...Tuple\(11\) +out: tup2 :...Tuple\(11,12\) +out: tup3 :...Tuple\(11,12,13\) +out: :$ +out: : ---Bind---- return: 0 END diff --git a/tests/43session.tests b/tests/43session.tests index d91e4b0a9..083df1c4e 100644 --- a/tests/43session.tests +++ b/tests/43session.tests @@ -43,7 +43,7 @@ out: TestSubMO1\(ID= [0-9]{3}\) out: TestSubMO2\(ID= [0-9]{3}\) out: TestSubMO21\(ID= [0-9]{3}\) out: specialAPI() -out: pID\(\w{16}\) +out: pID\(\w{8,16}\) END diff --git a/tests/lib/meta/function-closure-test.cpp b/tests/lib/meta/function-closure-test.cpp index 9576a2dcf..72b458cf8 100644 --- a/tests/lib/meta/function-closure-test.cpp +++ b/tests/lib/meta/function-closure-test.cpp @@ -22,9 +22,13 @@ /** @file function-closure-test.cpp + ** Testing a combination of tr1::function objects and metaprogramming. + ** Argument types will be extracted and represented as typelist, so they + ** can be manipulated at compile time. This test uses some functions with + ** and systematically applies or binds them to corresponding data tuples. + ** Moreover, closure objects will be constructed in various flavours, + ** combining a function object and a set of parameters. ** - ** @todo define function-closure-test - ** ** @see function-closure.hpp ** @see control::CmdClosure real world usage example ** @@ -40,14 +44,9 @@ #include "meta/dummy-functions.hpp" #include "meta/typelist-diagnostics.hpp" #include "meta/tuple-diagnostics.hpp" -//#include "lib/util.hpp" -//#include #include -using lib::test::showSizeof; -using lib::test::showType; - using ::test::Test; using std::string; using std::cout; @@ -82,6 +81,12 @@ namespace test { return one.o_ + two.o_ + three.o_; } + + int fun0 () { return -1; } + int fun1 (int i1) { return i1; } + int fun2 (int i1, int i2) { return i1+i2; } + int fun3 (int i1, int i2, int i3) { return i1+i2+i3; } + } // (End) test data @@ -90,8 +95,8 @@ namespace test { /************************************************************************* - * @test building a function closure for a given functor - * and arguments passed in as tuple + * @test building a function closure for a given function or functor, + * while arguments are passed in as tuple * - accessing signatures as typelists * - apply free function to tuple * - apply functor to tuple @@ -151,35 +156,189 @@ namespace test { void check_applyFree () { - UNIMPLEMENTED ("verify apply free function to tuple"); + cout << "\t:\n\t: ---Apply---\n"; + + Tuple > tup0 ; + Tuple > tup1 (11); + Tuple > tup2 (11,12); + Tuple > tup3 (11,12,13); + DUMPVAL (tup0); + DUMPVAL (tup1); + DUMPVAL (tup2); + DUMPVAL (tup3); + + ASSERT (-1 == func::Apply<0>::invoke (fun0, tup0) ); + ASSERT (11 == func::Apply<1>::invoke (fun1, tup1) ); + ASSERT (11+12 == func::Apply<2>::invoke (fun2, tup2) ); + ASSERT (11+12+13 == func::Apply<3>::invoke (fun3, tup3) ); + + ASSERT (-1 == TupleApplicator (tup0) (fun0) ); + ASSERT (11 == TupleApplicator (tup1) (fun1) ); + ASSERT (11+12 == TupleApplicator (tup2) (fun2) ); + ASSERT (11+12+13 == TupleApplicator (tup3) (fun3) ); + + ASSERT (-1 == func::apply(fun0, tup0) ); + ASSERT (11 == func::apply(fun1, tup1) ); + ASSERT (11+12 == func::apply(fun2, tup2) ); + ASSERT (11+12+13 == func::apply(fun3, tup3) ); + } void check_applyFunc () { - UNIMPLEMENTED ("verify apply functor to tuple"); + Tuple > tup0 ; + Tuple > tup1 (11); + Tuple > tup2 (11,12); + Tuple > tup3 (11,12,13); + function functor0 (fun0); + function functor1 (fun1); + function functor2 (fun2); + function functor3 (fun3); + + ASSERT (-1 == func::Apply<0>::invoke (functor0, tup0) ); + ASSERT (11 == func::Apply<1>::invoke (functor1, tup1) ); + ASSERT (11+12 == func::Apply<2>::invoke (functor2, tup2) ); + ASSERT (11+12+13 == func::Apply<3>::invoke (functor3, tup3) ); + + ASSERT (-1 == TupleApplicator (tup0) (functor0) ); + ASSERT (11 == TupleApplicator (tup1) (functor1) ); + ASSERT (11+12 == TupleApplicator (tup2) (functor2) ); + ASSERT (11+12+13 == TupleApplicator (tup3) (functor3) ); + + ASSERT (-1 == func::apply(functor0, tup0) ); + ASSERT (11 == func::apply(functor1, tup1) ); + ASSERT (11+12 == func::apply(functor2, tup2) ); + ASSERT (11+12+13 == func::apply(functor3, tup3) ); + } void check_bindFree () { - UNIMPLEMENTED ("verify bind free function to tuple"); + cout << "\t:\n\t: ---Bind----\n"; + + Tuple > tup0 ; + Tuple > tup1 (11); + Tuple > tup2 (11,12); + Tuple > tup3 (11,12,13); + + typedef function BoundFun; + + BoundFun functor0 = func::Apply<0>::bind (fun0, tup0); + BoundFun functor1 = func::Apply<1>::bind (fun1, tup1); + BoundFun functor2 = func::Apply<2>::bind (fun2, tup3); + BoundFun functor3 = func::Apply<3>::bind (fun3, tup3); + + ASSERT (-1 == functor0() ); + ASSERT (11 == functor1() ); + ASSERT (11+12 == functor2() ); + ASSERT (11+12+13 == functor3() ); + + functor0 = TupleApplicator (tup0).bind (fun0); + functor1 = TupleApplicator (tup1).bind (fun1); + functor2 = TupleApplicator (tup2).bind (fun2); + functor3 = TupleApplicator (tup3).bind (fun3); + + ASSERT (-1 == functor0() ); + ASSERT (11 == functor1() ); + ASSERT (11+12 == functor2() ); + ASSERT (11+12+13 == functor3() ); + } void check_bindFunc () { - UNIMPLEMENTED ("verify bind functor to tuple"); + Tuple > tup0 ; + Tuple > tup1 (11); + Tuple > tup2 (11,12); + Tuple > tup3 (11,12,13); + function unbound_functor0 (fun0); + function unbound_functor1 (fun1); + function unbound_functor2 (fun2); + function unbound_functor3 (fun3); + + typedef function BoundFun; + + BoundFun functor0 = func::Apply<0>::bind (unbound_functor0, tup0); + BoundFun functor1 = func::Apply<1>::bind (unbound_functor1, tup1); + BoundFun functor2 = func::Apply<2>::bind (unbound_functor2, tup3); + BoundFun functor3 = func::Apply<3>::bind (unbound_functor3, tup3); + + ASSERT (-1 == functor0() ); + ASSERT (11 == functor1() ); + ASSERT (11+12 == functor2() ); + ASSERT (11+12+13 == functor3() ); + + functor0 = TupleApplicator (tup0).bind (unbound_functor0); + functor1 = TupleApplicator (tup1).bind (unbound_functor1); + functor2 = TupleApplicator (tup2).bind (unbound_functor2); + functor3 = TupleApplicator (tup3).bind (unbound_functor3); + + ASSERT (-1 == functor0() ); + ASSERT (11 == functor1() ); + ASSERT (11+12 == functor2() ); + ASSERT (11+12+13 == functor3() ); + } void build_closure () { - UNIMPLEMENTED ("build a simple tuple closure"); + Tuple > tup0 ; + Tuple > tup1 (11); + Tuple > tup2 (11,12); + Tuple > tup3 (11,12,13); + + FunctionClosure clo0 (fun0,tup0); + FunctionClosure clo1 (fun1,tup1); + FunctionClosure clo2 (fun2,tup2); + FunctionClosure clo3 (fun3,tup3); + + ASSERT (-1 == clo0() ); + ASSERT (11 == clo1() ); + ASSERT (11+12 == clo2() ); + ASSERT (11+12+13 == clo3() ); + + function unbound_functor0 (fun0); + function unbound_functor1 (fun1); + function unbound_functor2 (fun2); + function unbound_functor3 (fun3); + + clo0 = FunctionClosure (unbound_functor0,tup0); + clo1 = FunctionClosure (unbound_functor1,tup1); + clo2 = FunctionClosure (unbound_functor2,tup2); + clo3 = FunctionClosure (unbound_functor3,tup3); + + ASSERT (-1 == clo0() ); + ASSERT (11 == clo1() ); + ASSERT (11+12 == clo2() ); + ASSERT (11+12+13 == clo3() ); + + ASSERT (-1 == func::closure(fun0,tup0) () ); + ASSERT (11 == func::closure(fun1,tup1) () ); + ASSERT (11+12 == func::closure(fun2,tup2) () ); + ASSERT (11+12+13 == func::closure(fun3,tup3) () ); + + ASSERT (-1 == func::closure(unbound_functor0,tup0) () ); + ASSERT (11 == func::closure(unbound_functor1,tup1) () ); + ASSERT (11+12 == func::closure(unbound_functor2,tup2) () ); + ASSERT (11+12+13 == func::closure(unbound_functor3,tup3) () ); + + + // finally combine all techniques.... + typedef Tuple::Type NumberzArg; + typedef FunctionTypedef::Sig NumberzSig; + Tuple numberzTup (Num<5>(22), Num<6>(33), Num<7>(44)); + + FunctionClosure numClo (getNumberz<5,6,7>, numberzTup ); + + ASSERT (22+33+44 == numClo() ); } }; diff --git a/tests/lib/meta/tuple-diagnostics.hpp b/tests/lib/meta/tuple-diagnostics.hpp index 63c176503..127b845e1 100644 --- a/tests/lib/meta/tuple-diagnostics.hpp +++ b/tests/lib/meta/tuple-diagnostics.hpp @@ -109,7 +109,7 @@ namespace test { TupleElementDisplayer(TUP const& tuple) : BASE(tuple) {} string - dump (string const& prefix = "(") const + dump (string const& prefix ="(") const { return BASE::dump (prefix+showTupElement(element())+","); } @@ -122,11 +122,8 @@ namespace test { public: TupleElementDisplayer(TUP const& tuple) : TUP(tuple) {} - - protected: - string - dump (string const& prefix) const + dump (string const& prefix ="(") const { if (1 < prefix.length()) // removing the trailing comma diff --git a/tests/lib/meta/type-tuple-test.cpp b/tests/lib/meta/type-tuple-test.cpp index 44e79aab3..a11358344 100644 --- a/tests/lib/meta/type-tuple-test.cpp +++ b/tests/lib/meta/type-tuple-test.cpp @@ -157,14 +157,53 @@ namespace test { void check_sub_tuple_types() { - UNIMPLEMENTED ("verify head and tail type"); + cout << "\t:\n\t: ---Head-and-Tail---\n"; + + typedef Append::List L2; + + typedef Tuple T_L2; + typedef Types Head; + typedef T_L2::TailType Tail; + DISPLAY (T_L2); + DISPLAY (Head); + DISPLAY (Tail); + + typedef T_L2::ThisTuple T2; + typedef Types Head2; + typedef T2::TailType Tail2; + DISPLAY (T2); + DISPLAY (Head2); + DISPLAY (Tail2); } void check_shiftedTuple() { - UNIMPLEMENTED ("verify shifted type tuple"); + cout << "\t:\n\t: ---Shifted-Tuple---\n"; + + typedef Append::List L3; + typedef Tuple::Type Ty3; + typedef Tuple T3; + + typedef Shifted::Type Ty_0; DISPLAY (Ty_0); + typedef Shifted::Type Ty_1; DISPLAY (Ty_1); + typedef Shifted::Type Ty_2; DISPLAY (Ty_2); + typedef Shifted::Type Ty_3; DISPLAY (Ty_3); + typedef Shifted::Type Ty_4; DISPLAY (Ty_4); + + typedef T3::ShiftedTuple<0>::Type T_0; DISPLAY (T_0); + typedef T3::ShiftedTuple<1>::Type T_1; DISPLAY (T_1); + typedef T3::ShiftedTuple<2>::Type T_2; DISPLAY (T_2); + typedef T3::ShiftedTuple<3>::Type T_3; DISPLAY (T_3); + typedef T3::ShiftedTuple<4>::Type T_4; DISPLAY (T_4); + + T3 tu3; DUMPVAL (tu3); + T_0 tu3_0 = tu3.getShifted<0>(); DUMPVAL (tu3_0); + T_1 tu3_1 = tu3.getShifted<1>(); DUMPVAL (tu3_1); + T_2 tu3_2 = tu3.getShifted<2>(); DUMPVAL (tu3_2); + T_3 tu3_3 = tu3.getShifted<3>(); DUMPVAL (tu3_3); + T_4 tu3_4 = tu3.getShifted<4>(); DUMPVAL (tu3_4); }