1 | // - casts.hpp -- BLambda Library ------------- |
---|
2 | // |
---|
3 | // Copyright (C) 2000 Gary Powell (powellg@amazon.com) |
---|
4 | // Copyright (C) 1999, 2000 Jaakko Järvi (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 | // ----------------------------------------------- |
---|
13 | |
---|
14 | #if !defined(BOOST_LAMBDA_CASTS_HPP) |
---|
15 | #define BOOST_LAMBDA_CASTS_HPP |
---|
16 | |
---|
17 | #include <typeinfo> |
---|
18 | |
---|
19 | namespace boost { |
---|
20 | namespace lambda { |
---|
21 | |
---|
22 | template<class T> class cast_action; |
---|
23 | |
---|
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; |
---|
28 | |
---|
29 | class typeid_action; |
---|
30 | class sizeof_action; |
---|
31 | |
---|
32 | // Cast actions |
---|
33 | |
---|
34 | template<class T> class cast_action<static_cast_action<T> > |
---|
35 | { |
---|
36 | public: |
---|
37 | template<class RET, class Arg1> |
---|
38 | static RET apply(Arg1 &a1) { |
---|
39 | return static_cast<RET>(a1); |
---|
40 | } |
---|
41 | }; |
---|
42 | |
---|
43 | template<class T> class cast_action<dynamic_cast_action<T> > { |
---|
44 | public: |
---|
45 | template<class RET, class Arg1> |
---|
46 | static RET apply(Arg1 &a1) { |
---|
47 | return dynamic_cast<RET>(a1); |
---|
48 | } |
---|
49 | }; |
---|
50 | |
---|
51 | template<class T> class cast_action<const_cast_action<T> > { |
---|
52 | public: |
---|
53 | template<class RET, class Arg1> |
---|
54 | static RET apply(Arg1 &a1) { |
---|
55 | return const_cast<RET>(a1); |
---|
56 | } |
---|
57 | }; |
---|
58 | |
---|
59 | template<class T> class cast_action<reinterpret_cast_action<T> > { |
---|
60 | public: |
---|
61 | template<class RET, class Arg1> |
---|
62 | static RET apply(Arg1 &a1) { |
---|
63 | return reinterpret_cast<RET>(a1); |
---|
64 | } |
---|
65 | }; |
---|
66 | |
---|
67 | // typedid action |
---|
68 | class typeid_action { |
---|
69 | public: |
---|
70 | template<class RET, class Arg1> |
---|
71 | static RET apply(Arg1 &a1) { |
---|
72 | return typeid(a1); |
---|
73 | } |
---|
74 | }; |
---|
75 | |
---|
76 | // sizeof action |
---|
77 | class sizeof_action |
---|
78 | { |
---|
79 | public: |
---|
80 | template<class RET, class Arg1> |
---|
81 | static RET apply(Arg1 &a1) { |
---|
82 | return sizeof(a1); |
---|
83 | } |
---|
84 | }; |
---|
85 | |
---|
86 | |
---|
87 | // return types of casting lambda_functors (all "T" type.) |
---|
88 | |
---|
89 | template<template <class> class cast_type, class T, class A> |
---|
90 | struct return_type_N<cast_action< cast_type<T> >, A> { |
---|
91 | typedef T type; |
---|
92 | }; |
---|
93 | |
---|
94 | // return type of typeid_action |
---|
95 | template<class A> |
---|
96 | struct return_type_N<typeid_action, A> { |
---|
97 | typedef std::type_info const & type; |
---|
98 | }; |
---|
99 | |
---|
100 | // return type of sizeof_action |
---|
101 | |
---|
102 | template<class A> |
---|
103 | struct return_type_N<sizeof_action, A> { |
---|
104 | typedef std::size_t type; |
---|
105 | }; |
---|
106 | |
---|
107 | |
---|
108 | // the four cast & typeid overloads. |
---|
109 | // casts can take ordinary variables (not just lambda functors) |
---|
110 | |
---|
111 | // static_cast |
---|
112 | template <class T, class Arg1> |
---|
113 | inline const lambda_functor< |
---|
114 | lambda_functor_base< |
---|
115 | action<1, cast_action<static_cast_action<T> > >, |
---|
116 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
117 | > |
---|
118 | > |
---|
119 | ll_static_cast(const Arg1& a1) { |
---|
120 | return |
---|
121 | lambda_functor_base< |
---|
122 | action<1, cast_action<static_cast_action<T> > >, |
---|
123 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
124 | > |
---|
125 | ( tuple<typename const_copy_argument <const Arg1>::type>(a1)); |
---|
126 | } |
---|
127 | |
---|
128 | // dynamic_cast |
---|
129 | template <class T, class Arg1> |
---|
130 | inline const lambda_functor< |
---|
131 | lambda_functor_base< |
---|
132 | action<1, cast_action<dynamic_cast_action<T> > >, |
---|
133 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
134 | > |
---|
135 | > |
---|
136 | ll_dynamic_cast(const Arg1& a1) { |
---|
137 | return |
---|
138 | lambda_functor_base< |
---|
139 | action<1, cast_action<dynamic_cast_action<T> > >, |
---|
140 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
141 | > |
---|
142 | ( tuple<typename const_copy_argument <const Arg1>::type>(a1)); |
---|
143 | } |
---|
144 | |
---|
145 | // const_cast |
---|
146 | template <class T, class Arg1> |
---|
147 | inline const lambda_functor< |
---|
148 | lambda_functor_base< |
---|
149 | action<1, cast_action<const_cast_action<T> > >, |
---|
150 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
151 | > |
---|
152 | > |
---|
153 | ll_const_cast(const Arg1& a1) { |
---|
154 | return |
---|
155 | lambda_functor_base< |
---|
156 | action<1, cast_action<const_cast_action<T> > >, |
---|
157 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
158 | > |
---|
159 | ( tuple<typename const_copy_argument <const Arg1>::type>(a1)); |
---|
160 | } |
---|
161 | |
---|
162 | // reinterpret_cast |
---|
163 | template <class T, class Arg1> |
---|
164 | inline const lambda_functor< |
---|
165 | lambda_functor_base< |
---|
166 | action<1, cast_action<reinterpret_cast_action<T> > >, |
---|
167 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
168 | > |
---|
169 | > |
---|
170 | ll_reinterpret_cast(const Arg1& a1) { |
---|
171 | return |
---|
172 | lambda_functor_base< |
---|
173 | action<1, cast_action<reinterpret_cast_action<T> > >, |
---|
174 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
175 | > |
---|
176 | ( tuple<typename const_copy_argument <const Arg1>::type>(a1)); |
---|
177 | } |
---|
178 | |
---|
179 | // typeid |
---|
180 | // can be applied to a normal variable as well (can refer to a polymorphic |
---|
181 | // class object) |
---|
182 | template <class Arg1> |
---|
183 | inline const lambda_functor< |
---|
184 | lambda_functor_base< |
---|
185 | action<1, typeid_action>, |
---|
186 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
187 | > |
---|
188 | > |
---|
189 | ll_typeid(const Arg1& a1) { |
---|
190 | return |
---|
191 | lambda_functor_base< |
---|
192 | action<1, typeid_action>, |
---|
193 | tuple<typename const_copy_argument <const Arg1>::type> |
---|
194 | > |
---|
195 | ( tuple<typename const_copy_argument <const Arg1>::type>(a1)); |
---|
196 | } |
---|
197 | |
---|
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< |
---|
202 | lambda_functor_base< |
---|
203 | action<1, sizeof_action>, |
---|
204 | tuple<lambda_functor<Arg1> > |
---|
205 | > |
---|
206 | > |
---|
207 | ll_sizeof(const lambda_functor<Arg1>& a1) { |
---|
208 | return |
---|
209 | lambda_functor_base< |
---|
210 | action<1, sizeof_action>, |
---|
211 | tuple<lambda_functor<Arg1> > |
---|
212 | > |
---|
213 | ( tuple<lambda_functor<Arg1> >(a1)); |
---|
214 | } |
---|
215 | |
---|
216 | } // namespace lambda |
---|
217 | } // namespace boost |
---|
218 | |
---|
219 | #endif |
---|