1 // Copyright (C) 2007 Peder Holt 2 3 // Use, modification and distribution is subject to the Boost Software 4 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) 5 6 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED 7 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED 8 9 # include <boost/config.hpp> 10 # include <boost/config/workaround.hpp> 11 # include <boost/typeof/constant.hpp> 12 13 namespace boost 14 { 15 namespace type_of 16 { 17 18 template<int N> struct encode_counter : encode_counter<N - 1> {}; 19 template<> struct encode_counter<0> {}; 20 21 char (*encode_index(...))[1]; 22 23 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1000>*)0))) 24 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next]; 25 26 27 //Typeof code 28 29 template<typename ID> 30 struct msvc_extract_type 31 { 32 struct id2type; 33 }; 34 35 template<typename T, typename ID> 36 struct msvc_register_type : msvc_extract_type<ID> 37 { 38 typedef msvc_extract_type<ID> base_type; 39 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature, also works for Digital Mars 40 { 41 typedef T type; 42 }; 43 }; 44 45 46 template<int ID> 47 struct msvc_typeid_wrapper { 48 typedef typename msvc_extract_type<constant<int,ID> >::id2type id2type; 49 typedef typename id2type::type type; 50 }; 51 52 //Tie it all together 53 template<typename T> 54 struct encode_type 55 { 56 //Get the next available compile time constants index 57 BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T)); 58 //Instantiate the template 59 typedef typename msvc_register_type<T,constant<int,value> >::id2type type; 60 //Set the next compile time constants index 61 BOOST_STATIC_CONSTANT(unsigned,next=value+1); 62 //Increment the compile time constant (only needed when extensions are not active 63 BOOST_TYPEOF_NEXT_INDEX(next); 64 }; 65 66 template<class T> 67 struct sizer 68 { 69 typedef char(*type)[encode_type<T>::value]; 70 }; 71 72 template<typename T> 73 typename sizer<T>::type encode_start(T const&); 74 75 template<typename Organizer, typename T> 76 msvc_register_type<T,Organizer> typeof_register_type(const T&,Organizer* =0); 77 78 # define BOOST_TYPEOF(expr) \ 79 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type 80 81 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr) 82 83 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ 84 struct name {\ 85 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\ 86 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\ 87 typedef typename id2type::type type;\ 88 }; 89 90 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ 91 struct name {\ 92 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\ 93 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\ 94 typedef id2type::type type;\ 95 }; 96 97 } 98 } 99 100 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED 101