blob: 3b40f760a95c6d8ac4459b5b9e8861dd8aec400a [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001// 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 Thompson3d613fd2013-10-15 15:39:04 -07006#ifndef NDNBOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
7# define NDNBOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
Jeff Thompsona28eed82013-08-22 16:21:10 -07008
9# include <ndnboost/config.hpp>
10# include <ndnboost/detail/workaround.hpp>
11# include <ndnboost/mpl/int.hpp>
12
13namespace 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 Thompson3d613fd2013-10-15 15:39:04 -070023# 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 Thompsona28eed82013-08-22 16:21:10 -070025
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 Thompson3d613fd2013-10-15 15:39:04 -070057 NDNBOOST_STATIC_CONSTANT(unsigned,value=NDNBOOST_TYPEOF_INDEX(T));
Jeff Thompsona28eed82013-08-22 16:21:10 -070058 //Instantiate the template
59 typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
60 //Set the next compile time constants index
Jeff Thompson3d613fd2013-10-15 15:39:04 -070061 NDNBOOST_STATIC_CONSTANT(unsigned,next=value+1);
Jeff Thompsona28eed82013-08-22 16:21:10 -070062 //Increment the compile time constant (only needed when extensions are not active
Jeff Thompson3d613fd2013-10-15 15:39:04 -070063 NDNBOOST_TYPEOF_NEXT_INDEX(next);
Jeff Thompsona28eed82013-08-22 16:21:10 -070064 };
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 Thompson3d613fd2013-10-15 15:39:04 -070078# define NDNBOOST_TYPEOF(expr) \
Jeff Thompsona28eed82013-08-22 16:21:10 -070079 ndnboost::type_of::msvc_typeid_wrapper<sizeof(*ndnboost::type_of::encode_start(expr))>::type
80
Jeff Thompson3d613fd2013-10-15 15:39:04 -070081# define NDNBOOST_TYPEOF_TPL(expr) typename NDNBOOST_TYPEOF(expr)
Jeff Thompsona28eed82013-08-22 16:21:10 -070082
Jeff Thompson3d613fd2013-10-15 15:39:04 -070083# define NDNBOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
Jeff Thompsona28eed82013-08-22 16:21:10 -070084 struct name {\
Jeff Thompson3d613fd2013-10-15 15:39:04 -070085 NDNBOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(ndnboost::type_of::typeof_register_type<name>(expr)));\
Jeff Thompsona28eed82013-08-22 16:21:10 -070086 typedef typename ndnboost::type_of::msvc_extract_type<name>::id2type id2type;\
87 typedef typename id2type::type type;\
88 };
89
Jeff Thompson3d613fd2013-10-15 15:39:04 -070090# define NDNBOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
Jeff Thompsona28eed82013-08-22 16:21:10 -070091 struct name {\
Jeff Thompson3d613fd2013-10-15 15:39:04 -070092 NDNBOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(ndnboost::type_of::typeof_register_type<name>(expr)));\
Jeff Thompsona28eed82013-08-22 16:21:10 -070093 typedef ndnboost::type_of::msvc_extract_type<name>::id2type id2type;\
94 typedef id2type::type type;\
95 };
96
97 }
98}
99
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700100#endif//NDNBOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED