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