Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 6 | #ifndef NDNBOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED |
| 7 | # define NDNBOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 8 | |
| 9 | # include <ndnboost/config.hpp> |
| 10 | # include <ndnboost/detail/workaround.hpp> |
| 11 | # include <ndnboost/mpl/int.hpp> |
| 12 | |
| 13 | namespace ndnboost |
| 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 23 | # define NDNBOOST_TYPEOF_INDEX(T) (sizeof(*ndnboost::type_of::encode_index((ndnboost::type_of::encode_counter<1000>*)0))) |
| 24 | # define NDNBOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next]; |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 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<mpl::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 |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 57 | NDNBOOST_STATIC_CONSTANT(unsigned,value=NDNBOOST_TYPEOF_INDEX(T)); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 58 | //Instantiate the template |
| 59 | typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type; |
| 60 | //Set the next compile time constants index |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 61 | NDNBOOST_STATIC_CONSTANT(unsigned,next=value+1); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 62 | //Increment the compile time constant (only needed when extensions are not active |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 63 | NDNBOOST_TYPEOF_NEXT_INDEX(next); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 78 | # define NDNBOOST_TYPEOF(expr) \ |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 79 | ndnboost::type_of::msvc_typeid_wrapper<sizeof(*ndnboost::type_of::encode_start(expr))>::type |
| 80 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 81 | # define NDNBOOST_TYPEOF_TPL(expr) typename NDNBOOST_TYPEOF(expr) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 83 | # define NDNBOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \ |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 84 | struct name {\ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 85 | NDNBOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(ndnboost::type_of::typeof_register_type<name>(expr)));\ |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 86 | typedef typename ndnboost::type_of::msvc_extract_type<name>::id2type id2type;\ |
| 87 | typedef typename id2type::type type;\ |
| 88 | }; |
| 89 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 90 | # define NDNBOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \ |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 91 | struct name {\ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 92 | NDNBOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(ndnboost::type_of::typeof_register_type<name>(expr)));\ |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 93 | typedef ndnboost::type_of::msvc_extract_type<name>::id2type id2type;\ |
| 94 | typedef id2type::type type;\ |
| 95 | }; |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 100 | #endif//NDNBOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED |