Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
1 // Boost Lambda Library ret.hpp -----------------------------------------
3 // Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see www.boost.org
12 #ifndef BOOST_LAMBDA_RET_HPP
13 #define BOOST_LAMBDA_RET_HPP
20 // Add specializations for function references for ret, protect and unlambda
21 // e.g void foo(); unlambda(foo); fails, as it would add a const qualifier
22 // for a function type.
23 // on the other hand unlambda(*foo) does work
26 // -- ret -------------------------
27 // the explicit return type template
29 // TODO: It'd be nice to make ret a nop for other than lambda functors
30 // but causes an ambiguiyty with gcc (not with KCC), check what is the
31 // right interpretation.
33 // // ret for others than lambda functors has no effect
34 // template <class U, class T>
35 // inline const T& ret(const T& t) { return t; }
38 template<class RET, class Arg>
42 explicit_return_type_action<RET>,
43 tuple<lambda_functor<Arg> >
46 ret(const lambda_functor<Arg>& a1)
50 explicit_return_type_action<RET>,
51 tuple<lambda_functor<Arg> >
53 (tuple<lambda_functor<Arg> >(a1));
56 // protect ------------------
58 // protecting others than lambda functors has no effect
60 inline const T& protect(const T& t) { return t; }
67 tuple<lambda_functor<Arg> >
70 protect(const lambda_functor<Arg>& a1)
75 tuple<lambda_functor<Arg> >
77 (tuple<lambda_functor<Arg> >(a1));
80 // -------------------------------------------------------------------
82 // Hides the lambda functorness of a lambda functor.
83 // After this, the functor is immune to argument substitution, etc.
84 // This can be used, e.g. to make it safe to pass lambda functors as
85 // arguments to functions, which might use them as target functions
87 // note, unlambda and protect are different things. Protect hides the lambda
88 // functor for one application, unlambda for good.
90 template <class LambdaFunctor>
91 class non_lambda_functor
96 // This functor defines the result_type typedef.
97 // The result type must be deducible without knowing the arguments
99 template <class SigArgs> struct sig {
101 LambdaFunctor::inherited::
102 template sig<typename SigArgs::tail_type>::type type;
105 explicit non_lambda_functor(const LambdaFunctor& a) : lf(a) {}
107 typename LambdaFunctor::nullary_return_type
110 call<typename LambdaFunctor::nullary_return_type>
111 (cnull_type(), cnull_type(), cnull_type(), cnull_type());
115 typename sig<tuple<const non_lambda_functor, A&> >::type
116 operator()(A& a) const {
117 return lf.template call<typename sig<tuple<const non_lambda_functor, A&> >::type >(a, cnull_type(), cnull_type(), cnull_type());
120 template<class A, class B>
121 typename sig<tuple<const non_lambda_functor, A&, B&> >::type
122 operator()(A& a, B& b) const {
123 return lf.template call<typename sig<tuple<const non_lambda_functor, A&, B&> >::type >(a, b, cnull_type(), cnull_type());
126 template<class A, class B, class C>
127 typename sig<tuple<const non_lambda_functor, A&, B&, C&> >::type
128 operator()(A& a, B& b, C& c) const {
129 return lf.template call<typename sig<tuple<const non_lambda_functor, A&, B&, C&> >::type>(a, b, c, cnull_type());
134 inline const Arg& unlambda(const Arg& a) { return a; }
137 inline const non_lambda_functor<lambda_functor<Arg> >
138 unlambda(const lambda_functor<Arg>& a)
140 return non_lambda_functor<lambda_functor<Arg> >(a);
143 // Due to a language restriction, lambda functors cannot be made to
144 // accept non-const rvalue arguments. Usually iterators do not return
145 // temporaries, but sometimes they do. That's why a workaround is provided.
146 // Note, that this potentially breaks const correctness, so be careful!
148 // any lambda functor can be turned into a const_incorrect_lambda_functor
149 // The operator() takes arguments as consts and then casts constness
150 // away. So this breaks const correctness!!! but is a necessary workaround
151 // in some cases due to language limitations.
152 // Note, that this is not a lambda_functor anymore, so it can not be used
153 // as a sub lambda expression.
155 template <class LambdaFunctor>
156 struct const_incorrect_lambda_functor {
160 explicit const_incorrect_lambda_functor(const LambdaFunctor& a) : lf(a) {}
162 template <class SigArgs> struct sig {
164 LambdaFunctor::inherited::template
165 sig<typename SigArgs::tail_type>::type type;
168 // The nullary case is not needed (no arguments, no parameter type problems)
171 typename sig<tuple<const const_incorrect_lambda_functor, A&> >::type
172 operator()(const A& a) const {
173 return lf.template call<typename sig<tuple<const const_incorrect_lambda_functor, A&> >::type >(const_cast<A&>(a), cnull_type(), cnull_type(), cnull_type());
176 template<class A, class B>
177 typename sig<tuple<const const_incorrect_lambda_functor, A&, B&> >::type
178 operator()(const A& a, const B& b) const {
179 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());
182 template<class A, class B, class C>
183 typename sig<tuple<const const_incorrect_lambda_functor, A&, B&, C&> >::type
184 operator()(const A& a, const B& b, const C& c) const {
185 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());
189 // ------------------------------------------------------------------------
190 // any lambda functor can be turned into a const_parameter_lambda_functor
191 // The operator() takes arguments as const.
192 // This is useful if lambda functors are called with non-const rvalues.
193 // Note, that this is not a lambda_functor anymore, so it can not be used
194 // as a sub lambda expression.
196 template <class LambdaFunctor>
197 struct const_parameter_lambda_functor {
201 explicit const_parameter_lambda_functor(const LambdaFunctor& a) : lf(a) {}
203 template <class SigArgs> struct sig {
205 LambdaFunctor::inherited::template
206 sig<typename SigArgs::tail_type>::type type;
209 // The nullary case is not needed: no arguments, no constness problems.
212 typename sig<tuple<const const_parameter_lambda_functor, const A&> >::type
213 operator()(const A& a) const {
214 return lf.template call<typename sig<tuple<const const_parameter_lambda_functor, const A&> >::type >(a, cnull_type(), cnull_type(), cnull_type());
217 template<class A, class B>
218 typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&> >::type
219 operator()(const A& a, const B& b) const {
220 return lf.template call<typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&> >::type >(a, b, cnull_type(), cnull_type());
223 template<class A, class B, class C>
224 typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&, const C&>
226 operator()(const A& a, const B& b, const C& c) const {
227 return lf.template call<typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&, const C&> >::type>(a, b, c, cnull_type());
232 inline const const_incorrect_lambda_functor<lambda_functor<Arg> >
233 break_const(const lambda_functor<Arg>& lf)
235 return const_incorrect_lambda_functor<lambda_functor<Arg> >(lf);
240 inline const const_parameter_lambda_functor<lambda_functor<Arg> >
241 const_parameters(const lambda_functor<Arg>& lf)
243 return const_parameter_lambda_functor<lambda_functor<Arg> >(lf);
246 // make void ------------------------------------------------
247 // make_void( x ) turns a lambda functor x with some return type y into
248 // another lambda functor, which has a void return type
249 // when called, the original return type is discarded
251 // we use this action. The action class will be called, which means that
252 // the wrapped lambda functor is evaluated, but we just don't do anything
254 struct voidifier_action {
255 template<class Ret, class A> static void apply(A&) {}
258 template<class Args> struct return_type_N<voidifier_action, Args> {
266 action<1, voidifier_action>,
267 tuple<lambda_functor<Arg1> >
270 make_void(const lambda_functor<Arg1>& a1) {
273 action<1, voidifier_action>,
274 tuple<lambda_functor<Arg1> >
276 (tuple<lambda_functor<Arg1> > (a1));
279 // for non-lambda functors, make_void does nothing
280 // (the argument gets evaluated immediately)
285 lambda_functor_base<do_nothing_action, null_type>
287 make_void(const Arg1& a1) {
289 lambda_functor_base<do_nothing_action, null_type>();
292 // std_functor -----------------------------------------------------
294 // The STL uses the result_type typedef as the convention to let binders know
295 // the return type of a function object.
296 // LL uses the sig template.
297 // To let LL know that the function object has the result_type typedef
298 // defined, it can be wrapped with the std_functor function.
301 // Just inherit form the template parameter (the standard functor),
302 // and provide a sig template. So we have a class which is still the
303 // same functor + the sig template.
306 struct result_type_to_sig : public T {
307 template<class Args> struct sig { typedef typename T::result_type type; };
308 result_type_to_sig(const T& t) : T(t) {}
312 inline result_type_to_sig<F> std_functor(const F& f) { return f; }
315 } // namespace lambda