src/sysc/packages/boost/detail/numeric_traits.hpp

Go to the documentation of this file.
00001 // (C) Copyright David Abrahams 2001. Permission to copy, use, modify,
00002 // sell and distribute this software is granted provided this
00003 // copyright notice appears in all copies. This software is provided
00004 // "as is" without express or implied warranty, and with no claim as
00005 // to its suitability for any purpose.
00006 //
00007 // Template class is_signed and its documentation is:
00008 // (C) Copyright Howard Hinnant 2001. Permission to copy, use, modify,
00009 // sell and distribute this software is granted provided this
00010 // copyright notice appears in all copies. This software is provided
00011 // "as is" without express or implied warranty, and with no claim as
00012 // to its suitability for any purpose.
00013 //
00014 // Template class numeric_traits<Number> --
00015 //
00016 //    Supplies:
00017 //
00018 //      typedef difference_type -- a type used to represent the difference
00019 //      between any two values of Number.
00020 //
00021 //    Support:
00022 //      1. Not all specializations are supplied
00023 //
00024 //      2. Use of specializations that are not supplied will cause a
00025 //      compile-time error
00026 //
00027 //      3. Users are free to specialize numeric_traits for any type.
00028 //
00029 //      4. Right now, specializations are only supplied for integer types.
00030 //
00031 //      5. On implementations which do not supply compile-time constants in
00032 //      std::numeric_limits<>, only specializations for built-in integer types
00033 //      are supplied.
00034 //
00035 //      6. Handling of numbers whose range of representation is at least as
00036 //      great as boost::intmax_t can cause some differences to be
00037 //      unrepresentable in difference_type:
00038 //
00039 //        Number    difference_type
00040 //        ------    ---------------
00041 //        signed    Number
00042 //        unsigned  intmax_t
00043 //
00044 // template <class Number> typename numeric_traits<Number>::difference_type
00045 // numeric_distance(Number x, Number y)
00046 //    computes (y - x), attempting to avoid overflows.
00047 //
00048 
00049 // See http://www.boost.org for most recent version including documentation.
00050 
00051 // Revision History
00052 // 11 Feb 2001 - Use BOOST_STATIC_CONSTANT (David Abrahams)
00053 // 11 Feb 2001 - Rolled back ineffective Borland-specific code
00054 //               (David Abrahams)
00055 // 10 Feb 2001 - Rolled in supposed Borland fixes from John Maddock, but
00056 //               not seeing any improvement yet (David Abrahams)
00057 // 06 Feb 2001 - Factored if_true out into boost/detail/select_type.hpp
00058 //               (David Abrahams)
00059 // 23 Jan 2001 - Fixed logic of difference_type selection, which was
00060 //               completely wack. In the process, added digit_traits<>
00061 //               to compute the number of digits in intmax_t even when
00062 //               not supplied by numeric_limits<>. (David Abrahams)
00063 // 21 Jan 2001 - Created (David Abrahams)
00064 
00065 #ifndef BOOST_NUMERIC_TRAITS_HPP_DWA20001901
00066 # define BOOST_NUMERIC_TRAITS_HPP_DWA20001901
00067 
00068 # include <sysc/packages/boost/config.hpp>
00069 # include <sysc/packages/boost/cstdint.hpp>
00070 # include <sysc/packages/boost/static_assert.hpp>
00071 # include <sysc/packages/boost/type_traits.hpp>
00072 # include <sysc/packages/boost/detail/select_type.hpp>
00073 # include <sysc/packages/boost/limits.hpp>
00074 
00075 namespace boost { namespace detail {
00076 
00077   // Template class is_signed -- determine whether a numeric type is signed
00078   // Requires that T is constructable from the literals -1 and 0.  Compile-time
00079   // error results if that requirement is not met (and thus signedness is not
00080   // likely to have meaning for that type).
00081   template <class Number>
00082   struct is_signed
00083   {
00084 #if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_MSVC) && BOOST_MSVC <= 1300
00085     BOOST_STATIC_CONSTANT(bool, value = (Number(-1) < Number(0)));
00086 #else
00087     BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<Number>::is_signed);
00088 #endif
00089   };
00090 
00091 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
00092   // digit_traits - compute the number of digits in a built-in integer
00093   // type. Needed for implementations on which numeric_limits is not specialized
00094   // for intmax_t (e.g. VC6).
00095   template <bool is_specialized> struct digit_traits_select;
00096 
00097   // numeric_limits is specialized; just select that version of digits
00098   template <> struct digit_traits_select<true>
00099   {
00100       template <class T> struct traits
00101       {
00102           BOOST_STATIC_CONSTANT(int, digits = std::numeric_limits<T>::digits);
00103       };
00104   };
00105 
00106   // numeric_limits is not specialized; compute digits from sizeof(T)
00107   template <> struct digit_traits_select<false>
00108   {
00109       template <class T> struct traits
00110       {
00111           BOOST_STATIC_CONSTANT(int, digits = (
00112               sizeof(T) * std::numeric_limits<unsigned char>::digits
00113               - (is_signed<T>::value ? 1 : 0))
00114               );
00115       };
00116   };
00117 
00118   // here's the "usable" template
00119   template <class T> struct digit_traits
00120   {
00121       typedef digit_traits_select<
00122                 ::std::numeric_limits<T>::is_specialized> selector;
00123       typedef typename selector::template traits<T> traits;
00124       BOOST_STATIC_CONSTANT(int, digits = traits::digits);
00125   };
00126 #endif
00127 
00128   // Template class integer_traits<Integer> -- traits of various integer types
00129   // This should probably be rolled into boost::integer_traits one day, but I
00130   // need it to work without <limits>
00131   template <class Integer>
00132   struct integer_traits
00133   {
00134 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
00135    private:
00136       typedef Integer integer_type;
00137       typedef std::numeric_limits<integer_type> x;
00138 #   if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
00139       // for some reason, MSVC asserts when it shouldn't unless we make these
00140       // local definitions
00141       BOOST_STATIC_CONSTANT(bool, is_integer = x::is_integer);
00142       BOOST_STATIC_CONSTANT(bool, is_specialized = x::is_specialized);
00143       
00144       BOOST_STATIC_ASSERT(is_integer);
00145       BOOST_STATIC_ASSERT(is_specialized);
00146 #   endif
00147    public:
00148       typedef typename
00149       if_true<(int(x::is_signed)
00150               && (!int(x::is_bounded)
00151                  // digits is the number of no-sign bits
00152                   || (int(x::digits) + 1 >= digit_traits<boost::intmax_t>::digits)))>::template then<
00153         Integer,
00154           
00155       typename if_true<(int(x::digits) + 1 < digit_traits<signed int>::digits)>::template then<
00156         signed int,
00157 
00158       typename if_true<(int(x::digits) + 1 < digit_traits<signed long>::digits)>::template then<
00159         signed long,
00160 
00161    // else
00162         intmax_t
00163       >::type>::type>::type difference_type;
00164 #else
00165       BOOST_STATIC_ASSERT(boost::is_integral<Integer>::value);
00166 
00167       typedef typename
00168       if_true<(sizeof(Integer) >= sizeof(intmax_t))>::template then<
00169                
00170         typename if_true<(is_signed<Integer>::value)>::template then<
00171           Integer,
00172           intmax_t
00173         >::type,
00174 
00175         typename if_true<(sizeof(Integer) < sizeof(std::ptrdiff_t))>::template then<
00176           std::ptrdiff_t,
00177           intmax_t
00178         >::type
00179       >::type difference_type;
00180 # endif
00181   };
00182 
00183   // Right now, only supports integers, but should be expanded.
00184   template <class Number>
00185   struct numeric_traits
00186   {
00187       typedef typename integer_traits<Number>::difference_type difference_type;
00188   };
00189 
00190   template <class Number>
00191   typename numeric_traits<Number>::difference_type numeric_distance(Number x, Number y)
00192   {
00193       typedef typename numeric_traits<Number>::difference_type difference_type;
00194       return difference_type(y) - difference_type(x);
00195   }
00196 }}
00197 
00198 #endif // BOOST_NUMERIC_TRAITS_HPP_DWA20001901

Generated on Wed Apr 25 13:53:28 2007 for SystemC by  doxygen 1.5.1