]> git.lyx.org Git - lyx.git/blob - boost/boost/integer.hpp
Make the default format translatable, and load the cite formats in
[lyx.git] / boost / boost / integer.hpp
1 //  boost integer.hpp header file  -------------------------------------------//
2
3 //  Copyright Beman Dawes and Daryle Walker 1999.  Distributed under the Boost
4 //  Software License, Version 1.0. (See accompanying file
5 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 //  See http://www.boost.org/libs/integer for documentation.
8
9 //  Revision History
10 //   22 Sep 01  Added value-based integer templates. (Daryle Walker)
11 //   01 Apr 01  Modified to use new <boost/limits.hpp> header. (John Maddock)
12 //   30 Jul 00  Add typename syntax fix (Jens Maurer)
13 //   28 Aug 99  Initial version
14
15 #ifndef BOOST_INTEGER_HPP
16 #define BOOST_INTEGER_HPP
17
18 #include <boost/integer_fwd.hpp>  // self include
19
20 #include <boost/integer_traits.hpp>  // for boost::::boost::integer_traits
21 #include <boost/limits.hpp>          // for ::std::numeric_limits
22 #include <boost/cstdint.hpp>         // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T
23
24 //
25 // We simply cannot include this header on gcc without getting copious warnings of the kind:
26 //
27 // boost/integer.hpp:77:30: warning: use of C99 long long integer constant
28 //
29 // And yet there is no other reasonable implementation, so we declare this a system header
30 // to suppress these warnings.
31 //
32 #if defined(__GNUC__) && (__GNUC__ >= 4)
33 #pragma GCC system_header
34 #endif
35
36 namespace boost
37 {
38
39   //  Helper templates  ------------------------------------------------------//
40
41   //  fast integers from least integers
42   //  int_fast_t<> works correctly for unsigned too, in spite of the name.
43   template< typename LeastInt >
44   struct int_fast_t 
45   { 
46      typedef LeastInt fast; 
47      typedef fast     type;
48   }; // imps may specialize
49
50   namespace detail{
51
52   //  convert category to type 
53   template< int Category > struct int_least_helper {}; // default is empty
54
55   //  specializatons: 1=long, 2=int, 3=short, 4=signed char,
56   //     6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char
57   //  no specializations for 0 and 5: requests for a type > long are in error
58 #ifdef BOOST_HAS_LONG_LONG
59   template<> struct int_least_helper<1> { typedef boost::long_long_type least; };
60 #endif
61   template<> struct int_least_helper<2> { typedef long least; };
62   template<> struct int_least_helper<3> { typedef int least; };
63   template<> struct int_least_helper<4> { typedef short least; };
64   template<> struct int_least_helper<5> { typedef signed char least; };
65 #ifdef BOOST_HAS_LONG_LONG
66   template<> struct int_least_helper<6> { typedef boost::ulong_long_type least; };
67 #endif
68   template<> struct int_least_helper<7> { typedef unsigned long least; };
69   template<> struct int_least_helper<8> { typedef unsigned int least; };
70   template<> struct int_least_helper<9> { typedef unsigned short least; };
71   template<> struct int_least_helper<10> { typedef unsigned char least; };
72
73   template <int Bits>
74   struct exact_signed_base_helper{};
75   template <int Bits>
76   struct exact_unsigned_base_helper{};
77
78   template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
79   template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
80 #if USHRT_MAX != UCHAR_MAX
81   template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
82   template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
83 #endif
84 #if UINT_MAX != USHRT_MAX
85   template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
86   template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
87 #endif
88 #if ULONG_MAX != UINT_MAX
89   template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
90   template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
91 #endif
92 #if defined(BOOST_HAS_LONG_LONG) &&\
93    ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
94     (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
95     (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
96     (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
97   template <> struct exact_signed_base_helper<sizeof(boost::long_long_type)* CHAR_BIT> { typedef boost::long_long_type exact; };
98   template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
99 #endif
100
101
102   } // namespace detail
103
104   //  integer templates specifying number of bits  ---------------------------//
105
106   //  signed
107   template< int Bits >   // bits (including sign) required
108   struct int_t : public detail::exact_signed_base_helper<Bits>
109   {
110       typedef typename detail::int_least_helper
111         <
112 #ifdef BOOST_HAS_LONG_LONG
113           (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
114 #else
115            1 +
116 #endif
117           (Bits-1 <= ::std::numeric_limits<long>::digits) +
118           (Bits-1 <= ::std::numeric_limits<int>::digits) +
119           (Bits-1 <= ::std::numeric_limits<short>::digits) +
120           (Bits-1 <= ::std::numeric_limits<signed char>::digits)
121         >::least  least;
122       typedef typename int_fast_t<least>::type  fast;
123   };
124
125   //  unsigned
126   template< int Bits >   // bits required
127   struct uint_t : public detail::exact_unsigned_base_helper<Bits>
128   {
129 #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
130      // It's really not clear why this workaround should be needed... shrug I guess!  JM
131      BOOST_STATIC_CONSTANT(int, s = 
132            6 +
133           (Bits <= ::std::numeric_limits<unsigned long>::digits) +
134           (Bits <= ::std::numeric_limits<unsigned int>::digits) +
135           (Bits <= ::std::numeric_limits<unsigned short>::digits) +
136           (Bits <= ::std::numeric_limits<unsigned char>::digits));
137      typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
138 #else
139       typedef typename detail::int_least_helper
140         < 
141           5 +
142 #ifdef BOOST_HAS_LONG_LONG
143           (Bits-1 <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
144 #else
145            1 +
146 #endif
147           (Bits <= ::std::numeric_limits<unsigned long>::digits) +
148           (Bits <= ::std::numeric_limits<unsigned int>::digits) +
149           (Bits <= ::std::numeric_limits<unsigned short>::digits) +
150           (Bits <= ::std::numeric_limits<unsigned char>::digits)
151         >::least  least;
152 #endif
153       typedef typename int_fast_t<least>::type  fast;
154       // int_fast_t<> works correctly for unsigned too, in spite of the name.
155   };
156
157   //  integer templates specifying extreme value  ----------------------------//
158
159   //  signed
160 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
161   template< boost::long_long_type MaxValue >   // maximum value to require support
162 #else
163   template< long MaxValue >   // maximum value to require support
164 #endif
165   struct int_max_value_t 
166   {
167       typedef typename detail::int_least_helper
168         <
169 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
170           (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
171 #else
172            1 +
173 #endif
174           (MaxValue <= ::boost::integer_traits<long>::const_max) +
175           (MaxValue <= ::boost::integer_traits<int>::const_max) +
176           (MaxValue <= ::boost::integer_traits<short>::const_max) +
177           (MaxValue <= ::boost::integer_traits<signed char>::const_max)
178         >::least  least;
179       typedef typename int_fast_t<least>::type  fast;
180   };
181
182 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
183   template< boost::long_long_type MinValue >   // minimum value to require support
184 #else
185   template< long MinValue >   // minimum value to require support
186 #endif
187   struct int_min_value_t 
188   {
189       typedef typename detail::int_least_helper
190         <
191 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
192           (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
193 #else
194            1 +
195 #endif
196           (MinValue >= ::boost::integer_traits<long>::const_min) +
197           (MinValue >= ::boost::integer_traits<int>::const_min) +
198           (MinValue >= ::boost::integer_traits<short>::const_min) +
199           (MinValue >= ::boost::integer_traits<signed char>::const_min)
200         >::least  least;
201       typedef typename int_fast_t<least>::type  fast;
202   };
203
204   //  unsigned
205 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
206   template< boost::ulong_long_type MaxValue >   // minimum value to require support
207 #else
208   template< unsigned long MaxValue >   // minimum value to require support
209 #endif
210   struct uint_value_t 
211   {
212 #if (defined(__BORLANDC__) || defined(__CODEGEAR__))
213      // It's really not clear why this workaround should be needed... shrug I guess!  JM
214 #if defined(BOOST_NO_INTEGRAL_INT64_T)
215       BOOST_STATIC_CONSTANT(unsigned, which = 
216            6 +
217           (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
218           (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
219           (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
220           (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
221       typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
222 #else // BOOST_NO_INTEGRAL_INT64_T
223       BOOST_STATIC_CONSTANT(unsigned, which = 
224            5 +
225           (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
226           (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
227           (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
228           (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
229           (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
230       typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
231 #endif // BOOST_NO_INTEGRAL_INT64_T
232 #else
233       typedef typename detail::int_least_helper
234         < 
235           5 +
236 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
237           (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
238 #else
239            1 +
240 #endif
241           (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
242           (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
243           (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
244           (MaxValue <= ::boost::integer_traits<unsigned char>::const_max)
245         >::least  least;
246 #endif
247       typedef typename int_fast_t<least>::type  fast;
248   };
249
250
251 } // namespace boost
252
253 #endif  // BOOST_INTEGER_HPP