3ce354442f5278610812a80c8bfcea9048359bbc
[senf.git] / boost / typeof / msvc / typeof_impl.hpp
1                                                                                                                                                                                                                                                                
2 // Copyright (C) 2005 Igor Chesnokov, mailto:ichesnokov@gmail.com
3 // Copyright (C) 2005 Peder Holt
4
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
9 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
10
11 # include <boost/config.hpp>
12 # include <boost/detail/workaround.hpp>
13 # include <boost/mpl/int.hpp>
14 # include <boost/type_traits/is_function.hpp>
15 # include <boost/utility/enable_if.hpp>
16
17 namespace boost
18 {
19     namespace type_of
20     {
21
22         //Compile time constant code
23 # if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS)
24         template<int N> struct the_counter;
25         
26         template<typename T,int N = 5/*for similarity*/>
27         struct encode_counter
28         {
29             __if_exists(the_counter<N + 256>)
30             {
31                 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count));
32             }
33             __if_not_exists(the_counter<N + 256>)
34             {
35                 __if_exists(the_counter<N + 64>)
36                 {
37                     BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count));
38                 }
39                 __if_not_exists(the_counter<N + 64>)
40                 {
41                     __if_exists(the_counter<N + 16>)
42                     { 
43                         BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
44                     }
45                     __if_not_exists(the_counter<N + 16>)
46                     {
47                         __if_exists(the_counter<N + 4>)
48                         {
49                             BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count));
50                         }
51                         __if_not_exists(the_counter<N + 4>)
52                         {
53                             __if_exists(the_counter<N>)
54                             {
55                                 BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count));
56                             }
57                             __if_not_exists(the_counter<N>)
58                             {
59                                 BOOST_STATIC_CONSTANT(unsigned,count=N);
60                                 typedef the_counter<N> type;
61                             }
62                         }
63                     }
64                 }
65             }
66         };
67
68 # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count)
69 # define BOOST_TYPEOF_NEXT_INDEX(next)
70 # else
71         template<int N> struct encode_counter : encode_counter<N - 1> {};
72         template<> struct encode_counter<0> {};
73         
74         //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4)
75         char (*encode_index(...))[5];
76
77 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
78 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
79 # endif
80
81         //Typeof code
82
83 # if BOOST_WORKAROUND(BOOST_MSVC,==1300)
84         template<typename ID>
85         struct msvc_extract_type
86         {
87             template<bool>
88             struct id2type_impl;
89
90             typedef id2type_impl<true> id2type;
91         };
92
93         template<typename T, typename ID>
94         struct msvc_register_type : msvc_extract_type<ID>
95         {
96             template<>
97             struct id2type_impl<true>  //VC7.0 specific bugfeature
98             {
99                 typedef T type;
100             };
101         };
102 # else 
103         template<typename ID>
104         struct msvc_extract_type
105         {
106             struct id2type;
107         };
108
109         template<typename T, typename ID>
110         struct msvc_register_type : msvc_extract_type<ID>
111         {
112             typedef msvc_extract_type<ID> base_type;
113             struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature
114             {
115                 typedef T type;
116             };
117         };
118 # endif
119
120         template<int ID>
121         struct msvc_typeid_wrapper {
122             typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
123             typedef typename id2type::type type;
124         };
125         //Workaround for ETI-bug for VC6 and VC7
126         template<>
127         struct msvc_typeid_wrapper<1> {
128             typedef msvc_typeid_wrapper<1> type;
129         };
130         //Workaround for ETI-bug for VC7.1
131         template<>
132         struct msvc_typeid_wrapper<4> {
133             typedef msvc_typeid_wrapper<4> type;
134         };
135
136         //Tie it all together
137         template<typename T>
138         struct encode_type
139         {
140             //Get the next available compile time constants index
141             BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));
142             //Instantiate the template
143             typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
144             //Set the next compile time constants index
145             BOOST_STATIC_CONSTANT(unsigned,next=value+1);
146             //Increment the compile time constant (only needed when extensions are not active
147             BOOST_TYPEOF_NEXT_INDEX(next);     
148         };
149
150         template<class T>
151         struct sizer
152         {
153             typedef char(*type)[encode_type<T>::value];
154         };
155
156 # ifdef BOOST_NO_SFINAE
157
158         template<typename T>
159             typename sizer<T>::type encode_start(T const&);
160
161 # else
162
163         template<typename T> typename disable_if<
164             typename is_function<T>::type, 
165             typename sizer<T>::type>::type encode_start(T const&);
166
167         template<typename T> typename enable_if<
168             typename is_function<T>::type, 
169             typename sizer<T>::type>::type encode_start(T&);
170
171 # endif
172     }
173 }
174
175 # define BOOST_TYPEOF(expr) \
176     boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
177
178 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
179
180 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
181     struct name {\
182         template<typename T>\
183         static boost::type_of::msvc_register_type<T,name> _typeof_register_function(const T&);\
184         BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(_typeof_register_function(expr)));\
185         typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
186         typedef typename id2type::type type;\
187     };
188
189 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
190     struct name {\
191         template<typename T>\
192         static boost::type_of::msvc_register_type<T,name> _typeof_register_function(const T&);\
193         BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(_typeof_register_function(expr)));\
194         typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
195         typedef id2type::type type;\
196     };
197
198 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED