epoc32/include/stdapis/boost/numeric/conversion/converter_policies.hpp
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
permissions -rw-r--r--
Final list of Symbian^2 public API header files
williamr@2
     1
//  © Copyright Fernando Luis Cacciola Carballal 2000-2004
williamr@2
     2
//  Use, modification, and distribution is subject to the Boost Software
williamr@2
     3
//  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
williamr@2
     4
//  http://www.boost.org/LICENSE_1_0.txt)
williamr@2
     5
williamr@2
     6
//  See library home page at http://www.boost.org/libs/numeric/conversion
williamr@2
     7
//
williamr@2
     8
// Contact the author at: fernando_cacciola@hotmail.com
williamr@2
     9
//
williamr@2
    10
#ifndef BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP
williamr@2
    11
#define BOOST_NUMERIC_CONVERSION_CONVERTER_POLICIES_FLC_12NOV2002_HPP
williamr@2
    12
williamr@2
    13
#include <typeinfo> // for std::bad_cast
williamr@2
    14
williamr@2
    15
#include <cmath> // for std::floor and std::ceil
williamr@2
    16
williamr@2
    17
#include <functional>
williamr@2
    18
williamr@2
    19
#include "boost/type_traits/is_arithmetic.hpp"
williamr@2
    20
williamr@2
    21
#include "boost/mpl/if.hpp"
williamr@2
    22
#include "boost/mpl/integral_c.hpp"
williamr@2
    23
williamr@2
    24
namespace boost { namespace numeric
williamr@2
    25
{
williamr@2
    26
williamr@2
    27
template<class S>
williamr@2
    28
struct Trunc
williamr@2
    29
{
williamr@2
    30
  typedef S source_type ;
williamr@2
    31
williamr@2
    32
  typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
williamr@2
    33
williamr@2
    34
  static source_type nearbyint ( argument_type s )
williamr@2
    35
  {
williamr@2
    36
#if !defined(BOOST_NO_STDC_NAMESPACE)
williamr@2
    37
    using std::floor ;
williamr@2
    38
    using std::ceil  ;
williamr@2
    39
#endif
williamr@2
    40
williamr@2
    41
    return s < static_cast<S>(0) ? ceil(s) : floor(s) ;
williamr@2
    42
  }
williamr@2
    43
williamr@2
    44
  typedef mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style ;
williamr@2
    45
} ;
williamr@2
    46
williamr@2
    47
williamr@2
    48
williamr@2
    49
template<class S>
williamr@2
    50
struct Floor
williamr@2
    51
{
williamr@2
    52
  typedef S source_type ;
williamr@2
    53
williamr@2
    54
  typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
williamr@2
    55
williamr@2
    56
  static source_type nearbyint ( argument_type s )
williamr@2
    57
  {
williamr@2
    58
#if !defined(BOOST_NO_STDC_NAMESPACE)
williamr@2
    59
    using std::floor ;
williamr@2
    60
#endif
williamr@2
    61
williamr@2
    62
    return floor(s) ;
williamr@2
    63
  }
williamr@2
    64
williamr@2
    65
  typedef mpl::integral_c< std::float_round_style, std::round_toward_neg_infinity> round_style ;
williamr@2
    66
} ;
williamr@2
    67
williamr@2
    68
template<class S>
williamr@2
    69
struct Ceil
williamr@2
    70
{
williamr@2
    71
  typedef S source_type ;
williamr@2
    72
williamr@2
    73
  typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
williamr@2
    74
williamr@2
    75
  static source_type nearbyint ( argument_type s )
williamr@2
    76
  {
williamr@2
    77
#if !defined(BOOST_NO_STDC_NAMESPACE)
williamr@2
    78
    using std::ceil ;
williamr@2
    79
#endif
williamr@2
    80
williamr@2
    81
    return ceil(s) ;
williamr@2
    82
  }
williamr@2
    83
williamr@2
    84
  typedef mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style ;
williamr@2
    85
} ;
williamr@2
    86
williamr@2
    87
template<class S>
williamr@2
    88
struct RoundEven
williamr@2
    89
{
williamr@2
    90
  typedef S source_type ;
williamr@2
    91
williamr@2
    92
  typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
williamr@2
    93
williamr@2
    94
  static source_type nearbyint ( argument_type s )
williamr@2
    95
  {
williamr@2
    96
    // Algorithm contributed by Guillaume Melquiond
williamr@2
    97
williamr@2
    98
#if !defined(BOOST_NO_STDC_NAMESPACE)
williamr@2
    99
    using std::floor ;
williamr@2
   100
    using std::ceil  ;
williamr@2
   101
#endif
williamr@2
   102
williamr@2
   103
    // only works inside the range not at the boundaries
williamr@2
   104
    S prev = floor(s);
williamr@2
   105
    S next = ceil(s);
williamr@2
   106
williamr@2
   107
    S rt = (s - prev) - (next - s); // remainder type
williamr@2
   108
williamr@2
   109
    S const zero(0.0);
williamr@2
   110
    S const two(2.0);
williamr@2
   111
williamr@2
   112
    if ( rt < zero )
williamr@2
   113
      return prev;
williamr@2
   114
    else if ( rt > zero )
williamr@2
   115
      return next;
williamr@2
   116
    else
williamr@2
   117
    {
williamr@2
   118
      bool is_prev_even = two * floor(prev / two) == prev ;
williamr@2
   119
      return ( is_prev_even ? prev : next ) ;
williamr@2
   120
    }
williamr@2
   121
  }
williamr@2
   122
williamr@2
   123
  typedef mpl::integral_c< std::float_round_style, std::round_to_nearest> round_style ;
williamr@2
   124
} ;
williamr@2
   125
williamr@2
   126
williamr@2
   127
enum range_check_result
williamr@2
   128
{
williamr@2
   129
  cInRange     = 0 ,
williamr@2
   130
  cNegOverflow = 1 ,
williamr@2
   131
  cPosOverflow = 2
williamr@2
   132
} ;
williamr@2
   133
williamr@2
   134
class bad_numeric_cast : public std::bad_cast
williamr@2
   135
{
williamr@2
   136
  public:
williamr@2
   137
williamr@2
   138
    virtual const char * what() const throw()
williamr@2
   139
      {  return "bad numeric conversion: overflow"; }
williamr@2
   140
};
williamr@2
   141
williamr@2
   142
class negative_overflow : public bad_numeric_cast
williamr@2
   143
{
williamr@2
   144
  public:
williamr@2
   145
williamr@2
   146
    virtual const char * what() const throw()
williamr@2
   147
      {  return "bad numeric conversion: negative overflow"; }
williamr@2
   148
};
williamr@2
   149
class positive_overflow : public bad_numeric_cast
williamr@2
   150
{
williamr@2
   151
  public:
williamr@2
   152
williamr@2
   153
    virtual const char * what() const throw()
williamr@2
   154
      { return "bad numeric conversion: positive overflow"; }
williamr@2
   155
};
williamr@2
   156
williamr@2
   157
struct def_overflow_handler
williamr@2
   158
{
williamr@2
   159
  void operator() ( range_check_result r ) // throw(negative_overflow,positive_overflow)
williamr@2
   160
  {
williamr@2
   161
    if ( r == cNegOverflow )
williamr@2
   162
      throw negative_overflow() ;
williamr@2
   163
    else if ( r == cPosOverflow )
williamr@2
   164
           throw positive_overflow() ;
williamr@2
   165
  }
williamr@2
   166
} ;
williamr@2
   167
williamr@2
   168
struct silent_overflow_handler
williamr@2
   169
{
williamr@2
   170
  void operator() ( range_check_result ) {} // throw()
williamr@2
   171
} ;
williamr@2
   172
williamr@2
   173
template<class Traits>
williamr@2
   174
struct raw_converter
williamr@2
   175
{
williamr@2
   176
  typedef typename Traits::result_type   result_type   ;
williamr@2
   177
  typedef typename Traits::argument_type argument_type ;
williamr@2
   178
williamr@2
   179
  static result_type low_level_convert ( argument_type s ) { return static_cast<result_type>(s) ; }
williamr@2
   180
} ;
williamr@2
   181
williamr@2
   182
struct UseInternalRangeChecker {} ;
williamr@2
   183
williamr@2
   184
} } // namespace boost::numeric
williamr@2
   185
williamr@2
   186
#endif