casts.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // - casts.hpp -- BLambda Library -------------
  2. //
  3. // Copyright (C) 2000 Gary Powell (powellg@amazon.com)
  4. // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // For more information, see http://www.boost.org
  11. // -----------------------------------------------
  12. #if !defined(BOOST_LAMBDA_CASTS_HPP)
  13. #define BOOST_LAMBDA_CASTS_HPP
  14. #include "boost/lambda/detail/suppress_unused.hpp"
  15. #include "boost/lambda/core.hpp"
  16. #include <typeinfo>
  17. namespace boost {
  18. namespace lambda {
  19. template<class Act, class Args>
  20. struct return_type_N;
  21. template<class T> class cast_action;
  22. template<class T> class static_cast_action;
  23. template<class T> class dynamic_cast_action;
  24. template<class T> class const_cast_action;
  25. template<class T> class reinterpret_cast_action;
  26. class typeid_action;
  27. class sizeof_action;
  28. // Cast actions
  29. template<class T> class cast_action<static_cast_action<T> >
  30. {
  31. public:
  32. template<class RET, class Arg1>
  33. static RET apply(Arg1 &a1) {
  34. return static_cast<RET>(a1);
  35. }
  36. };
  37. template<class T> class cast_action<dynamic_cast_action<T> > {
  38. public:
  39. template<class RET, class Arg1>
  40. static RET apply(Arg1 &a1) {
  41. return dynamic_cast<RET>(a1);
  42. }
  43. };
  44. template<class T> class cast_action<const_cast_action<T> > {
  45. public:
  46. template<class RET, class Arg1>
  47. static RET apply(Arg1 &a1) {
  48. return const_cast<RET>(a1);
  49. }
  50. };
  51. template<class T> class cast_action<reinterpret_cast_action<T> > {
  52. public:
  53. template<class RET, class Arg1>
  54. static RET apply(Arg1 &a1) {
  55. return reinterpret_cast<RET>(a1);
  56. }
  57. };
  58. // typeid action
  59. class typeid_action {
  60. public:
  61. template<class RET, class Arg1>
  62. static RET apply(Arg1 &a1) {
  63. detail::suppress_unused_variable_warnings(a1);
  64. return typeid(a1);
  65. }
  66. };
  67. // sizeof action
  68. class sizeof_action
  69. {
  70. public:
  71. template<class RET, class Arg1>
  72. static RET apply(Arg1 &a1) {
  73. return sizeof(a1);
  74. }
  75. };
  76. // return types of casting lambda_functors (all "T" type.)
  77. template<template <class> class cast_type, class T, class A>
  78. struct return_type_N<cast_action< cast_type<T> >, A> {
  79. typedef T type;
  80. };
  81. // return type of typeid_action
  82. template<class A>
  83. struct return_type_N<typeid_action, A> {
  84. typedef std::type_info const & type;
  85. };
  86. // return type of sizeof_action
  87. template<class A>
  88. struct return_type_N<sizeof_action, A> {
  89. typedef std::size_t type;
  90. };
  91. // the four cast & typeid overloads.
  92. // casts can take ordinary variables (not just lambda functors)
  93. // static_cast
  94. template <class T, class Arg1>
  95. inline const lambda_functor<
  96. lambda_functor_base<
  97. action<1, cast_action<static_cast_action<T> > >,
  98. tuple<typename const_copy_argument <const Arg1>::type>
  99. >
  100. >
  101. ll_static_cast(const Arg1& a1) {
  102. return
  103. lambda_functor_base<
  104. action<1, cast_action<static_cast_action<T> > >,
  105. tuple<typename const_copy_argument <const Arg1>::type>
  106. >
  107. ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
  108. }
  109. // dynamic_cast
  110. template <class T, class Arg1>
  111. inline const lambda_functor<
  112. lambda_functor_base<
  113. action<1, cast_action<dynamic_cast_action<T> > >,
  114. tuple<typename const_copy_argument <const Arg1>::type>
  115. >
  116. >
  117. ll_dynamic_cast(const Arg1& a1) {
  118. return
  119. lambda_functor_base<
  120. action<1, cast_action<dynamic_cast_action<T> > >,
  121. tuple<typename const_copy_argument <const Arg1>::type>
  122. >
  123. ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
  124. }
  125. // const_cast
  126. template <class T, class Arg1>
  127. inline const lambda_functor<
  128. lambda_functor_base<
  129. action<1, cast_action<const_cast_action<T> > >,
  130. tuple<typename const_copy_argument <const Arg1>::type>
  131. >
  132. >
  133. ll_const_cast(const Arg1& a1) {
  134. return
  135. lambda_functor_base<
  136. action<1, cast_action<const_cast_action<T> > >,
  137. tuple<typename const_copy_argument <const Arg1>::type>
  138. >
  139. ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
  140. }
  141. // reinterpret_cast
  142. template <class T, class Arg1>
  143. inline const lambda_functor<
  144. lambda_functor_base<
  145. action<1, cast_action<reinterpret_cast_action<T> > >,
  146. tuple<typename const_copy_argument <const Arg1>::type>
  147. >
  148. >
  149. ll_reinterpret_cast(const Arg1& a1) {
  150. return
  151. lambda_functor_base<
  152. action<1, cast_action<reinterpret_cast_action<T> > >,
  153. tuple<typename const_copy_argument <const Arg1>::type>
  154. >
  155. ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
  156. }
  157. // typeid
  158. // can be applied to a normal variable as well (can refer to a polymorphic
  159. // class object)
  160. template <class Arg1>
  161. inline const lambda_functor<
  162. lambda_functor_base<
  163. action<1, typeid_action>,
  164. tuple<typename const_copy_argument <const Arg1>::type>
  165. >
  166. >
  167. ll_typeid(const Arg1& a1) {
  168. return
  169. lambda_functor_base<
  170. action<1, typeid_action>,
  171. tuple<typename const_copy_argument <const Arg1>::type>
  172. >
  173. ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
  174. }
  175. // sizeof(expression)
  176. // Always takes a lambda expression (if not, built in sizeof will do)
  177. template <class Arg1>
  178. inline const lambda_functor<
  179. lambda_functor_base<
  180. action<1, sizeof_action>,
  181. tuple<lambda_functor<Arg1> >
  182. >
  183. >
  184. ll_sizeof(const lambda_functor<Arg1>& a1) {
  185. return
  186. lambda_functor_base<
  187. action<1, sizeof_action>,
  188. tuple<lambda_functor<Arg1> >
  189. >
  190. ( tuple<lambda_functor<Arg1> >(a1));
  191. }
  192. } // namespace lambda
  193. } // namespace boost
  194. #endif