// Copyright (C) 2005 Igor Chesnokov, mailto:ichesnokov@gmail.com (VC 6.5,VC 7.1 + counter code) // Copyright (C) 2005-2007 Peder Holt (VC 7.0 + framework) // Copyright (C) 2006 Steven Watanabe (VC 8.0) // Use, modification and distribution is subject to the Boost Software // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED # include # include # include # include # include # if BOOST_WORKAROUND(BOOST_MSVC,>=1310) # include # endif namespace boost { namespace type_of { //Compile time constant code # if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS) template struct the_counter; template struct encode_counter { __if_exists(the_counter) { BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter::count)); } __if_not_exists(the_counter) { __if_exists(the_counter) { BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter::count)); } __if_not_exists(the_counter) { __if_exists(the_counter) { BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter::count)); } __if_not_exists(the_counter) { __if_exists(the_counter) { BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter::count)); } __if_not_exists(the_counter) { __if_exists(the_counter) { BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter::count)); } __if_not_exists(the_counter) { BOOST_STATIC_CONSTANT(unsigned,count=N); typedef the_counter type; } } } } } }; # define BOOST_TYPEOF_INDEX(T) (encode_counter::count) # define BOOST_TYPEOF_NEXT_INDEX(next) # else template struct encode_counter : encode_counter {}; template<> struct encode_counter<0> {}; //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4) char (*encode_index(...))[5]; # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0))) # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter*))[next]; # endif //Typeof code # if BOOST_WORKAROUND(BOOST_MSVC,==1300) template struct msvc_extract_type { template struct id2type_impl; typedef id2type_impl id2type; }; template struct msvc_register_type : msvc_extract_type { template<> struct id2type_impl //VC7.0 specific bugfeature { typedef T type; }; }; #elif BOOST_WORKAROUND(BOOST_MSVC,>=1400) struct msvc_extract_type_default_param {}; template struct msvc_extract_type; template struct msvc_extract_type { template struct id2type_impl; typedef id2type_impl id2type; }; template struct msvc_extract_type : msvc_extract_type { template<> struct id2type_impl //VC8.0 specific bugfeature { typedef T type; }; template struct id2type_impl; typedef id2type_impl id2type; }; template struct msvc_register_type : msvc_extract_type { }; # else template struct msvc_extract_type { struct id2type; }; template struct msvc_register_type : msvc_extract_type { typedef msvc_extract_type base_type; struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature { typedef T type; }; }; # endif # if BOOST_WORKAROUND(BOOST_MSVC,==1310) template struct msvc_typeid_wrapper { typedef typename msvc_extract_type::id2type id2type; typedef typename id2type::type wrapped_type; typedef typename wrapped_type::type type; }; //This class is used for registering the type T. encode_type is mapped against typeid(encode_type). //msvc_typeid_wrapper)> will now have a type typedef that equals encode_type. template struct encode_type { typedef encode_type input_type; //Invoke registration of encode_type. typeid(encode_type) is now mapped to encode_type. Do not use registered_type for anything. //The reason for registering encode_type rather than T, is that VC handles typeid(function reference) poorly. By adding another //level of indirection, we solve this problem. typedef typename msvc_register_type >::id2type registered_type; typedef T type; }; template typename disable_if< typename is_function::type, typename encode_type::input_type>::type encode_start(T const&); template typename enable_if< typename is_function::type, typename encode_type::input_type>::type encode_start(T&); template msvc_register_type typeof_register_type(const T&); # define BOOST_TYPEOF(expr) \ boost::type_of::msvc_typeid_wrapper::type # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr) # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ struct name {\ enum {_typeof_register_value=sizeof(typeid(boost::type_of::typeof_register_type(expr)))};\ typedef typename boost::type_of::msvc_extract_type::id2type id2type;\ typedef typename id2type::type type;\ }; # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ struct name {\ enum {_typeof_register_value=sizeof(typeid(boost::type_of::typeof_register_type(expr)))};\ typedef boost::type_of::msvc_extract_type::id2type id2type;\ typedef id2type::type type;\ }; # else template struct msvc_typeid_wrapper { typedef typename msvc_extract_type >::id2type id2type; typedef typename id2type::type type; }; //Workaround for ETI-bug for VC6 and VC7 template<> struct msvc_typeid_wrapper<1> { typedef msvc_typeid_wrapper<1> type; }; //Workaround for ETI-bug for VC7.1 template<> struct msvc_typeid_wrapper<4> { typedef msvc_typeid_wrapper<4> type; }; //Tie it all together template struct encode_type { //Get the next available compile time constants index BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T)); //Instantiate the template typedef typename msvc_register_type >::id2type type; //Set the next compile time constants index BOOST_STATIC_CONSTANT(unsigned,next=value+1); //Increment the compile time constant (only needed when extensions are not active BOOST_TYPEOF_NEXT_INDEX(next); }; template struct sizer { typedef char(*type)[encode_type::value]; }; # if BOOST_WORKAROUND(BOOST_MSVC,>=1310) template typename disable_if< typename is_function::type, typename sizer::type>::type encode_start(T const&); template typename enable_if< typename is_function::type, typename sizer::type>::type encode_start(T&); # else template typename sizer::type encode_start(T const&); # endif template msvc_register_type typeof_register_type(const T&,Organizer* =0); # define BOOST_TYPEOF(expr) \ boost::type_of::msvc_typeid_wrapper::type # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr) # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ struct name {\ BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type(expr)));\ typedef typename boost::type_of::msvc_extract_type::id2type id2type;\ typedef typename id2type::type type;\ }; # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ struct name {\ BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type(expr)));\ typedef boost::type_of::msvc_extract_type::id2type id2type;\ typedef id2type::type type;\ }; #endif } } #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED