os/ossrv/ossrv_pub/boost_apis/boost/detail/numeric_traits.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// (C) Copyright David Abrahams 2001, Howard Hinnant 2001.
sl@0
     2
//
sl@0
     3
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     4
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     5
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
//
sl@0
     7
// Template class numeric_traits<Number> --
sl@0
     8
//
sl@0
     9
//    Supplies:
sl@0
    10
//
sl@0
    11
//      typedef difference_type -- a type used to represent the difference
sl@0
    12
//      between any two values of Number.
sl@0
    13
//
sl@0
    14
//    Support:
sl@0
    15
//      1. Not all specializations are supplied
sl@0
    16
//
sl@0
    17
//      2. Use of specializations that are not supplied will cause a
sl@0
    18
//      compile-time error
sl@0
    19
//
sl@0
    20
//      3. Users are free to specialize numeric_traits for any type.
sl@0
    21
//
sl@0
    22
//      4. Right now, specializations are only supplied for integer types.
sl@0
    23
//
sl@0
    24
//      5. On implementations which do not supply compile-time constants in
sl@0
    25
//      std::numeric_limits<>, only specializations for built-in integer types
sl@0
    26
//      are supplied.
sl@0
    27
//
sl@0
    28
//      6. Handling of numbers whose range of representation is at least as
sl@0
    29
//      great as boost::intmax_t can cause some differences to be
sl@0
    30
//      unrepresentable in difference_type:
sl@0
    31
//
sl@0
    32
//        Number    difference_type
sl@0
    33
//        ------    ---------------
sl@0
    34
//        signed    Number
sl@0
    35
//        unsigned  intmax_t
sl@0
    36
//
sl@0
    37
// template <class Number> typename numeric_traits<Number>::difference_type
sl@0
    38
// numeric_distance(Number x, Number y)
sl@0
    39
//    computes (y - x), attempting to avoid overflows.
sl@0
    40
//
sl@0
    41
sl@0
    42
// See http://www.boost.org for most recent version including documentation.
sl@0
    43
sl@0
    44
// Revision History
sl@0
    45
// 11 Feb 2001 - Use BOOST_STATIC_CONSTANT (David Abrahams)
sl@0
    46
// 11 Feb 2001 - Rolled back ineffective Borland-specific code
sl@0
    47
//               (David Abrahams)
sl@0
    48
// 10 Feb 2001 - Rolled in supposed Borland fixes from John Maddock, but
sl@0
    49
//               not seeing any improvement yet (David Abrahams)
sl@0
    50
// 06 Feb 2001 - Factored if_true out into boost/detail/select_type.hpp
sl@0
    51
//               (David Abrahams)
sl@0
    52
// 23 Jan 2001 - Fixed logic of difference_type selection, which was
sl@0
    53
//               completely wack. In the process, added digit_traits<>
sl@0
    54
//               to compute the number of digits in intmax_t even when
sl@0
    55
//               not supplied by numeric_limits<>. (David Abrahams)
sl@0
    56
// 21 Jan 2001 - Created (David Abrahams)
sl@0
    57
sl@0
    58
#ifndef BOOST_NUMERIC_TRAITS_HPP_DWA20001901
sl@0
    59
# define BOOST_NUMERIC_TRAITS_HPP_DWA20001901
sl@0
    60
sl@0
    61
# include <boost/config.hpp>
sl@0
    62
# include <boost/cstdint.hpp>
sl@0
    63
# include <boost/static_assert.hpp>
sl@0
    64
# include <boost/type_traits.hpp>
sl@0
    65
# include <boost/detail/select_type.hpp>
sl@0
    66
# include <boost/limits.hpp>
sl@0
    67
sl@0
    68
namespace boost { namespace detail {
sl@0
    69
sl@0
    70
  // Template class is_signed -- determine whether a numeric type is signed
sl@0
    71
  // Requires that T is constructable from the literals -1 and 0.  Compile-time
sl@0
    72
  // error results if that requirement is not met (and thus signedness is not
sl@0
    73
  // likely to have meaning for that type).
sl@0
    74
  template <class Number>
sl@0
    75
  struct is_signed
sl@0
    76
  {
sl@0
    77
#if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_MSVC) && BOOST_MSVC <= 1300
sl@0
    78
    BOOST_STATIC_CONSTANT(bool, value = (Number(-1) < Number(0)));
sl@0
    79
#else
sl@0
    80
    BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<Number>::is_signed);
sl@0
    81
#endif
sl@0
    82
  };
sl@0
    83
sl@0
    84
# ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    85
  // digit_traits - compute the number of digits in a built-in integer
sl@0
    86
  // type. Needed for implementations on which numeric_limits is not specialized
sl@0
    87
  // for intmax_t (e.g. VC6).
sl@0
    88
  template <bool is_specialized> struct digit_traits_select;
sl@0
    89
sl@0
    90
  // numeric_limits is specialized; just select that version of digits
sl@0
    91
  template <> struct digit_traits_select<true>
sl@0
    92
  {
sl@0
    93
      template <class T> struct traits
sl@0
    94
      {
sl@0
    95
          BOOST_STATIC_CONSTANT(int, digits = std::numeric_limits<T>::digits);
sl@0
    96
      };
sl@0
    97
  };
sl@0
    98
sl@0
    99
  // numeric_limits is not specialized; compute digits from sizeof(T)
sl@0
   100
  template <> struct digit_traits_select<false>
sl@0
   101
  {
sl@0
   102
      template <class T> struct traits
sl@0
   103
      {
sl@0
   104
          BOOST_STATIC_CONSTANT(int, digits = (
sl@0
   105
              sizeof(T) * std::numeric_limits<unsigned char>::digits
sl@0
   106
              - (is_signed<T>::value ? 1 : 0))
sl@0
   107
              );
sl@0
   108
      };
sl@0
   109
  };
sl@0
   110
sl@0
   111
  // here's the "usable" template
sl@0
   112
  template <class T> struct digit_traits
sl@0
   113
  {
sl@0
   114
      typedef digit_traits_select<
sl@0
   115
                ::std::numeric_limits<T>::is_specialized> selector;
sl@0
   116
      typedef typename selector::template traits<T> traits;
sl@0
   117
      BOOST_STATIC_CONSTANT(int, digits = traits::digits);
sl@0
   118
  };
sl@0
   119
#endif
sl@0
   120
sl@0
   121
  // Template class integer_traits<Integer> -- traits of various integer types
sl@0
   122
  // This should probably be rolled into boost::integer_traits one day, but I
sl@0
   123
  // need it to work without <limits>
sl@0
   124
  template <class Integer>
sl@0
   125
  struct integer_traits
sl@0
   126
  {
sl@0
   127
# ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
   128
   private:
sl@0
   129
      typedef Integer integer_type;
sl@0
   130
      typedef std::numeric_limits<integer_type> x;
sl@0
   131
#   if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
sl@0
   132
      // for some reason, MSVC asserts when it shouldn't unless we make these
sl@0
   133
      // local definitions
sl@0
   134
      BOOST_STATIC_CONSTANT(bool, is_integer = x::is_integer);
sl@0
   135
      BOOST_STATIC_CONSTANT(bool, is_specialized = x::is_specialized);
sl@0
   136
      
sl@0
   137
      BOOST_STATIC_ASSERT(is_integer);
sl@0
   138
      BOOST_STATIC_ASSERT(is_specialized);
sl@0
   139
#   endif
sl@0
   140
   public:
sl@0
   141
      typedef typename
sl@0
   142
      if_true<(int(x::is_signed)
sl@0
   143
              && (!int(x::is_bounded)
sl@0
   144
                 // digits is the number of no-sign bits
sl@0
   145
                  || (int(x::digits) + 1 >= digit_traits<boost::intmax_t>::digits)))>::template then<
sl@0
   146
        Integer,
sl@0
   147
          
sl@0
   148
      typename if_true<(int(x::digits) + 1 < digit_traits<signed int>::digits)>::template then<
sl@0
   149
        signed int,
sl@0
   150
sl@0
   151
      typename if_true<(int(x::digits) + 1 < digit_traits<signed long>::digits)>::template then<
sl@0
   152
        signed long,
sl@0
   153
sl@0
   154
   // else
sl@0
   155
        intmax_t
sl@0
   156
      >::type>::type>::type difference_type;
sl@0
   157
#else
sl@0
   158
      BOOST_STATIC_ASSERT(boost::is_integral<Integer>::value);
sl@0
   159
sl@0
   160
      typedef typename
sl@0
   161
      if_true<(sizeof(Integer) >= sizeof(intmax_t))>::template then<
sl@0
   162
               
sl@0
   163
        typename if_true<(is_signed<Integer>::value)>::template then<
sl@0
   164
          Integer,
sl@0
   165
          intmax_t
sl@0
   166
        >::type,
sl@0
   167
sl@0
   168
        typename if_true<(sizeof(Integer) < sizeof(std::ptrdiff_t))>::template then<
sl@0
   169
          std::ptrdiff_t,
sl@0
   170
          intmax_t
sl@0
   171
        >::type
sl@0
   172
      >::type difference_type;
sl@0
   173
# endif
sl@0
   174
  };
sl@0
   175
sl@0
   176
  // Right now, only supports integers, but should be expanded.
sl@0
   177
  template <class Number>
sl@0
   178
  struct numeric_traits
sl@0
   179
  {
sl@0
   180
      typedef typename integer_traits<Number>::difference_type difference_type;
sl@0
   181
  };
sl@0
   182
sl@0
   183
  template <class Number>
sl@0
   184
  typename numeric_traits<Number>::difference_type numeric_distance(Number x, Number y)
sl@0
   185
  {
sl@0
   186
      typedef typename numeric_traits<Number>::difference_type difference_type;
sl@0
   187
      return difference_type(y) - difference_type(x);
sl@0
   188
  }
sl@0
   189
}}
sl@0
   190
sl@0
   191
#endif // BOOST_NUMERIC_TRAITS_HPP_DWA20001901