First public contribution.
1 // - casts.hpp -- BLambda Library -------------
3 // Copyright (C) 2000 Gary Powell (powellg@amazon.com)
4 // Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
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)
10 // For more information, see http://www.boost.org
12 // -----------------------------------------------
14 #if !defined(BOOST_LAMBDA_CASTS_HPP)
15 #define BOOST_LAMBDA_CASTS_HPP
22 template<class T> class cast_action;
24 template<class T> class static_cast_action;
25 template<class T> class dynamic_cast_action;
26 template<class T> class const_cast_action;
27 template<class T> class reinterpret_cast_action;
34 template<class T> class cast_action<static_cast_action<T> >
37 template<class RET, class Arg1>
38 static RET apply(Arg1 &a1) {
39 return static_cast<RET>(a1);
43 template<class T> class cast_action<dynamic_cast_action<T> > {
45 template<class RET, class Arg1>
46 static RET apply(Arg1 &a1) {
47 return dynamic_cast<RET>(a1);
51 template<class T> class cast_action<const_cast_action<T> > {
53 template<class RET, class Arg1>
54 static RET apply(Arg1 &a1) {
55 return const_cast<RET>(a1);
59 template<class T> class cast_action<reinterpret_cast_action<T> > {
61 template<class RET, class Arg1>
62 static RET apply(Arg1 &a1) {
63 return reinterpret_cast<RET>(a1);
70 template<class RET, class Arg1>
71 static RET apply(Arg1 &a1) {
80 template<class RET, class Arg1>
81 static RET apply(Arg1 &a1) {
87 // return types of casting lambda_functors (all "T" type.)
89 template<template <class> class cast_type, class T, class A>
90 struct return_type_N<cast_action< cast_type<T> >, A> {
94 // return type of typeid_action
96 struct return_type_N<typeid_action, A> {
97 typedef std::type_info const & type;
100 // return type of sizeof_action
103 struct return_type_N<sizeof_action, A> {
104 typedef std::size_t type;
108 // the four cast & typeid overloads.
109 // casts can take ordinary variables (not just lambda functors)
112 template <class T, class Arg1>
113 inline const lambda_functor<
115 action<1, cast_action<static_cast_action<T> > >,
116 tuple<typename const_copy_argument <const Arg1>::type>
119 ll_static_cast(const Arg1& a1) {
122 action<1, cast_action<static_cast_action<T> > >,
123 tuple<typename const_copy_argument <const Arg1>::type>
125 ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
129 template <class T, class Arg1>
130 inline const lambda_functor<
132 action<1, cast_action<dynamic_cast_action<T> > >,
133 tuple<typename const_copy_argument <const Arg1>::type>
136 ll_dynamic_cast(const Arg1& a1) {
139 action<1, cast_action<dynamic_cast_action<T> > >,
140 tuple<typename const_copy_argument <const Arg1>::type>
142 ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
146 template <class T, class Arg1>
147 inline const lambda_functor<
149 action<1, cast_action<const_cast_action<T> > >,
150 tuple<typename const_copy_argument <const Arg1>::type>
153 ll_const_cast(const Arg1& a1) {
156 action<1, cast_action<const_cast_action<T> > >,
157 tuple<typename const_copy_argument <const Arg1>::type>
159 ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
163 template <class T, class Arg1>
164 inline const lambda_functor<
166 action<1, cast_action<reinterpret_cast_action<T> > >,
167 tuple<typename const_copy_argument <const Arg1>::type>
170 ll_reinterpret_cast(const Arg1& a1) {
173 action<1, cast_action<reinterpret_cast_action<T> > >,
174 tuple<typename const_copy_argument <const Arg1>::type>
176 ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
180 // can be applied to a normal variable as well (can refer to a polymorphic
182 template <class Arg1>
183 inline const lambda_functor<
185 action<1, typeid_action>,
186 tuple<typename const_copy_argument <const Arg1>::type>
189 ll_typeid(const Arg1& a1) {
192 action<1, typeid_action>,
193 tuple<typename const_copy_argument <const Arg1>::type>
195 ( tuple<typename const_copy_argument <const Arg1>::type>(a1));
198 // sizeof(expression)
199 // Always takes a lambda expression (if not, built in sizeof will do)
200 template <class Arg1>
201 inline const lambda_functor<
203 action<1, sizeof_action>,
204 tuple<lambda_functor<Arg1> >
207 ll_sizeof(const lambda_functor<Arg1>& a1) {
210 action<1, sizeof_action>,
211 tuple<lambda_functor<Arg1> >
213 ( tuple<lambda_functor<Arg1> >(a1));
216 } // namespace lambda