native typeof implementation for VC7.1 and VC8.0 now uses typeid() instead of sizeof() to map a type. This bypasses some bugs in Microsofts sizeof implementation, and removes a limitation on the number of typeof invocations that can be done in a single compilation unit

[SVN r38801]
This commit is contained in:
Peder Holt 2007-08-20 18:09:14 +00:00
parent 17d05e6396
commit 24c7fe2f65

View File

@ -1,6 +1,6 @@
// Copyright (C) 2005 Igor Chesnokov, mailto:ichesnokov@gmail.com (VC 6.5,VC 7.1 + counter code)
// Copyright (C) 2005 Peder Holt (VC 7.0 + framework)
// 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
@ -15,6 +15,10 @@
# include <boost/type_traits/is_function.hpp>
# include <boost/utility/enable_if.hpp>
# if BOOST_WORKAROUND(BOOST_MSVC,>=1310)
# include <typeinfo>
# endif
namespace boost
{
namespace type_of
@ -23,7 +27,7 @@ namespace boost
//Compile time constant code
# if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS)
template<int N> struct the_counter;
template<typename T,int N = 5/*for similarity*/>
struct encode_counter
{
@ -40,7 +44,7 @@ namespace boost
__if_not_exists(the_counter<N + 64>)
{
__if_exists(the_counter<N + 16>)
{
{
BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
}
__if_not_exists(the_counter<N + 16>)
@ -71,7 +75,7 @@ namespace boost
# else
template<int N> struct encode_counter : encode_counter<N - 1> {};
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];
@ -132,7 +136,7 @@ namespace boost
struct msvc_register_type : msvc_extract_type<ID, T>
{
};
# else
# else
template<typename ID>
struct msvc_extract_type
{
@ -149,7 +153,57 @@ namespace boost
};
};
# endif
# if BOOST_WORKAROUND(BOOST_MSVC,>=1310)
template<const std::type_info& ref_type_info>
struct msvc_typeid_wrapper {
typedef typename msvc_extract_type<msvc_typeid_wrapper>::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<T> is mapped against typeid(encode_type<T>).
//msvc_typeid_wrapper<typeid(encode_type<T>)> will now have a type typedef that equals encode_type<T>.
template<typename T>
struct encode_type
{
typedef encode_type<T> input_type;
//Invoke registration of encode_type<T>. typeid(encode_type<T>) is now mapped to encode_type<T>. Do not use registered_type for anything.
//The reason for registering encode_type<T> 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<input_type,msvc_typeid_wrapper<typeid(input_type)> >::id2type registered_type;
typedef T type;
};
template<typename T> typename disable_if<
typename is_function<T>::type,
typename encode_type<T>::input_type>::type encode_start(T const&);
template<typename T> typename enable_if<
typename is_function<T>::type,
typename encode_type<T>::input_type>::type encode_start(T&);
template<typename Organizer, typename T>
msvc_register_type<T,Organizer> typeof_register_type(const T&);
# define BOOST_TYPEOF(expr) \
boost::type_of::msvc_typeid_wrapper<typeid(boost::type_of::encode_start(expr))>::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<name>(expr)))};\
typedef typename boost::type_of::msvc_extract_type<name>::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<name>(expr)))};\
typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
typedef id2type::type type;\
};
# else
template<int ID>
struct msvc_typeid_wrapper {
typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
@ -177,7 +231,7 @@ namespace boost
//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);
BOOST_TYPEOF_NEXT_INDEX(next);
};
template<class T>
@ -186,24 +240,11 @@ namespace boost
typedef char(*type)[encode_type<T>::value];
};
# ifdef BOOST_NO_SFINAE
template<typename T>
typename sizer<T>::type encode_start(T const&);
# else
template<typename T> typename disable_if<
typename is_function<T>::type,
typename sizer<T>::type>::type encode_start(T const&);
template<typename T> typename enable_if<
typename is_function<T>::type,
typename sizer<T>::type>::type encode_start(T&);
# endif
}
}
template<typename Organizer, typename T>
msvc_register_type<T,Organizer> typeof_register_type(const T&,Organizer* =0);
# define BOOST_TYPEOF(expr) \
boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
@ -212,20 +253,20 @@ namespace boost
# define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
struct name {\
template<typename T>\
static boost::type_of::msvc_register_type<T,name> _typeof_register_function(const T&);\
BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(_typeof_register_function(expr)));\
BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
typedef typename id2type::type type;\
};
# define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
struct name {\
template<typename T>\
static boost::type_of::msvc_register_type<T,name> _typeof_register_function(const T&);\
BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(_typeof_register_function(expr)));\
BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
typedef id2type::type type;\
};
#endif
}
}
#endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED