1 // Copyright (C) 2004 Arkadiy Vertleyb
2 // Use, modification and distribution is subject to the Boost Software
3 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_TYPEOF_INT_ENCODING_HPP_INCLUDED
6 #define BOOST_TYPEOF_INT_ENCODING_HPP_INCLUDED
8 #include <boost/mpl/if.hpp>
9 #include <boost/mpl/size_t.hpp>
10 #include <boost/config.hpp>
12 namespace boost
{ namespace type_of
{
14 template<class T
> struct get_unsigned
18 template<> struct get_unsigned
<signed char>
20 typedef unsigned char type
;
22 template<> struct get_unsigned
<char>
24 typedef unsigned char type
;
26 template<> struct get_unsigned
<short>
28 typedef unsigned short type
;
30 template<> struct get_unsigned
<int>
32 typedef unsigned int type
;
34 template<> struct get_unsigned
<long>
36 typedef unsigned long type
;
39 //////////////////////////
41 template<std::size_t n
, bool Overflow
>
44 BOOST_STATIC_CONSTANT(std::size_t , value
=((n
+ 1) * 2 + (Overflow
? 1 : 0)));
47 template<std::size_t m
>
50 BOOST_STATIC_CONSTANT(std::size_t, value
= (m
/ 2) - 1);
51 BOOST_STATIC_CONSTANT(std::size_t, overflow
= (m
% 2 == 1));
54 ////////////////////////////////
56 template<class V
, std::size_t n
, bool overflow
= (n
>= 0x3fffffff)>
57 struct encode_size_t
: push_back
<
59 boost::mpl::size_t<pack
<n
, false>::value
>
63 template<class V
, std::size_t n
>
64 struct encode_size_t
<V
, n
, true> : push_back
<typename push_back
<
66 boost::mpl::size_t<pack
<n
% 0x3ffffffe, true>::value
> >::type
,
67 boost::mpl::size_t<n
/ 0x3ffffffe>
71 template<class V
, class T
, T n
>
72 struct encode_integral
: encode_size_t
< V
, (typename get_unsigned
<T
>::type
)n
,(((typename get_unsigned
<T
>::type
)n
)>=0x3fffffff) >
75 template<class V
, bool b
>
76 struct encode_integral
<V
, bool, b
> : encode_size_t
< V
, b
?1:0, false>
78 ///////////////////////////
80 template<std::size_t n
, class Iter
, bool overflow
>
83 template<std::size_t n
, class Iter
>
84 struct decode_size_t
<n
, Iter
, false>
86 BOOST_STATIC_CONSTANT(std::size_t,value
= n
);
90 template<std::size_t n
, class Iter
>
91 struct decode_size_t
<n
, Iter
, true>
93 BOOST_STATIC_CONSTANT(std::size_t,m
= Iter::type::value
);
95 BOOST_STATIC_CONSTANT(std::size_t,value
= (std::size_t)m
* 0x3ffffffe + n
);
96 typedef typename
Iter::next iter
;
99 template<class T
, class Iter
>
100 struct decode_integral
102 typedef decode_integral
<T
,Iter
> self_t
;
103 BOOST_STATIC_CONSTANT(std::size_t,m
= Iter::type::value
);
105 BOOST_STATIC_CONSTANT(std::size_t,n
= unpack
<m
>::value
);
107 BOOST_STATIC_CONSTANT(std::size_t,overflow
= unpack
<m
>::overflow
);
109 typedef typename
Iter::next nextpos
;
111 static const T value
= (T
)(std::size_t)decode_size_t
<n
, nextpos
, overflow
>::value
;
113 typedef typename decode_size_t
<self_t::n
, nextpos
, self_t::overflow
>::iter iter
;
118 #endif//BOOST_TYPEOF_INT_ENCODING_HPP_INCLUDED