1 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) 5 6 // Use, modification and distribution is subject to the Boost Software 7 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) 8 9 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED 10 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED 11 12 # include <boost/config.hpp> 13 # include <boost/config/workaround.hpp> 14 # include <boost/typeof/constant.hpp> 15 # include <boost/type_traits/enable_if.hpp> 16 # include <boost/type_traits/is_function.hpp> 17 18 # include <typeinfo> 19 20 namespace boost 21 { 22 namespace type_of 23 { 24 25 //Compile time constant code 26 # if defined(_MSC_EXTENSIONS) 27 template<int N> struct the_counter; 28 29 template<typename T,int N = 5/*for similarity*/> 30 struct encode_counter 31 { __if_existsboost::type_of::encode_counter32 __if_exists(the_counter<N + 256>) 33 { 34 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count)); 35 } __if_not_existsboost::type_of::encode_counter36 __if_not_exists(the_counter<N + 256>) 37 { 38 __if_exists(the_counter<N + 64>) 39 { 40 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count)); 41 } 42 __if_not_exists(the_counter<N + 64>) 43 { 44 __if_exists(the_counter<N + 16>) 45 { 46 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count)); 47 } 48 __if_not_exists(the_counter<N + 16>) 49 { 50 __if_exists(the_counter<N + 4>) 51 { 52 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count)); 53 } 54 __if_not_exists(the_counter<N + 4>) 55 { 56 __if_exists(the_counter<N>) 57 { 58 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count)); 59 } 60 __if_not_exists(the_counter<N>) 61 { 62 BOOST_STATIC_CONSTANT(unsigned,count=N); 63 typedef the_counter<N> type; 64 } 65 } 66 } 67 } 68 } 69 }; 70 71 # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count) 72 # define BOOST_TYPEOF_NEXT_INDEX(next) 73 # else 74 template<int N> struct encode_counter : encode_counter<N - 1> {}; 75 template<> struct encode_counter<0> {}; 76 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]; 79 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]; 82 # endif 83 84 //Typeof code 85 86 # if BOOST_WORKAROUND(BOOST_MSVC,>=1400) 87 struct msvc_extract_type_default_param {}; 88 89 template<typename ID, typename T = msvc_extract_type_default_param> 90 struct msvc_extract_type; 91 92 template<typename ID> 93 struct msvc_extract_type<ID, msvc_extract_type_default_param> { 94 template<bool> 95 struct id2type_impl; 96 97 typedef id2type_impl<true> id2type; 98 }; 99 100 template<typename ID, typename T> 101 struct msvc_extract_type : msvc_extract_type<ID,msvc_extract_type_default_param> 102 { 103 template<> 104 struct id2type_impl<true> //VC8.0 specific bugfeature 105 { 106 typedef T type; 107 }; 108 template<bool> 109 struct id2type_impl; 110 111 typedef id2type_impl<true> id2type; 112 }; 113 114 template<typename T, typename ID> 115 struct msvc_register_type : msvc_extract_type<ID, T> 116 { 117 }; 118 # else 119 template<typename ID> 120 struct msvc_extract_type 121 { 122 struct id2type; 123 }; 124 125 template<typename T, typename ID> 126 struct msvc_register_type : msvc_extract_type<ID> 127 { 128 typedef msvc_extract_type<ID> base_type; 129 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature 130 { 131 typedef T type; 132 }; 133 }; 134 # endif 135 136 template<int ID> 137 struct msvc_typeid_wrapper { 138 typedef typename msvc_extract_type<constant<int,ID> >::id2type id2type; 139 typedef typename id2type::type type; 140 }; 141 //Workaround for ETI-bug for VC6 and VC7 142 template<> 143 struct msvc_typeid_wrapper<1> { 144 typedef msvc_typeid_wrapper<1> type; 145 }; 146 //Workaround for ETI-bug for VC7.1 147 template<> 148 struct msvc_typeid_wrapper<4> { 149 typedef msvc_typeid_wrapper<4> type; 150 }; 151 152 //Tie it all together 153 template<typename T> 154 struct encode_type 155 { 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,constant<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); 164 }; 165 166 template<class T> 167 struct sizer 168 { 169 typedef char(*type)[encode_type<T>::value]; 170 }; 171 template<typename T> typename enable_if_< 172 !is_function<T>::value, 173 typename sizer<T>::type>::type encode_start(T const&); 174 175 template<typename T> typename enable_if_< 176 is_function<T>::value, 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); 180 181 # define BOOST_TYPEOF(expr) \ 182 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type 183 184 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr) 185 186 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ 187 struct name {\ 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;\ 191 }; 192 193 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ 194 struct name {\ 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;\ 198 }; 199 200 } 201 } 202 203 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED 204