williamr@2: // Boost Lambda Library ret.hpp ----------------------------------------- williamr@2: williamr@2: // Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi) williamr@2: // williamr@2: // Distributed under the Boost Software License, Version 1.0. (See williamr@2: // accompanying file LICENSE_1_0.txt or copy at williamr@2: // http://www.boost.org/LICENSE_1_0.txt) williamr@2: // williamr@2: // For more information, see www.boost.org williamr@2: williamr@2: williamr@2: #ifndef BOOST_LAMBDA_RET_HPP williamr@2: #define BOOST_LAMBDA_RET_HPP williamr@2: williamr@2: namespace boost { williamr@2: namespace lambda { williamr@2: williamr@2: // TODO: williamr@2: williamr@2: // Add specializations for function references for ret, protect and unlambda williamr@2: // e.g void foo(); unlambda(foo); fails, as it would add a const qualifier williamr@2: // for a function type. williamr@2: // on the other hand unlambda(*foo) does work williamr@2: williamr@2: williamr@2: // -- ret ------------------------- williamr@2: // the explicit return type template williamr@2: williamr@2: // TODO: It'd be nice to make ret a nop for other than lambda functors williamr@2: // but causes an ambiguiyty with gcc (not with KCC), check what is the williamr@2: // right interpretation. williamr@2: williamr@2: // // ret for others than lambda functors has no effect williamr@2: // template williamr@2: // inline const T& ret(const T& t) { return t; } williamr@2: williamr@2: williamr@2: template williamr@2: inline const williamr@2: lambda_functor< williamr@2: lambda_functor_base< williamr@2: explicit_return_type_action, williamr@2: tuple > williamr@2: > williamr@2: > williamr@2: ret(const lambda_functor& a1) williamr@2: { williamr@2: return williamr@2: lambda_functor_base< williamr@2: explicit_return_type_action, williamr@2: tuple > williamr@2: > williamr@2: (tuple >(a1)); williamr@2: } williamr@2: williamr@2: // protect ------------------ williamr@2: williamr@2: // protecting others than lambda functors has no effect williamr@2: template williamr@2: inline const T& protect(const T& t) { return t; } williamr@2: williamr@2: template williamr@2: inline const williamr@2: lambda_functor< williamr@2: lambda_functor_base< williamr@2: protect_action, williamr@2: tuple > williamr@2: > williamr@2: > williamr@2: protect(const lambda_functor& a1) williamr@2: { williamr@2: return williamr@2: lambda_functor_base< williamr@2: protect_action, williamr@2: tuple > williamr@2: > williamr@2: (tuple >(a1)); williamr@2: } williamr@2: williamr@2: // ------------------------------------------------------------------- williamr@2: williamr@2: // Hides the lambda functorness of a lambda functor. williamr@2: // After this, the functor is immune to argument substitution, etc. williamr@2: // This can be used, e.g. to make it safe to pass lambda functors as williamr@2: // arguments to functions, which might use them as target functions williamr@2: williamr@2: // note, unlambda and protect are different things. Protect hides the lambda williamr@2: // functor for one application, unlambda for good. williamr@2: williamr@2: template williamr@2: class non_lambda_functor williamr@2: { williamr@2: LambdaFunctor lf; williamr@2: public: williamr@2: williamr@2: // This functor defines the result_type typedef. williamr@2: // The result type must be deducible without knowing the arguments williamr@2: williamr@2: template struct sig { williamr@2: typedef typename williamr@2: LambdaFunctor::inherited:: williamr@2: template sig::type type; williamr@2: }; williamr@2: williamr@2: explicit non_lambda_functor(const LambdaFunctor& a) : lf(a) {} williamr@2: williamr@2: typename LambdaFunctor::nullary_return_type williamr@2: operator()() const { williamr@2: return lf.template williamr@2: call williamr@2: (cnull_type(), cnull_type(), cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(A& a) const { williamr@2: return lf.template call >::type >(a, cnull_type(), cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(A& a, B& b) const { williamr@2: return lf.template call >::type >(a, b, cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(A& a, B& b, C& c) const { williamr@2: return lf.template call >::type>(a, b, c, cnull_type()); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline const Arg& unlambda(const Arg& a) { return a; } williamr@2: williamr@2: template williamr@2: inline const non_lambda_functor > williamr@2: unlambda(const lambda_functor& a) williamr@2: { williamr@2: return non_lambda_functor >(a); williamr@2: } williamr@2: williamr@2: // Due to a language restriction, lambda functors cannot be made to williamr@2: // accept non-const rvalue arguments. Usually iterators do not return williamr@2: // temporaries, but sometimes they do. That's why a workaround is provided. williamr@2: // Note, that this potentially breaks const correctness, so be careful! williamr@2: williamr@2: // any lambda functor can be turned into a const_incorrect_lambda_functor williamr@2: // The operator() takes arguments as consts and then casts constness williamr@2: // away. So this breaks const correctness!!! but is a necessary workaround williamr@2: // in some cases due to language limitations. williamr@2: // Note, that this is not a lambda_functor anymore, so it can not be used williamr@2: // as a sub lambda expression. williamr@2: williamr@2: template williamr@2: struct const_incorrect_lambda_functor { williamr@2: LambdaFunctor lf; williamr@2: public: williamr@2: williamr@2: explicit const_incorrect_lambda_functor(const LambdaFunctor& a) : lf(a) {} williamr@2: williamr@2: template struct sig { williamr@2: typedef typename williamr@2: LambdaFunctor::inherited::template williamr@2: sig::type type; williamr@2: }; williamr@2: williamr@2: // The nullary case is not needed (no arguments, no parameter type problems) williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(const A& a) const { williamr@2: return lf.template call >::type >(const_cast(a), cnull_type(), cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(const A& a, const B& b) const { williamr@2: return lf.template call >::type >(const_cast(a), const_cast(b), cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(const A& a, const B& b, const C& c) const { williamr@2: return lf.template call >::type>(const_cast(a), const_cast(b), const_cast(c), cnull_type()); williamr@2: } williamr@2: }; williamr@2: williamr@2: // ------------------------------------------------------------------------ williamr@2: // any lambda functor can be turned into a const_parameter_lambda_functor williamr@2: // The operator() takes arguments as const. williamr@2: // This is useful if lambda functors are called with non-const rvalues. williamr@2: // Note, that this is not a lambda_functor anymore, so it can not be used williamr@2: // as a sub lambda expression. williamr@2: williamr@2: template williamr@2: struct const_parameter_lambda_functor { williamr@2: LambdaFunctor lf; williamr@2: public: williamr@2: williamr@2: explicit const_parameter_lambda_functor(const LambdaFunctor& a) : lf(a) {} williamr@2: williamr@2: template struct sig { williamr@2: typedef typename williamr@2: LambdaFunctor::inherited::template williamr@2: sig::type type; williamr@2: }; williamr@2: williamr@2: // The nullary case is not needed: no arguments, no constness problems. williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(const A& a) const { williamr@2: return lf.template call >::type >(a, cnull_type(), cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig >::type williamr@2: operator()(const A& a, const B& b) const { williamr@2: return lf.template call >::type >(a, b, cnull_type(), cnull_type()); williamr@2: } williamr@2: williamr@2: template williamr@2: typename sig williamr@2: >::type williamr@2: operator()(const A& a, const B& b, const C& c) const { williamr@2: return lf.template call >::type>(a, b, c, cnull_type()); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline const const_incorrect_lambda_functor > williamr@2: break_const(const lambda_functor& lf) williamr@2: { williamr@2: return const_incorrect_lambda_functor >(lf); williamr@2: } williamr@2: williamr@2: williamr@2: template williamr@2: inline const const_parameter_lambda_functor > williamr@2: const_parameters(const lambda_functor& lf) williamr@2: { williamr@2: return const_parameter_lambda_functor >(lf); williamr@2: } williamr@2: williamr@2: // make void ------------------------------------------------ williamr@2: // make_void( x ) turns a lambda functor x with some return type y into williamr@2: // another lambda functor, which has a void return type williamr@2: // when called, the original return type is discarded williamr@2: williamr@2: // we use this action. The action class will be called, which means that williamr@2: // the wrapped lambda functor is evaluated, but we just don't do anything williamr@2: // with the result. williamr@2: struct voidifier_action { williamr@2: template static void apply(A&) {} williamr@2: }; williamr@2: williamr@2: template struct return_type_N { williamr@2: typedef void type; williamr@2: }; williamr@2: williamr@2: template williamr@2: inline const williamr@2: lambda_functor< williamr@2: lambda_functor_base< williamr@2: action<1, voidifier_action>, williamr@2: tuple > williamr@2: > williamr@2: > williamr@2: make_void(const lambda_functor& a1) { williamr@2: return williamr@2: lambda_functor_base< williamr@2: action<1, voidifier_action>, williamr@2: tuple > williamr@2: > williamr@2: (tuple > (a1)); williamr@2: } williamr@2: williamr@2: // for non-lambda functors, make_void does nothing williamr@2: // (the argument gets evaluated immediately) williamr@2: williamr@2: template williamr@2: inline const williamr@2: lambda_functor< williamr@2: lambda_functor_base williamr@2: > williamr@2: make_void(const Arg1& a1) { williamr@2: return williamr@2: lambda_functor_base(); williamr@2: } williamr@2: williamr@2: // std_functor ----------------------------------------------------- williamr@2: williamr@2: // The STL uses the result_type typedef as the convention to let binders know williamr@2: // the return type of a function object. williamr@2: // LL uses the sig template. williamr@2: // To let LL know that the function object has the result_type typedef williamr@2: // defined, it can be wrapped with the std_functor function. williamr@2: williamr@2: williamr@2: // Just inherit form the template parameter (the standard functor), williamr@2: // and provide a sig template. So we have a class which is still the williamr@2: // same functor + the sig template. williamr@2: williamr@2: template williamr@2: struct result_type_to_sig : public T { williamr@2: template struct sig { typedef typename T::result_type type; }; williamr@2: result_type_to_sig(const T& t) : T(t) {} williamr@2: }; williamr@2: williamr@2: template williamr@2: inline result_type_to_sig std_functor(const F& f) { return f; } williamr@2: williamr@2: williamr@2: } // namespace lambda williamr@2: } // namespace boost williamr@2: williamr@2: #endif williamr@2: williamr@2: williamr@2: williamr@2: williamr@2: williamr@2: williamr@2: