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