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