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>
25 //Compile time constant code
26 # if defined(_MSC_EXTENSIONS)
27 template<int N> struct the_counter;
29 template<typename T,int N = 5/*for similarity*/>
32 __if_exists(the_counter<N + 256>)
34 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count));
36 __if_not_exists(the_counter<N + 256>)
38 __if_exists(the_counter<N + 64>)
40 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count));
42 __if_not_exists(the_counter<N + 64>)
44 __if_exists(the_counter<N + 16>)
46 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
48 __if_not_exists(the_counter<N + 16>)
50 __if_exists(the_counter<N + 4>)
52 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count));
54 __if_not_exists(the_counter<N + 4>)
56 __if_exists(the_counter<N>)
58 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count));
60 __if_not_exists(the_counter<N>)
62 BOOST_STATIC_CONSTANT(unsigned,count=N);
63 typedef the_counter<N> type;
71 # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count)
72 # define BOOST_TYPEOF_NEXT_INDEX(next)
74 template<int N> struct encode_counter : encode_counter<N - 1> {};
75 template<> struct encode_counter<0> {};
77 //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4)
78 char (*encode_index(...))[5];
80 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
81 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
86 # if BOOST_WORKAROUND(BOOST_MSVC,>=1400)
87 struct msvc_extract_type_default_param {};
89 template<typename ID, typename T = msvc_extract_type_default_param>
90 struct msvc_extract_type;
93 struct msvc_extract_type<ID, msvc_extract_type_default_param> {
97 typedef id2type_impl<true> id2type;
100 template<typename ID, typename T>
101 struct msvc_extract_type : msvc_extract_type<ID,msvc_extract_type_default_param>
104 struct id2type_impl<true> //VC8.0 specific bugfeature
111 typedef id2type_impl<true> id2type;
114 template<typename T, typename ID>
115 struct msvc_register_type : msvc_extract_type<ID, T>
119 template<typename ID>
120 struct msvc_extract_type
125 template<typename T, typename ID>
126 struct msvc_register_type : msvc_extract_type<ID>
128 typedef msvc_extract_type<ID> base_type;
129 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature
137 struct msvc_typeid_wrapper {
138 typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
139 typedef typename id2type::type type;
141 //Workaround for ETI-bug for VC6 and VC7
143 struct msvc_typeid_wrapper<1> {
144 typedef msvc_typeid_wrapper<1> type;
146 //Workaround for ETI-bug for VC7.1
148 struct msvc_typeid_wrapper<4> {
149 typedef msvc_typeid_wrapper<4> type;
152 //Tie it all together
156 //Get the next available compile time constants index
157 BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));
158 //Instantiate the template
159 typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
160 //Set the next compile time constants index
161 BOOST_STATIC_CONSTANT(unsigned,next=value+1);
162 //Increment the compile time constant (only needed when extensions are not active
163 BOOST_TYPEOF_NEXT_INDEX(next);
169 typedef char(*type)[encode_type<T>::value];
171 template<typename T> typename disable_if<
172 typename is_function<T>::type,
173 typename sizer<T>::type>::type encode_start(T const&);
175 template<typename T> typename enable_if<
176 typename is_function<T>::type,
177 typename sizer<T>::type>::type encode_start(T&);
178 template<typename Organizer, typename T>
179 msvc_register_type<T,Organizer> typeof_register_type(const T&,Organizer* =0);
181 # define BOOST_TYPEOF(expr) \
182 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
184 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
186 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
188 enum {_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr))};\
189 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
190 typedef typename id2type::type type;\
193 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
195 enum {_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr))};\
196 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
197 typedef id2type::type type;\
203 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED