2 // Copyright (C) 2005 Igor Chesnokov, mailto:ichesnokov@gmail.com (VC 6.5,VC 7.1 + counter code)
3 // Copyright (C) 2005-2007 Peder Holt (VC 7.0 + framework)
4 // Copyright (C) 2006 Steven Watanabe (VC 8.0)
6 // Use, modification and distribution is subject to the Boost Software
7 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
10 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
12 # include <boost/config.hpp>
13 # include <boost/detail/workaround.hpp>
14 # include <boost/mpl/int.hpp>
15 # include <boost/type_traits/is_function.hpp>
16 # include <boost/utility/enable_if.hpp>
18 # if BOOST_WORKAROUND(BOOST_MSVC,>=1310)
27 //Compile time constant code
28 # if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS)
29 template<int N> struct the_counter;
31 template<typename T,int N = 5/*for similarity*/>
34 __if_exists(the_counter<N + 256>)
36 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count));
38 __if_not_exists(the_counter<N + 256>)
40 __if_exists(the_counter<N + 64>)
42 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count));
44 __if_not_exists(the_counter<N + 64>)
46 __if_exists(the_counter<N + 16>)
48 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
50 __if_not_exists(the_counter<N + 16>)
52 __if_exists(the_counter<N + 4>)
54 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count));
56 __if_not_exists(the_counter<N + 4>)
58 __if_exists(the_counter<N>)
60 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count));
62 __if_not_exists(the_counter<N>)
64 BOOST_STATIC_CONSTANT(unsigned,count=N);
65 typedef the_counter<N> type;
73 # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count)
74 # define BOOST_TYPEOF_NEXT_INDEX(next)
76 template<int N> struct encode_counter : encode_counter<N - 1> {};
77 template<> struct encode_counter<0> {};
79 //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4)
80 char (*encode_index(...))[5];
82 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
83 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
88 # if BOOST_WORKAROUND(BOOST_MSVC,==1300)
90 struct msvc_extract_type
95 typedef id2type_impl<true> id2type;
98 template<typename T, typename ID>
99 struct msvc_register_type : msvc_extract_type<ID>
102 struct id2type_impl<true> //VC7.0 specific bugfeature
107 #elif BOOST_WORKAROUND(BOOST_MSVC,>=1400)
108 struct msvc_extract_type_default_param {};
110 template<typename ID, typename T = msvc_extract_type_default_param>
111 struct msvc_extract_type;
113 template<typename ID>
114 struct msvc_extract_type<ID, msvc_extract_type_default_param> {
118 typedef id2type_impl<true> id2type;
121 template<typename ID, typename T>
122 struct msvc_extract_type : msvc_extract_type<ID,msvc_extract_type_default_param>
125 struct id2type_impl<true> //VC8.0 specific bugfeature
132 typedef id2type_impl<true> id2type;
135 template<typename T, typename ID>
136 struct msvc_register_type : msvc_extract_type<ID, T>
140 template<typename ID>
141 struct msvc_extract_type
146 template<typename T, typename ID>
147 struct msvc_register_type : msvc_extract_type<ID>
149 typedef msvc_extract_type<ID> base_type;
150 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature
156 // EAN: preprocess this block out on advice of Peder Holt
157 // to eliminate errors in type_traits/common_type.hpp
158 # if 0 //BOOST_WORKAROUND(BOOST_MSVC,==1310)
159 template<const std::type_info& ref_type_info>
160 struct msvc_typeid_wrapper {
161 typedef typename msvc_extract_type<msvc_typeid_wrapper>::id2type id2type;
162 typedef typename id2type::type wrapped_type;
163 typedef typename wrapped_type::type type;
165 //This class is used for registering the type T. encode_type<T> is mapped against typeid(encode_type<T>).
166 //msvc_typeid_wrapper<typeid(encode_type<T>)> will now have a type typedef that equals encode_type<T>.
170 typedef encode_type<T> input_type;
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.
172 //The reason for registering encode_type<T> rather than T, is that VC handles typeid(function reference) poorly. By adding another
173 //level of indirection, we solve this problem.
174 typedef typename msvc_register_type<input_type,msvc_typeid_wrapper<typeid(input_type)> >::id2type registered_type;
178 template<typename T> typename disable_if<
179 typename is_function<T>::type,
180 typename encode_type<T>::input_type>::type encode_start(T const&);
182 template<typename T> typename enable_if<
183 typename is_function<T>::type,
184 typename encode_type<T>::input_type>::type encode_start(T&);
186 template<typename Organizer, typename T>
187 msvc_register_type<T,Organizer> typeof_register_type(const T&);
190 # define BOOST_TYPEOF(expr) \
191 boost::type_of::msvc_typeid_wrapper<typeid(boost::type_of::encode_start(expr))>::type
193 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
195 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
197 enum {_typeof_register_value=sizeof(typeid(boost::type_of::typeof_register_type<name>(expr)))};\
198 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
199 typedef typename id2type::type type;\
202 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
204 enum {_typeof_register_value=sizeof(typeid(boost::type_of::typeof_register_type<name>(expr)))};\
205 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
206 typedef id2type::type type;\
211 struct msvc_typeid_wrapper {
212 typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
213 typedef typename id2type::type type;
215 //Workaround for ETI-bug for VC6 and VC7
217 struct msvc_typeid_wrapper<1> {
218 typedef msvc_typeid_wrapper<1> type;
220 //Workaround for ETI-bug for VC7.1
222 struct msvc_typeid_wrapper<4> {
223 typedef msvc_typeid_wrapper<4> type;
226 //Tie it all together
230 //Get the next available compile time constants index
231 BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));
232 //Instantiate the template
233 typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
234 //Set the next compile time constants index
235 BOOST_STATIC_CONSTANT(unsigned,next=value+1);
236 //Increment the compile time constant (only needed when extensions are not active
237 BOOST_TYPEOF_NEXT_INDEX(next);
243 typedef char(*type)[encode_type<T>::value];
245 # if BOOST_WORKAROUND(BOOST_MSVC,>=1310)
246 template<typename T> typename disable_if<
247 typename is_function<T>::type,
248 typename sizer<T>::type>::type encode_start(T const&);
250 template<typename T> typename enable_if<
251 typename is_function<T>::type,
252 typename sizer<T>::type>::type encode_start(T&);
255 typename sizer<T>::type encode_start(T const&);
257 template<typename Organizer, typename T>
258 msvc_register_type<T,Organizer> typeof_register_type(const T&,Organizer* =0);
260 # define BOOST_TYPEOF(expr) \
261 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
263 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
265 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
267 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
268 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
269 typedef typename id2type::type type;\
272 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
274 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
275 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
276 typedef id2type::type type;\
283 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED