2 // Copyright (C) 2005 Igor Chesnokov, mailto:ichesnokov@gmail.com
3 // Copyright (C) 2005 Peder Holt
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
9 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
11 # include <boost/config.hpp>
12 # include <boost/detail/workaround.hpp>
13 # include <boost/mpl/int.hpp>
14 # include <boost/type_traits/is_function.hpp>
15 # include <boost/utility/enable_if.hpp>
22 //Compile time constant code
23 # if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS)
24 template<int N> struct the_counter;
26 template<typename T,int N = 5/*for similarity*/>
29 __if_exists(the_counter<N + 256>)
31 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count));
33 __if_not_exists(the_counter<N + 256>)
35 __if_exists(the_counter<N + 64>)
37 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count));
39 __if_not_exists(the_counter<N + 64>)
41 __if_exists(the_counter<N + 16>)
43 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
45 __if_not_exists(the_counter<N + 16>)
47 __if_exists(the_counter<N + 4>)
49 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count));
51 __if_not_exists(the_counter<N + 4>)
53 __if_exists(the_counter<N>)
55 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count));
57 __if_not_exists(the_counter<N>)
59 BOOST_STATIC_CONSTANT(unsigned,count=N);
60 typedef the_counter<N> type;
68 # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count)
69 # define BOOST_TYPEOF_NEXT_INDEX(next)
71 template<int N> struct encode_counter : encode_counter<N - 1> {};
72 template<> struct encode_counter<0> {};
74 //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4)
75 char (*encode_index(...))[5];
77 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
78 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
83 # if BOOST_WORKAROUND(BOOST_MSVC,==1300)
85 struct msvc_extract_type
90 typedef id2type_impl<true> id2type;
93 template<typename T, typename ID>
94 struct msvc_register_type : msvc_extract_type<ID>
97 struct id2type_impl<true> //VC7.0 specific bugfeature
103 template<typename ID>
104 struct msvc_extract_type
109 template<typename T, typename ID>
110 struct msvc_register_type : msvc_extract_type<ID>
112 typedef msvc_extract_type<ID> base_type;
113 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature
121 struct msvc_typeid_wrapper {
122 typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
123 typedef typename id2type::type type;
125 //Workaround for ETI-bug for VC6 and VC7
127 struct msvc_typeid_wrapper<1> {
128 typedef msvc_typeid_wrapper<1> type;
130 //Workaround for ETI-bug for VC7.1
132 struct msvc_typeid_wrapper<4> {
133 typedef msvc_typeid_wrapper<4> type;
136 //Tie it all together
140 //Get the next available compile time constants index
141 BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));
142 //Instantiate the template
143 typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
144 //Set the next compile time constants index
145 BOOST_STATIC_CONSTANT(unsigned,next=value+1);
146 //Increment the compile time constant (only needed when extensions are not active
147 BOOST_TYPEOF_NEXT_INDEX(next);
153 typedef char(*type)[encode_type<T>::value];
156 # ifdef BOOST_NO_SFINAE
159 typename sizer<T>::type encode_start(T const&);
163 template<typename T> typename disable_if<
164 typename is_function<T>::type,
165 typename sizer<T>::type>::type encode_start(T const&);
167 template<typename T> typename enable_if<
168 typename is_function<T>::type,
169 typename sizer<T>::type>::type encode_start(T&);
175 # define BOOST_TYPEOF(expr) \
176 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
178 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
180 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
182 template<typename T>\
183 static boost::type_of::msvc_register_type<T,name> _typeof_register_function(const T&);\
184 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(_typeof_register_function(expr)));\
185 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
186 typedef typename id2type::type type;\
189 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
191 template<typename T>\
192 static boost::type_of::msvc_register_type<T,name> _typeof_register_function(const T&);\
193 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(_typeof_register_function(expr)));\
194 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
195 typedef id2type::type type;\
198 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED