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/config.hpp>
9 #include <boost/typeof/constant.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 BOOST_STATIC_CONSTANT(std::size_t , value=((n + 1) * 2 + (Overflow ? 1 : 0)));
46 template<std::size_t m>
49 BOOST_STATIC_CONSTANT(std::size_t, value = (m / 2) - 1);
50 BOOST_STATIC_CONSTANT(std::size_t, 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::type_of::constant<std::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::type_of::constant<std::size_t,pack<n % 0x3ffffffe, true>::value> >::type,
66 boost::type_of::constant<std::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>
85 BOOST_STATIC_CONSTANT(std::size_t,value = n);
89 template<std::size_t n, class Iter>
90 struct decode_size_t<n, Iter, true>
92 BOOST_STATIC_CONSTANT(std::size_t,m = Iter::type::value);
94 BOOST_STATIC_CONSTANT(std::size_t,value = (std::size_t)m * 0x3ffffffe + n);
95 typedef typename Iter::next iter;
98 template<class T, class Iter>
99 struct decode_integral
101 typedef decode_integral<T,Iter> self_t;
102 BOOST_STATIC_CONSTANT(std::size_t,m = Iter::type::value);
104 BOOST_STATIC_CONSTANT(std::size_t,n = unpack<m>::value);
106 BOOST_STATIC_CONSTANT(std::size_t,overflow = unpack<m>::overflow);
108 typedef typename Iter::next nextpos;
110 static const T value = (T)(std::size_t)decode_size_t<n, nextpos, overflow>::value;
112 typedef typename decode_size_t<self_t::n, nextpos, self_t::overflow>::iter iter;
117 #endif//BOOST_TYPEOF_INT_ENCODING_HPP_INCLUDED