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>
11 namespace boost { namespace type_of {
13 template<class T> struct get_unsigned
17 template<> struct get_unsigned<signed char>
19 typedef unsigned char type;
21 template<> struct get_unsigned<char>
23 typedef unsigned char type;
25 template<> struct get_unsigned<short>
27 typedef unsigned short type;
29 template<> struct get_unsigned<int>
31 typedef unsigned int type;
33 template<> struct get_unsigned<long>
35 typedef unsigned long type;
38 //////////////////////////
40 template<std::size_t n, bool Overflow>
43 enum {value = (n + 1) * 2 + (Overflow ? 1 : 0)};
46 template<std::size_t m>
49 enum {value = (m / 2) - 1};
50 enum {overflow = (m % 2 == 1)};
53 ////////////////////////////////
55 template<class V, std::size_t n, bool overflow = (n >= 0x3fffffff)>
56 struct encode_size_t : push_back<
58 boost::mpl::size_t<pack<n, false>::value>
62 template<class V, std::size_t n>
63 struct encode_size_t<V, n, true> : push_back<typename push_back<
65 boost::mpl::size_t<pack<n % 0x3ffffffe, true>::value> >::type,
66 boost::mpl::size_t<n / 0x3ffffffe>
70 template<class V, class T, T n>
71 struct encode_integral : encode_size_t< V, (typename get_unsigned<T>::type)n,(((typename get_unsigned<T>::type)n)>=0x3fffffff) >
74 template<class V, bool b>
75 struct encode_integral<V, bool, b> : encode_size_t< V, b?1:0, false>
77 ///////////////////////////
79 template<std::size_t n, class Iter, bool overflow>
82 template<std::size_t n, class Iter>
83 struct decode_size_t<n, Iter, false>
89 template<std::size_t n, class Iter>
90 struct decode_size_t<n, Iter, true>
92 enum {m = Iter::type::value};
94 enum {value = (std::size_t)m * 0x3ffffffe + n};
95 typedef typename Iter::next iter;
98 template<class T, class Iter>
99 struct decode_integral
101 enum {m = Iter::type::value};
103 enum {n = unpack<m>::value};
105 enum {overflow = unpack<m>::overflow};
107 typedef typename Iter::next nextpos;
109 static const T value = (T)(std::size_t)decode_size_t<n, nextpos, overflow>::value;
111 typedef typename decode_size_t<n, nextpos, overflow>::iter iter;
116 #endif//BOOST_TYPEOF_INT_ENCODING_HPP_INCLUDED