Chris@16
|
1
|
Chris@16
|
2 // Copyright (C) 2005 Igor Chesnokov, mailto:ichesnokov@gmail.com (VC 6.5,VC 7.1 + counter code)
|
Chris@16
|
3 // Copyright (C) 2005-2007 Peder Holt (VC 7.0 + framework)
|
Chris@16
|
4 // Copyright (C) 2006 Steven Watanabe (VC 8.0)
|
Chris@16
|
5
|
Chris@16
|
6 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
7 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
|
Chris@16
|
10 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
|
Chris@16
|
11
|
Chris@16
|
12 # include <boost/config.hpp>
|
Chris@16
|
13 # include <boost/detail/workaround.hpp>
|
Chris@16
|
14 # include <boost/mpl/int.hpp>
|
Chris@16
|
15 # include <boost/type_traits/is_function.hpp>
|
Chris@16
|
16 # include <boost/utility/enable_if.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 # if BOOST_WORKAROUND(BOOST_MSVC,>=1310)
|
Chris@16
|
19 # include <typeinfo>
|
Chris@16
|
20 # endif
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost
|
Chris@16
|
23 {
|
Chris@16
|
24 namespace type_of
|
Chris@16
|
25 {
|
Chris@16
|
26
|
Chris@16
|
27 //Compile time constant code
|
Chris@16
|
28 # if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS)
|
Chris@16
|
29 template<int N> struct the_counter;
|
Chris@16
|
30
|
Chris@16
|
31 template<typename T,int N = 5/*for similarity*/>
|
Chris@16
|
32 struct encode_counter
|
Chris@16
|
33 {
|
Chris@16
|
34 __if_exists(the_counter<N + 256>)
|
Chris@16
|
35 {
|
Chris@16
|
36 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count));
|
Chris@16
|
37 }
|
Chris@16
|
38 __if_not_exists(the_counter<N + 256>)
|
Chris@16
|
39 {
|
Chris@16
|
40 __if_exists(the_counter<N + 64>)
|
Chris@16
|
41 {
|
Chris@16
|
42 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count));
|
Chris@16
|
43 }
|
Chris@16
|
44 __if_not_exists(the_counter<N + 64>)
|
Chris@16
|
45 {
|
Chris@16
|
46 __if_exists(the_counter<N + 16>)
|
Chris@16
|
47 {
|
Chris@16
|
48 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
|
Chris@16
|
49 }
|
Chris@16
|
50 __if_not_exists(the_counter<N + 16>)
|
Chris@16
|
51 {
|
Chris@16
|
52 __if_exists(the_counter<N + 4>)
|
Chris@16
|
53 {
|
Chris@16
|
54 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count));
|
Chris@16
|
55 }
|
Chris@16
|
56 __if_not_exists(the_counter<N + 4>)
|
Chris@16
|
57 {
|
Chris@16
|
58 __if_exists(the_counter<N>)
|
Chris@16
|
59 {
|
Chris@16
|
60 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count));
|
Chris@16
|
61 }
|
Chris@16
|
62 __if_not_exists(the_counter<N>)
|
Chris@16
|
63 {
|
Chris@16
|
64 BOOST_STATIC_CONSTANT(unsigned,count=N);
|
Chris@16
|
65 typedef the_counter<N> type;
|
Chris@16
|
66 }
|
Chris@16
|
67 }
|
Chris@16
|
68 }
|
Chris@16
|
69 }
|
Chris@16
|
70 }
|
Chris@16
|
71 };
|
Chris@16
|
72
|
Chris@16
|
73 # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count)
|
Chris@16
|
74 # define BOOST_TYPEOF_NEXT_INDEX(next)
|
Chris@16
|
75 # else
|
Chris@16
|
76 template<int N> struct encode_counter : encode_counter<N - 1> {};
|
Chris@16
|
77 template<> struct encode_counter<0> {};
|
Chris@16
|
78
|
Chris@16
|
79 //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4)
|
Chris@16
|
80 char (*encode_index(...))[5];
|
Chris@16
|
81
|
Chris@16
|
82 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
|
Chris@16
|
83 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
|
Chris@16
|
84 # endif
|
Chris@16
|
85
|
Chris@16
|
86 //Typeof code
|
Chris@16
|
87
|
Chris@16
|
88 # if BOOST_WORKAROUND(BOOST_MSVC,==1300)
|
Chris@16
|
89 template<typename ID>
|
Chris@16
|
90 struct msvc_extract_type
|
Chris@16
|
91 {
|
Chris@16
|
92 template<bool>
|
Chris@16
|
93 struct id2type_impl;
|
Chris@16
|
94
|
Chris@16
|
95 typedef id2type_impl<true> id2type;
|
Chris@16
|
96 };
|
Chris@16
|
97
|
Chris@16
|
98 template<typename T, typename ID>
|
Chris@16
|
99 struct msvc_register_type : msvc_extract_type<ID>
|
Chris@16
|
100 {
|
Chris@16
|
101 template<>
|
Chris@16
|
102 struct id2type_impl<true> //VC7.0 specific bugfeature
|
Chris@16
|
103 {
|
Chris@16
|
104 typedef T type;
|
Chris@16
|
105 };
|
Chris@16
|
106 };
|
Chris@16
|
107 #elif BOOST_WORKAROUND(BOOST_MSVC,>=1400)
|
Chris@16
|
108 struct msvc_extract_type_default_param {};
|
Chris@16
|
109
|
Chris@16
|
110 template<typename ID, typename T = msvc_extract_type_default_param>
|
Chris@16
|
111 struct msvc_extract_type;
|
Chris@16
|
112
|
Chris@16
|
113 template<typename ID>
|
Chris@16
|
114 struct msvc_extract_type<ID, msvc_extract_type_default_param> {
|
Chris@16
|
115 template<bool>
|
Chris@16
|
116 struct id2type_impl;
|
Chris@16
|
117
|
Chris@16
|
118 typedef id2type_impl<true> id2type;
|
Chris@16
|
119 };
|
Chris@16
|
120
|
Chris@16
|
121 template<typename ID, typename T>
|
Chris@16
|
122 struct msvc_extract_type : msvc_extract_type<ID,msvc_extract_type_default_param>
|
Chris@16
|
123 {
|
Chris@16
|
124 template<>
|
Chris@16
|
125 struct id2type_impl<true> //VC8.0 specific bugfeature
|
Chris@16
|
126 {
|
Chris@16
|
127 typedef T type;
|
Chris@16
|
128 };
|
Chris@16
|
129 template<bool>
|
Chris@16
|
130 struct id2type_impl;
|
Chris@16
|
131
|
Chris@16
|
132 typedef id2type_impl<true> id2type;
|
Chris@16
|
133 };
|
Chris@16
|
134
|
Chris@16
|
135 template<typename T, typename ID>
|
Chris@16
|
136 struct msvc_register_type : msvc_extract_type<ID, T>
|
Chris@16
|
137 {
|
Chris@16
|
138 };
|
Chris@16
|
139 # else
|
Chris@16
|
140 template<typename ID>
|
Chris@16
|
141 struct msvc_extract_type
|
Chris@16
|
142 {
|
Chris@16
|
143 struct id2type;
|
Chris@16
|
144 };
|
Chris@16
|
145
|
Chris@16
|
146 template<typename T, typename ID>
|
Chris@16
|
147 struct msvc_register_type : msvc_extract_type<ID>
|
Chris@16
|
148 {
|
Chris@16
|
149 typedef msvc_extract_type<ID> base_type;
|
Chris@16
|
150 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature
|
Chris@16
|
151 {
|
Chris@16
|
152 typedef T type;
|
Chris@16
|
153 };
|
Chris@16
|
154 };
|
Chris@16
|
155 # endif
|
Chris@16
|
156 // EAN: preprocess this block out on advice of Peder Holt
|
Chris@16
|
157 // to eliminate errors in type_traits/common_type.hpp
|
Chris@16
|
158 # if 0 //BOOST_WORKAROUND(BOOST_MSVC,==1310)
|
Chris@16
|
159 template<const std::type_info& ref_type_info>
|
Chris@16
|
160 struct msvc_typeid_wrapper {
|
Chris@16
|
161 typedef typename msvc_extract_type<msvc_typeid_wrapper>::id2type id2type;
|
Chris@16
|
162 typedef typename id2type::type wrapped_type;
|
Chris@16
|
163 typedef typename wrapped_type::type type;
|
Chris@16
|
164 };
|
Chris@16
|
165 //This class is used for registering the type T. encode_type<T> is mapped against typeid(encode_type<T>).
|
Chris@16
|
166 //msvc_typeid_wrapper<typeid(encode_type<T>)> will now have a type typedef that equals encode_type<T>.
|
Chris@16
|
167 template<typename T>
|
Chris@16
|
168 struct encode_type
|
Chris@16
|
169 {
|
Chris@16
|
170 typedef encode_type<T> input_type;
|
Chris@16
|
171 //Invoke registration of encode_type<T>. typeid(encode_type<T>) is now mapped to encode_type<T>. Do not use registered_type for anything.
|
Chris@16
|
172 //The reason for registering encode_type<T> rather than T, is that VC handles typeid(function reference) poorly. By adding another
|
Chris@16
|
173 //level of indirection, we solve this problem.
|
Chris@16
|
174 typedef typename msvc_register_type<input_type,msvc_typeid_wrapper<typeid(input_type)> >::id2type registered_type;
|
Chris@16
|
175 typedef T type;
|
Chris@16
|
176 };
|
Chris@16
|
177
|
Chris@16
|
178 template<typename T> typename disable_if<
|
Chris@16
|
179 typename is_function<T>::type,
|
Chris@16
|
180 typename encode_type<T>::input_type>::type encode_start(T const&);
|
Chris@16
|
181
|
Chris@16
|
182 template<typename T> typename enable_if<
|
Chris@16
|
183 typename is_function<T>::type,
|
Chris@16
|
184 typename encode_type<T>::input_type>::type encode_start(T&);
|
Chris@16
|
185
|
Chris@16
|
186 template<typename Organizer, typename T>
|
Chris@16
|
187 msvc_register_type<T,Organizer> typeof_register_type(const T&);
|
Chris@16
|
188
|
Chris@16
|
189
|
Chris@16
|
190 # define BOOST_TYPEOF(expr) \
|
Chris@16
|
191 boost::type_of::msvc_typeid_wrapper<typeid(boost::type_of::encode_start(expr))>::type
|
Chris@16
|
192
|
Chris@16
|
193 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
|
Chris@16
|
194
|
Chris@16
|
195 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
|
Chris@16
|
196 struct name {\
|
Chris@16
|
197 enum {_typeof_register_value=sizeof(typeid(boost::type_of::typeof_register_type<name>(expr)))};\
|
Chris@16
|
198 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
|
Chris@16
|
199 typedef typename id2type::type type;\
|
Chris@16
|
200 };
|
Chris@16
|
201
|
Chris@16
|
202 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
|
Chris@16
|
203 struct name {\
|
Chris@16
|
204 enum {_typeof_register_value=sizeof(typeid(boost::type_of::typeof_register_type<name>(expr)))};\
|
Chris@16
|
205 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
|
Chris@16
|
206 typedef id2type::type type;\
|
Chris@16
|
207 };
|
Chris@16
|
208
|
Chris@16
|
209 # else
|
Chris@16
|
210 template<int ID>
|
Chris@16
|
211 struct msvc_typeid_wrapper {
|
Chris@16
|
212 typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
|
Chris@16
|
213 typedef typename id2type::type type;
|
Chris@16
|
214 };
|
Chris@16
|
215 //Workaround for ETI-bug for VC6 and VC7
|
Chris@16
|
216 template<>
|
Chris@16
|
217 struct msvc_typeid_wrapper<1> {
|
Chris@16
|
218 typedef msvc_typeid_wrapper<1> type;
|
Chris@16
|
219 };
|
Chris@16
|
220 //Workaround for ETI-bug for VC7.1
|
Chris@16
|
221 template<>
|
Chris@16
|
222 struct msvc_typeid_wrapper<4> {
|
Chris@16
|
223 typedef msvc_typeid_wrapper<4> type;
|
Chris@16
|
224 };
|
Chris@16
|
225
|
Chris@16
|
226 //Tie it all together
|
Chris@16
|
227 template<typename T>
|
Chris@16
|
228 struct encode_type
|
Chris@16
|
229 {
|
Chris@16
|
230 //Get the next available compile time constants index
|
Chris@16
|
231 BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));
|
Chris@16
|
232 //Instantiate the template
|
Chris@16
|
233 typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
|
Chris@16
|
234 //Set the next compile time constants index
|
Chris@16
|
235 BOOST_STATIC_CONSTANT(unsigned,next=value+1);
|
Chris@16
|
236 //Increment the compile time constant (only needed when extensions are not active
|
Chris@16
|
237 BOOST_TYPEOF_NEXT_INDEX(next);
|
Chris@16
|
238 };
|
Chris@16
|
239
|
Chris@16
|
240 template<class T>
|
Chris@16
|
241 struct sizer
|
Chris@16
|
242 {
|
Chris@16
|
243 typedef char(*type)[encode_type<T>::value];
|
Chris@16
|
244 };
|
Chris@16
|
245 # if BOOST_WORKAROUND(BOOST_MSVC,>=1310)
|
Chris@16
|
246 template<typename T> typename disable_if<
|
Chris@16
|
247 typename is_function<T>::type,
|
Chris@16
|
248 typename sizer<T>::type>::type encode_start(T const&);
|
Chris@16
|
249
|
Chris@16
|
250 template<typename T> typename enable_if<
|
Chris@16
|
251 typename is_function<T>::type,
|
Chris@16
|
252 typename sizer<T>::type>::type encode_start(T&);
|
Chris@16
|
253 # else
|
Chris@16
|
254 template<typename T>
|
Chris@16
|
255 typename sizer<T>::type encode_start(T const&);
|
Chris@16
|
256 # endif
|
Chris@16
|
257 template<typename Organizer, typename T>
|
Chris@16
|
258 msvc_register_type<T,Organizer> typeof_register_type(const T&,Organizer* =0);
|
Chris@16
|
259
|
Chris@16
|
260 # define BOOST_TYPEOF(expr) \
|
Chris@16
|
261 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
|
Chris@16
|
262
|
Chris@16
|
263 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
|
Chris@16
|
264
|
Chris@16
|
265 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
|
Chris@16
|
266 struct name {\
|
Chris@16
|
267 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
|
Chris@16
|
268 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
|
Chris@16
|
269 typedef typename id2type::type type;\
|
Chris@16
|
270 };
|
Chris@16
|
271
|
Chris@16
|
272 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
|
Chris@16
|
273 struct name {\
|
Chris@16
|
274 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
|
Chris@16
|
275 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
|
Chris@16
|
276 typedef id2type::type type;\
|
Chris@16
|
277 };
|
Chris@16
|
278
|
Chris@16
|
279 #endif
|
Chris@16
|
280 }
|
Chris@16
|
281 }
|
Chris@16
|
282
|
Chris@16
|
283 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
|