ret.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Boost Lambda Library ret.hpp -----------------------------------------
  2. // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. #ifndef BOOST_LAMBDA_RET_HPP
  10. #define BOOST_LAMBDA_RET_HPP
  11. namespace boost {
  12. namespace lambda {
  13. // TODO:
  14. // Add specializations for function references for ret, protect and unlambda
  15. // e.g void foo(); unlambda(foo); fails, as it would add a const qualifier
  16. // for a function type.
  17. // on the other hand unlambda(*foo) does work
  18. // -- ret -------------------------
  19. // the explicit return type template
  20. // TODO: It'd be nice to make ret a nop for other than lambda functors
  21. // but causes an ambiguiyty with gcc (not with KCC), check what is the
  22. // right interpretation.
  23. // // ret for others than lambda functors has no effect
  24. // template <class U, class T>
  25. // inline const T& ret(const T& t) { return t; }
  26. template<class RET, class Arg>
  27. inline const
  28. lambda_functor<
  29. lambda_functor_base<
  30. explicit_return_type_action<RET>,
  31. tuple<lambda_functor<Arg> >
  32. >
  33. >
  34. ret(const lambda_functor<Arg>& a1)
  35. {
  36. return
  37. lambda_functor_base<
  38. explicit_return_type_action<RET>,
  39. tuple<lambda_functor<Arg> >
  40. >
  41. (tuple<lambda_functor<Arg> >(a1));
  42. }
  43. // protect ------------------
  44. // protecting others than lambda functors has no effect
  45. template <class T>
  46. inline const T& protect(const T& t) { return t; }
  47. template<class Arg>
  48. inline const
  49. lambda_functor<
  50. lambda_functor_base<
  51. protect_action,
  52. tuple<lambda_functor<Arg> >
  53. >
  54. >
  55. protect(const lambda_functor<Arg>& a1)
  56. {
  57. return
  58. lambda_functor_base<
  59. protect_action,
  60. tuple<lambda_functor<Arg> >
  61. >
  62. (tuple<lambda_functor<Arg> >(a1));
  63. }
  64. // -------------------------------------------------------------------
  65. // Hides the lambda functorness of a lambda functor.
  66. // After this, the functor is immune to argument substitution, etc.
  67. // This can be used, e.g. to make it safe to pass lambda functors as
  68. // arguments to functions, which might use them as target functions
  69. // note, unlambda and protect are different things. Protect hides the lambda
  70. // functor for one application, unlambda for good.
  71. template <class LambdaFunctor>
  72. class non_lambda_functor
  73. {
  74. LambdaFunctor lf;
  75. public:
  76. // This functor defines the result_type typedef.
  77. // The result type must be deducible without knowing the arguments
  78. template <class SigArgs> struct sig {
  79. typedef typename
  80. LambdaFunctor::inherited::
  81. template sig<typename SigArgs::tail_type>::type type;
  82. };
  83. explicit non_lambda_functor(const LambdaFunctor& a) : lf(a) {}
  84. typename LambdaFunctor::nullary_return_type
  85. operator()() const {
  86. return lf.template
  87. call<typename LambdaFunctor::nullary_return_type>
  88. (cnull_type(), cnull_type(), cnull_type(), cnull_type());
  89. }
  90. template<class A>
  91. typename sig<tuple<const non_lambda_functor, A&> >::type
  92. operator()(A& a) const {
  93. return lf.template call<typename sig<tuple<const non_lambda_functor, A&> >::type >(a, cnull_type(), cnull_type(), cnull_type());
  94. }
  95. template<class A, class B>
  96. typename sig<tuple<const non_lambda_functor, A&, B&> >::type
  97. operator()(A& a, B& b) const {
  98. return lf.template call<typename sig<tuple<const non_lambda_functor, A&, B&> >::type >(a, b, cnull_type(), cnull_type());
  99. }
  100. template<class A, class B, class C>
  101. typename sig<tuple<const non_lambda_functor, A&, B&, C&> >::type
  102. operator()(A& a, B& b, C& c) const {
  103. return lf.template call<typename sig<tuple<const non_lambda_functor, A&, B&, C&> >::type>(a, b, c, cnull_type());
  104. }
  105. };
  106. template <class Arg>
  107. inline const Arg& unlambda(const Arg& a) { return a; }
  108. template <class Arg>
  109. inline const non_lambda_functor<lambda_functor<Arg> >
  110. unlambda(const lambda_functor<Arg>& a)
  111. {
  112. return non_lambda_functor<lambda_functor<Arg> >(a);
  113. }
  114. // Due to a language restriction, lambda functors cannot be made to
  115. // accept non-const rvalue arguments. Usually iterators do not return
  116. // temporaries, but sometimes they do. That's why a workaround is provided.
  117. // Note, that this potentially breaks const correctness, so be careful!
  118. // any lambda functor can be turned into a const_incorrect_lambda_functor
  119. // The operator() takes arguments as consts and then casts constness
  120. // away. So this breaks const correctness!!! but is a necessary workaround
  121. // in some cases due to language limitations.
  122. // Note, that this is not a lambda_functor anymore, so it can not be used
  123. // as a sub lambda expression.
  124. template <class LambdaFunctor>
  125. struct const_incorrect_lambda_functor {
  126. LambdaFunctor lf;
  127. public:
  128. explicit const_incorrect_lambda_functor(const LambdaFunctor& a) : lf(a) {}
  129. template <class SigArgs> struct sig {
  130. typedef typename
  131. LambdaFunctor::inherited::template
  132. sig<typename SigArgs::tail_type>::type type;
  133. };
  134. // The nullary case is not needed (no arguments, no parameter type problems)
  135. template<class A>
  136. typename sig<tuple<const const_incorrect_lambda_functor, A&> >::type
  137. operator()(const A& a) const {
  138. return lf.template call<typename sig<tuple<const const_incorrect_lambda_functor, A&> >::type >(const_cast<A&>(a), cnull_type(), cnull_type(), cnull_type());
  139. }
  140. template<class A, class B>
  141. typename sig<tuple<const const_incorrect_lambda_functor, A&, B&> >::type
  142. operator()(const A& a, const B& b) const {
  143. 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());
  144. }
  145. template<class A, class B, class C>
  146. typename sig<tuple<const const_incorrect_lambda_functor, A&, B&, C&> >::type
  147. operator()(const A& a, const B& b, const C& c) const {
  148. 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());
  149. }
  150. };
  151. // ------------------------------------------------------------------------
  152. // any lambda functor can be turned into a const_parameter_lambda_functor
  153. // The operator() takes arguments as const.
  154. // This is useful if lambda functors are called with non-const rvalues.
  155. // Note, that this is not a lambda_functor anymore, so it can not be used
  156. // as a sub lambda expression.
  157. template <class LambdaFunctor>
  158. struct const_parameter_lambda_functor {
  159. LambdaFunctor lf;
  160. public:
  161. explicit const_parameter_lambda_functor(const LambdaFunctor& a) : lf(a) {}
  162. template <class SigArgs> struct sig {
  163. typedef typename
  164. LambdaFunctor::inherited::template
  165. sig<typename SigArgs::tail_type>::type type;
  166. };
  167. // The nullary case is not needed: no arguments, no constness problems.
  168. template<class A>
  169. typename sig<tuple<const const_parameter_lambda_functor, const A&> >::type
  170. operator()(const A& a) const {
  171. return lf.template call<typename sig<tuple<const const_parameter_lambda_functor, const A&> >::type >(a, cnull_type(), cnull_type(), cnull_type());
  172. }
  173. template<class A, class B>
  174. typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&> >::type
  175. operator()(const A& a, const B& b) const {
  176. return lf.template call<typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&> >::type >(a, b, cnull_type(), cnull_type());
  177. }
  178. template<class A, class B, class C>
  179. typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&, const C&>
  180. >::type
  181. operator()(const A& a, const B& b, const C& c) const {
  182. return lf.template call<typename sig<tuple<const const_parameter_lambda_functor, const A&, const B&, const C&> >::type>(a, b, c, cnull_type());
  183. }
  184. };
  185. template <class Arg>
  186. inline const const_incorrect_lambda_functor<lambda_functor<Arg> >
  187. break_const(const lambda_functor<Arg>& lf)
  188. {
  189. return const_incorrect_lambda_functor<lambda_functor<Arg> >(lf);
  190. }
  191. template <class Arg>
  192. inline const const_parameter_lambda_functor<lambda_functor<Arg> >
  193. const_parameters(const lambda_functor<Arg>& lf)
  194. {
  195. return const_parameter_lambda_functor<lambda_functor<Arg> >(lf);
  196. }
  197. // make void ------------------------------------------------
  198. // make_void( x ) turns a lambda functor x with some return type y into
  199. // another lambda functor, which has a void return type
  200. // when called, the original return type is discarded
  201. // we use this action. The action class will be called, which means that
  202. // the wrapped lambda functor is evaluated, but we just don't do anything
  203. // with the result.
  204. struct voidifier_action {
  205. template<class Ret, class A> static void apply(A&) {}
  206. };
  207. template<class Args> struct return_type_N<voidifier_action, Args> {
  208. typedef void type;
  209. };
  210. template<class Arg1>
  211. inline const
  212. lambda_functor<
  213. lambda_functor_base<
  214. action<1, voidifier_action>,
  215. tuple<lambda_functor<Arg1> >
  216. >
  217. >
  218. make_void(const lambda_functor<Arg1>& a1) {
  219. return
  220. lambda_functor_base<
  221. action<1, voidifier_action>,
  222. tuple<lambda_functor<Arg1> >
  223. >
  224. (tuple<lambda_functor<Arg1> > (a1));
  225. }
  226. // for non-lambda functors, make_void does nothing
  227. // (the argument gets evaluated immediately)
  228. template<class Arg1>
  229. inline const
  230. lambda_functor<
  231. lambda_functor_base<do_nothing_action, null_type>
  232. >
  233. make_void(const Arg1&) {
  234. return
  235. lambda_functor_base<do_nothing_action, null_type>();
  236. }
  237. // std_functor -----------------------------------------------------
  238. // The STL uses the result_type typedef as the convention to let binders know
  239. // the return type of a function object.
  240. // LL uses the sig template.
  241. // To let LL know that the function object has the result_type typedef
  242. // defined, it can be wrapped with the std_functor function.
  243. // Just inherit form the template parameter (the standard functor),
  244. // and provide a sig template. So we have a class which is still the
  245. // same functor + the sig template.
  246. template<class T>
  247. struct result_type_to_sig : public T {
  248. template<class Args> struct sig { typedef typename T::result_type type; };
  249. result_type_to_sig(const T& t) : T(t) {}
  250. };
  251. template<class F>
  252. inline result_type_to_sig<F> std_functor(const F& f) { return f; }
  253. } // namespace lambda
  254. } // namespace boost
  255. #endif