source: NonGTP/Boost/boost/lambda/casts.hpp @ 857

Revision 857, 5.5 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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
19namespace boost {
20namespace lambda {
21
22template<class T> class cast_action;
23
24template<class T> class static_cast_action;
25template<class T> class dynamic_cast_action;
26template<class T> class const_cast_action;
27template<class T> class reinterpret_cast_action;
28
29class typeid_action;
30class sizeof_action;
31
32// Cast actions
33
34template<class T> class cast_action<static_cast_action<T> >
35{
36public:
37  template<class RET, class Arg1>
38  static RET apply(Arg1 &a1) {
39    return static_cast<RET>(a1);
40  }
41};
42
43template<class T> class cast_action<dynamic_cast_action<T> > {
44public:
45  template<class RET, class Arg1>
46  static RET apply(Arg1 &a1) {
47    return dynamic_cast<RET>(a1);
48  }
49};
50
51template<class T> class cast_action<const_cast_action<T> > {
52public:
53  template<class RET, class Arg1>
54  static RET apply(Arg1 &a1) {
55    return const_cast<RET>(a1);
56  }
57};
58
59template<class T> class cast_action<reinterpret_cast_action<T> > {
60public:
61  template<class RET, class Arg1>
62  static RET apply(Arg1 &a1) {
63    return reinterpret_cast<RET>(a1);
64  }
65};
66
67  // typedid action
68class typeid_action {
69public:
70  template<class RET, class Arg1>
71  static RET apply(Arg1 &a1) {
72    return typeid(a1);
73  }
74};
75
76// sizeof action
77class sizeof_action
78{
79public:
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
89template<template <class> class cast_type, class T, class A>
90struct return_type_N<cast_action< cast_type<T> >, A> {
91  typedef T type;
92};
93
94// return type of typeid_action
95template<class A>
96struct return_type_N<typeid_action, A> {
97  typedef std::type_info const & type;
98};
99
100// return type of sizeof_action
101
102template<class A>
103struct 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
112template <class T, class Arg1>
113inline 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>
119ll_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
129template <class T, class Arg1>
130inline 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>
136ll_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
146template <class T, class Arg1>
147inline 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>
153ll_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
163template <class T, class Arg1>
164inline 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>
170ll_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)
182template <class Arg1>
183inline const lambda_functor<
184  lambda_functor_base<
185    action<1, typeid_action>,
186    tuple<typename const_copy_argument <const Arg1>::type>
187  >
188>
189ll_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)
200template <class Arg1>
201inline const lambda_functor<
202  lambda_functor_base<
203    action<1, sizeof_action>,
204    tuple<lambda_functor<Arg1> >
205  >
206>
207ll_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
Note: See TracBrowser for help on using the repository browser.