typeof_impl.hpp 3.3 KB

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