epoc32/include/stdapis/boost/multi_index/detail/converter.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 2 epoc32/include/stdapis/boost/numeric/conversion/detail/converter.hpp@2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
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_DETAIL_CONVERTER_FLC_12NOV2002_HPP
williamr@2
    11
#define BOOST_NUMERIC_CONVERSION_DETAIL_CONVERTER_FLC_12NOV2002_HPP
williamr@2
    12
williamr@2
    13
#include <functional>
williamr@2
    14
williamr@2
    15
#include "boost/numeric/conversion/detail/meta.hpp"
williamr@2
    16
#include "boost/numeric/conversion/detail/conversion_traits.hpp"
williamr@2
    17
#include "boost/numeric/conversion/bounds.hpp"
williamr@2
    18
williamr@2
    19
#include "boost/type_traits/is_same.hpp"
williamr@2
    20
williamr@2
    21
#include "boost/mpl/integral_c.hpp"
williamr@2
    22
williamr@2
    23
namespace boost { namespace numeric { namespace convdetail
williamr@2
    24
{
williamr@2
    25
  // Integral Constants representing rounding modes
williamr@2
    26
  typedef mpl::integral_c<std::float_round_style, std::round_toward_zero>         round2zero_c ;
williamr@2
    27
  typedef mpl::integral_c<std::float_round_style, std::round_to_nearest>          round2nearest_c ;
williamr@2
    28
  typedef mpl::integral_c<std::float_round_style, std::round_toward_infinity>     round2inf_c ;
williamr@2
    29
  typedef mpl::integral_c<std::float_round_style, std::round_toward_neg_infinity> round2neg_inf_c ;
williamr@2
    30
williamr@2
    31
  // Metafunction:
williamr@2
    32
  //
williamr@2
    33
  //   for_round_style<RoundStyle,RoundToZero,RoundToNearest,RoundToInf,RoundToNegInf>::type
williamr@2
    34
  //
williamr@2
    35
  // {RoundStyle} Integral Constant specifying a round style as declared above.
williamr@2
    36
  // {RoundToZero,RoundToNearest,RoundToInf,RoundToNegInf} arbitrary types.
williamr@2
    37
  //
williamr@2
    38
  // Selects one of the 4 types according to the value of RoundStyle.
williamr@2
    39
  //
williamr@2
    40
  template<class RoundStyle,class RoundToZero,class RoundToNearest,class RoundToInf,class RoundToNegInf>
williamr@2
    41
  struct for_round_style
williamr@2
    42
  {
williamr@2
    43
    typedef ct_switch4<RoundStyle
williamr@2
    44
                       , round2zero_c, round2nearest_c, round2inf_c // round2neg_inf_c
williamr@2
    45
                       , RoundToZero , RoundToNearest , RoundToInf , RoundToNegInf
williamr@2
    46
                      > selector ;
williamr@2
    47
williamr@2
    48
    typedef typename selector::type type ;
williamr@2
    49
  } ;
williamr@2
    50
williamr@2
    51
williamr@2
    52
williamr@2
    53
williamr@2
    54
williamr@2
    55
williamr@2
    56
williamr@2
    57
williamr@2
    58
williamr@2
    59
williamr@2
    60
williamr@2
    61
williamr@2
    62
williamr@2
    63
williamr@2
    64
williamr@2
    65
williamr@2
    66
williamr@2
    67
williamr@2
    68
//--------------------------------------------------------------------------
williamr@2
    69
//                             Range Checking Logic.
williamr@2
    70
//
williamr@2
    71
// The range checking logic is built up by combining 1 or 2 predicates.
williamr@2
    72
// Each predicate is encapsulated in a template class and exposes
williamr@2
    73
// the static member function 'apply'.
williamr@2
    74
//
williamr@2
    75
//--------------------------------------------------------------------------
williamr@2
    76
williamr@2
    77
williamr@2
    78
  // Because a particular logic can combine either 1 or two predicates, the following
williamr@2
    79
  // tags are used to allow the predicate applier to receive 2 preds, but optimize away
williamr@2
    80
  // one of them if it is 'non-applicable'
williamr@2
    81
  struct non_applicable { typedef mpl::false_ do_apply ; } ;
williamr@2
    82
  struct applicable     { typedef mpl::true_  do_apply ; } ;
williamr@2
    83
williamr@2
    84
williamr@2
    85
  //--------------------------------------------------------------------------
williamr@2
    86
  //
williamr@2
    87
  //                      Range Checking Logic implementations.
williamr@2
    88
  //
williamr@2
    89
  // The following classes, collectivelly named 'Predicates', are instantiated within
williamr@2
    90
  // the corresponding range checkers.
williamr@2
    91
  // Their static member function 'apply' is called to perform the actual range checking logic.
williamr@2
    92
  //--------------------------------------------------------------------------
williamr@2
    93
williamr@2
    94
    // s < Lowest(T) ? cNegOverflow : cInRange
williamr@2
    95
    //
williamr@2
    96
    template<class Traits>
williamr@2
    97
    struct LT_LoT : applicable
williamr@2
    98
    {
williamr@2
    99
      typedef typename Traits::target_type T ;
williamr@2
   100
      typedef typename Traits::source_type S ;
williamr@2
   101
      typedef typename Traits::argument_type argument_type ;
williamr@2
   102
williamr@2
   103
      static range_check_result apply ( argument_type s )
williamr@2
   104
      {
williamr@2
   105
        return s < static_cast<S>(bounds<T>::lowest()) ? cNegOverflow : cInRange ;
williamr@2
   106
      }
williamr@2
   107
    } ;
williamr@2
   108
williamr@2
   109
    // s < 0 ? cNegOverflow : cInRange
williamr@2
   110
    //
williamr@2
   111
    template<class Traits>
williamr@2
   112
    struct LT_Zero : applicable
williamr@2
   113
    {
williamr@2
   114
      typedef typename Traits::source_type S ;
williamr@2
   115
      typedef typename Traits::argument_type argument_type ;
williamr@2
   116
williamr@2
   117
      static range_check_result apply ( argument_type s )
williamr@2
   118
      {
williamr@2
   119
        return s < static_cast<S>(0) ? cNegOverflow : cInRange ;
williamr@2
   120
      }
williamr@2
   121
    } ;
williamr@2
   122
williamr@2
   123
    // s <= Lowest(T)-1 ? cNegOverflow : cInRange
williamr@2
   124
    //
williamr@2
   125
    template<class Traits>
williamr@2
   126
    struct LE_PrevLoT : applicable
williamr@2
   127
    {
williamr@2
   128
      typedef typename Traits::target_type T ;
williamr@2
   129
      typedef typename Traits::source_type S ;
williamr@2
   130
      typedef typename Traits::argument_type argument_type ;
williamr@2
   131
williamr@2
   132
      static range_check_result apply ( argument_type s )
williamr@2
   133
      {
williamr@2
   134
        return s <= static_cast<S>(bounds<T>::lowest()) - static_cast<S>(1.0)
williamr@2
   135
                 ? cNegOverflow : cInRange ;
williamr@2
   136
      }
williamr@2
   137
    } ;
williamr@2
   138
williamr@2
   139
    // s < Lowest(T)-0.5 ? cNegOverflow : cInRange
williamr@2
   140
    //
williamr@2
   141
    template<class Traits>
williamr@2
   142
    struct LT_HalfPrevLoT : applicable
williamr@2
   143
    {
williamr@2
   144
      typedef typename Traits::target_type T ;
williamr@2
   145
      typedef typename Traits::source_type S ;
williamr@2
   146
      typedef typename Traits::argument_type argument_type ;
williamr@2
   147
williamr@2
   148
      static range_check_result apply ( argument_type s )
williamr@2
   149
      {
williamr@2
   150
        return s < static_cast<S>(bounds<T>::lowest()) - static_cast<S>(0.5)
williamr@2
   151
                 ? cNegOverflow : cInRange ;
williamr@2
   152
      }
williamr@2
   153
    } ;
williamr@2
   154
williamr@2
   155
    // s > Highest(T) ? cPosOverflow : cInRange
williamr@2
   156
    //
williamr@2
   157
    template<class Traits>
williamr@2
   158
    struct GT_HiT : applicable
williamr@2
   159
    {
williamr@2
   160
      typedef typename Traits::target_type T ;
williamr@2
   161
      typedef typename Traits::source_type S ;
williamr@2
   162
      typedef typename Traits::argument_type argument_type ;
williamr@2
   163
williamr@2
   164
      static range_check_result apply ( argument_type s )
williamr@2
   165
      {
williamr@2
   166
        return s > static_cast<S>(bounds<T>::highest())
williamr@2
   167
                 ? cPosOverflow : cInRange ;
williamr@2
   168
      }
williamr@2
   169
    } ;
williamr@2
   170
williamr@2
   171
    // s >= Lowest(T) + 1 ? cPosOverflow : cInRange
williamr@2
   172
    //
williamr@2
   173
    template<class Traits>
williamr@2
   174
    struct GE_SuccHiT : applicable
williamr@2
   175
    {
williamr@2
   176
      typedef typename Traits::target_type T ;
williamr@2
   177
      typedef typename Traits::source_type S ;
williamr@2
   178
      typedef typename Traits::argument_type argument_type ;
williamr@2
   179
williamr@2
   180
      static range_check_result apply ( argument_type s )
williamr@2
   181
      {
williamr@2
   182
        return s >= static_cast<S>(bounds<T>::highest()) + static_cast<S>(1.0)
williamr@2
   183
                 ? cPosOverflow : cInRange ;
williamr@2
   184
      }
williamr@2
   185
    } ;
williamr@2
   186
williamr@2
   187
    // s >= Lowest(T) + 0.5 ? cPosgOverflow : cInRange
williamr@2
   188
    //
williamr@2
   189
    template<class Traits>
williamr@2
   190
    struct GT_HalfSuccHiT : applicable
williamr@2
   191
    {
williamr@2
   192
      typedef typename Traits::target_type T ;
williamr@2
   193
      typedef typename Traits::source_type S ;
williamr@2
   194
      typedef typename Traits::argument_type argument_type ;
williamr@2
   195
williamr@2
   196
      static range_check_result apply ( argument_type s )
williamr@2
   197
      {
williamr@2
   198
        return s >= static_cast<S>(bounds<T>::highest()) + static_cast<S>(0.5)
williamr@2
   199
                 ? cPosOverflow : cInRange ;
williamr@2
   200
      }
williamr@2
   201
    } ;
williamr@2
   202
williamr@2
   203
williamr@2
   204
  //--------------------------------------------------------------------------
williamr@2
   205
  //
williamr@2
   206
  // Predicate Combiner.
williamr@2
   207
  //
williamr@2
   208
  // This helper classes are used to possibly combine the range checking logic
williamr@2
   209
  // individually performed by the predicates
williamr@2
   210
  //
williamr@2
   211
  //--------------------------------------------------------------------------
williamr@2
   212
williamr@2
   213
williamr@2
   214
    // Applies both predicates: first 'PredA', and if it equals 'cInRange', 'PredB'
williamr@2
   215
    template<class PredA, class PredB>
williamr@2
   216
    struct applyBoth
williamr@2
   217
    {
williamr@2
   218
      typedef typename PredA::argument_type argument_type ;
williamr@2
   219
williamr@2
   220
      static range_check_result apply ( argument_type s )
williamr@2
   221
      {
williamr@2
   222
        range_check_result r = PredA::apply(s) ;
williamr@2
   223
        if ( r == cInRange )
williamr@2
   224
          r = PredB::apply(s);
williamr@2
   225
        return r ;
williamr@2
   226
      }
williamr@2
   227
    } ;
williamr@2
   228
williamr@2
   229
    template<class PredA, class PredB>
williamr@2
   230
    struct combine
williamr@2
   231
    {
williamr@2
   232
      typedef applyBoth<PredA,PredB> Both ;
williamr@2
   233
      typedef void                   NNone ; // 'None' is defined as a macro in (/usr/X11R6/include/X11/X.h)
williamr@2
   234
williamr@2
   235
      typedef typename PredA::do_apply do_applyA ;
williamr@2
   236
      typedef typename PredB::do_apply do_applyB ;
williamr@2
   237
williamr@2
   238
      typedef typename for_both<do_applyA, do_applyB, Both, PredA, PredB, NNone>::type type ;
williamr@2
   239
    } ;
williamr@2
   240
williamr@2
   241
williamr@2
   242
williamr@2
   243
williamr@2
   244
williamr@2
   245
williamr@2
   246
williamr@2
   247
williamr@2
   248
williamr@2
   249
williamr@2
   250
williamr@2
   251
williamr@2
   252
//--------------------------------------------------------------------------
williamr@2
   253
//                             Range Checker classes.
williamr@2
   254
//
williamr@2
   255
// The following classes are VISIBLE base classes of the user-level converter<> class.
williamr@2
   256
// They supply the optimized 'out_of_range()' and 'validate_range()' static member functions
williamr@2
   257
// visible in the user interface.
williamr@2
   258
//
williamr@2
   259
//--------------------------------------------------------------------------
williamr@2
   260
williamr@2
   261
  // Dummy range checker.
williamr@2
   262
  template<class Traits>
williamr@2
   263
  struct dummy_range_checker
williamr@2
   264
  {
williamr@2
   265
    typedef typename Traits::argument_type argument_type ;
williamr@2
   266
williamr@2
   267
    static range_check_result out_of_range ( argument_type ) { return cInRange ; }
williamr@2
   268
    static void validate_range ( argument_type ) {}
williamr@2
   269
  } ;
williamr@2
   270
williamr@2
   271
  // Generic range checker.
williamr@2
   272
  //
williamr@2
   273
  // All the range checking logic for all possible combinations of source and target
williamr@2
   274
  // can be arranged in terms of one or two predicates, which test overflow on both neg/pos 'sides'
williamr@2
   275
  // of the ranges.
williamr@2
   276
  //
williamr@2
   277
  // These predicates are given here as IsNegOverflow and IsPosOverflow.
williamr@2
   278
  //
williamr@2
   279
  template<class Traits, class IsNegOverflow, class IsPosOverflow, class OverflowHandler>
williamr@2
   280
  struct generic_range_checker
williamr@2
   281
  {
williamr@2
   282
    typedef OverflowHandler overflow_handler ;
williamr@2
   283
williamr@2
   284
    typedef typename Traits::argument_type argument_type ;
williamr@2
   285
williamr@2
   286
    static range_check_result out_of_range ( argument_type s )
williamr@2
   287
    {
williamr@2
   288
      typedef typename combine<IsNegOverflow,IsPosOverflow>::type Predicate ;
williamr@2
   289
williamr@2
   290
      return Predicate::apply(s);
williamr@2
   291
    }
williamr@2
   292
williamr@2
   293
    static void validate_range ( argument_type s )
williamr@2
   294
      { OverflowHandler()( out_of_range(s) ) ; }
williamr@2
   295
  } ;
williamr@2
   296
williamr@2
   297
williamr@2
   298
williamr@2
   299
//--------------------------------------------------------------------------
williamr@2
   300
//
williamr@2
   301
// Selectors for the optimized Range Checker class.
williamr@2
   302
//
williamr@2
   303
//--------------------------------------------------------------------------
williamr@2
   304
williamr@2
   305
  template<class Traits,class OverflowHandler>
williamr@2
   306
  struct GetRC_Sig2Sig_or_Unsig2Unsig
williamr@2
   307
  {
williamr@2
   308
    typedef dummy_range_checker<Traits> Dummy ;
williamr@2
   309
williamr@2
   310
    typedef LT_LoT<Traits> Pred1 ;
williamr@2
   311
    typedef GT_HiT<Traits> Pred2 ;
williamr@2
   312
williamr@2
   313
    typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> Normal ;
williamr@2
   314
williamr@2
   315
    typedef typename Traits::subranged subranged ;
williamr@2
   316
williamr@2
   317
    typedef typename mpl::if_<subranged,Normal,Dummy>::type type ;
williamr@2
   318
  } ;
williamr@2
   319
williamr@2
   320
  template<class Traits, class OverflowHandler>
williamr@2
   321
  struct GetRC_Sig2Unsig
williamr@2
   322
  {
williamr@2
   323
    typedef LT_Zero<Traits> Pred1 ;
williamr@2
   324
    typedef GT_HiT <Traits> Pred2 ;
williamr@2
   325
williamr@2
   326
    typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> ChoiceA ;
williamr@2
   327
williamr@2
   328
    typedef generic_range_checker<Traits,Pred1,non_applicable,OverflowHandler> ChoiceB ;
williamr@2
   329
williamr@2
   330
    typedef typename Traits::target_type T ;
williamr@2
   331
    typedef typename Traits::source_type S ;
williamr@2
   332
williamr@2
   333
    typedef typename subranged_Unsig2Sig<S,T>::type oposite_subranged ;
williamr@2
   334
williamr@2
   335
    typedef typename mpl::not_<oposite_subranged>::type positively_subranged ;
williamr@2
   336
williamr@2
   337
    typedef typename mpl::if_<positively_subranged,ChoiceA,ChoiceB>::type type ;
williamr@2
   338
  } ;
williamr@2
   339
williamr@2
   340
  template<class Traits, class OverflowHandler>
williamr@2
   341
  struct GetRC_Unsig2Sig
williamr@2
   342
  {
williamr@2
   343
    typedef GT_HiT<Traits> Pred1 ;
williamr@2
   344
williamr@2
   345
    typedef generic_range_checker<Traits,non_applicable,Pred1,OverflowHandler> type ;
williamr@2
   346
  } ;
williamr@2
   347
williamr@2
   348
  template<class Traits,class OverflowHandler>
williamr@2
   349
  struct GetRC_Int2Int
williamr@2
   350
  {
williamr@2
   351
    typedef GetRC_Sig2Sig_or_Unsig2Unsig<Traits,OverflowHandler> Sig2SigQ     ;
williamr@2
   352
    typedef GetRC_Sig2Unsig             <Traits,OverflowHandler> Sig2UnsigQ   ;
williamr@2
   353
    typedef GetRC_Unsig2Sig             <Traits,OverflowHandler> Unsig2SigQ   ;
williamr@2
   354
    typedef Sig2SigQ                                             Unsig2UnsigQ ;
williamr@2
   355
williamr@2
   356
    typedef typename Traits::sign_mixture sign_mixture ;
williamr@2
   357
williamr@2
   358
    typedef typename
williamr@2
   359
      for_sign_mixture<sign_mixture,Sig2SigQ,Sig2UnsigQ,Unsig2SigQ,Unsig2UnsigQ>::type
williamr@2
   360
        selector ;
williamr@2
   361
williamr@2
   362
    typedef typename selector::type type ;
williamr@2
   363
  } ;
williamr@2
   364
williamr@2
   365
  template<class Traits>
williamr@2
   366
  struct GetRC_Int2Float
williamr@2
   367
  {
williamr@2
   368
    typedef dummy_range_checker<Traits> type ;
williamr@2
   369
  } ;
williamr@2
   370
williamr@2
   371
  template<class Traits, class OverflowHandler, class Float2IntRounder>
williamr@2
   372
  struct GetRC_Float2Int
williamr@2
   373
  {
williamr@2
   374
    typedef LE_PrevLoT    <Traits> Pred1 ;
williamr@2
   375
    typedef GE_SuccHiT    <Traits> Pred2 ;
williamr@2
   376
    typedef LT_HalfPrevLoT<Traits> Pred3 ;
williamr@2
   377
    typedef GT_HalfSuccHiT<Traits> Pred4 ;
williamr@2
   378
    typedef GT_HiT        <Traits> Pred5 ;
williamr@2
   379
    typedef LT_LoT        <Traits> Pred6 ;
williamr@2
   380
williamr@2
   381
    typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> ToZero    ;
williamr@2
   382
    typedef generic_range_checker<Traits,Pred3,Pred4,OverflowHandler> ToNearest ;
williamr@2
   383
    typedef generic_range_checker<Traits,Pred1,Pred5,OverflowHandler> ToInf     ;
williamr@2
   384
    typedef generic_range_checker<Traits,Pred6,Pred2,OverflowHandler> ToNegInf  ;
williamr@2
   385
williamr@2
   386
    typedef typename Float2IntRounder::round_style round_style ;
williamr@2
   387
williamr@2
   388
    typedef typename for_round_style<round_style,ToZero,ToNearest,ToInf,ToNegInf>::type type ;
williamr@2
   389
  } ;
williamr@2
   390
williamr@2
   391
  template<class Traits, class OverflowHandler>
williamr@2
   392
  struct GetRC_Float2Float
williamr@2
   393
  {
williamr@2
   394
    typedef dummy_range_checker<Traits> Dummy ;
williamr@2
   395
williamr@2
   396
    typedef LT_LoT<Traits> Pred1 ;
williamr@2
   397
    typedef GT_HiT<Traits> Pred2 ;
williamr@2
   398
williamr@2
   399
    typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> Normal ;
williamr@2
   400
williamr@2
   401
    typedef typename Traits::subranged subranged ;
williamr@2
   402
williamr@2
   403
    typedef typename mpl::if_<subranged,Normal,Dummy>::type type ;
williamr@2
   404
  } ;
williamr@2
   405
williamr@2
   406
  template<class Traits, class OverflowHandler, class Float2IntRounder>
williamr@2
   407
  struct GetRC_BuiltIn2BuiltIn
williamr@2
   408
  {
williamr@2
   409
    typedef GetRC_Int2Int<Traits,OverflowHandler>                    Int2IntQ ;
williamr@2
   410
    typedef GetRC_Int2Float<Traits>                                  Int2FloatQ ;
williamr@2
   411
    typedef GetRC_Float2Int<Traits,OverflowHandler,Float2IntRounder> Float2IntQ ;
williamr@2
   412
    typedef GetRC_Float2Float<Traits,OverflowHandler>                Float2FloatQ ;
williamr@2
   413
williamr@2
   414
    typedef typename Traits::int_float_mixture int_float_mixture ;
williamr@2
   415
williamr@2
   416
    typedef typename for_int_float_mixture<int_float_mixture, Int2IntQ, Int2FloatQ, Float2IntQ, Float2FloatQ>::type selector ;
williamr@2
   417
williamr@2
   418
    typedef typename selector::type type ;
williamr@2
   419
  } ;
williamr@2
   420
williamr@2
   421
  template<class Traits, class OverflowHandler, class Float2IntRounder>
williamr@2
   422
  struct GetRC
williamr@2
   423
  {
williamr@2
   424
    typedef GetRC_BuiltIn2BuiltIn<Traits,OverflowHandler,Float2IntRounder> BuiltIn2BuiltInQ ;
williamr@2
   425
williamr@2
   426
    typedef dummy_range_checker<Traits> Dummy ;
williamr@2
   427
williamr@2
   428
    typedef mpl::identity<Dummy> DummyQ ;
williamr@2
   429
williamr@2
   430
    typedef typename Traits::udt_builtin_mixture udt_builtin_mixture ;
williamr@2
   431
williamr@2
   432
    typedef typename for_udt_builtin_mixture<udt_builtin_mixture,BuiltIn2BuiltInQ,DummyQ,DummyQ,DummyQ>::type selector ;
williamr@2
   433
williamr@2
   434
    typedef typename selector::type type ;
williamr@2
   435
  } ;
williamr@2
   436
williamr@2
   437
williamr@2
   438
williamr@2
   439
williamr@2
   440
//--------------------------------------------------------------------------
williamr@2
   441
//                             Converter classes.
williamr@2
   442
//
williamr@2
   443
// The following classes are VISIBLE base classes of the user-level converter<> class.
williamr@2
   444
// They supply the optimized 'nearbyint()' and 'convert()' static member functions
williamr@2
   445
// visible in the user interface.
williamr@2
   446
//
williamr@2
   447
//--------------------------------------------------------------------------
williamr@2
   448
williamr@2
   449
  //
williamr@2
   450
  // Trivial Converter : used when (cv-unqualified) T == (cv-unqualified)  S
williamr@2
   451
  //
williamr@2
   452
  template<class Traits>
williamr@2
   453
  struct trivial_converter_impl : public std::unary_function<  BOOST_DEDUCED_TYPENAME Traits::argument_type
williamr@2
   454
                                                              ,BOOST_DEDUCED_TYPENAME Traits::result_type
williamr@2
   455
                                                            >
williamr@2
   456
                                 ,public dummy_range_checker<Traits>
williamr@2
   457
  {
williamr@2
   458
    typedef Traits traits ;
williamr@2
   459
williamr@2
   460
    typedef typename Traits::source_type   source_type   ;
williamr@2
   461
    typedef typename Traits::argument_type argument_type ;
williamr@2
   462
    typedef typename Traits::result_type   result_type   ;
williamr@2
   463
williamr@2
   464
    static result_type low_level_convert ( argument_type s ) { return s ; }
williamr@2
   465
    static source_type nearbyint         ( argument_type s ) { return s ; }
williamr@2
   466
    static result_type convert           ( argument_type s ) { return s ; }
williamr@2
   467
  } ;
williamr@2
   468
williamr@2
   469
williamr@2
   470
  //
williamr@2
   471
  // Rounding Converter : used for float to integral conversions.
williamr@2
   472
  //
williamr@2
   473
  template<class Traits,class RangeChecker,class RawConverter,class Float2IntRounder>
williamr@2
   474
  struct rounding_converter : public std::unary_function<  BOOST_DEDUCED_TYPENAME Traits::argument_type
williamr@2
   475
                                                          ,BOOST_DEDUCED_TYPENAME Traits::result_type
williamr@2
   476
                                                        >
williamr@2
   477
                             ,public RangeChecker
williamr@2
   478
                             ,public Float2IntRounder
williamr@2
   479
                             ,public RawConverter
williamr@2
   480
  {
williamr@2
   481
    typedef RangeChecker     RangeCheckerBase ;
williamr@2
   482
    typedef Float2IntRounder Float2IntRounderBase ;
williamr@2
   483
    typedef RawConverter     RawConverterBase ;
williamr@2
   484
williamr@2
   485
    typedef Traits traits ;
williamr@2
   486
williamr@2
   487
    typedef typename Traits::source_type   source_type   ;
williamr@2
   488
    typedef typename Traits::argument_type argument_type ;
williamr@2
   489
    typedef typename Traits::result_type   result_type   ;
williamr@2
   490
williamr@2
   491
    static result_type convert ( argument_type s )
williamr@2
   492
    {
williamr@2
   493
      RangeCheckerBase::validate_range(s);
williamr@2
   494
      source_type s1 = Float2IntRounderBase::nearbyint(s);
williamr@2
   495
      return RawConverterBase::low_level_convert(s1);
williamr@2
   496
    }
williamr@2
   497
  } ;
williamr@2
   498
williamr@2
   499
williamr@2
   500
  //
williamr@2
   501
  // Non-Rounding Converter : used for all other conversions.
williamr@2
   502
  //
williamr@2
   503
  template<class Traits,class RangeChecker,class RawConverter>
williamr@2
   504
  struct non_rounding_converter : public std::unary_function< BOOST_DEDUCED_TYPENAME Traits::argument_type
williamr@2
   505
                                                             ,BOOST_DEDUCED_TYPENAME Traits::result_type
williamr@2
   506
                                                           >
williamr@2
   507
                                 ,public RangeChecker
williamr@2
   508
                                 ,public RawConverter
williamr@2
   509
  {
williamr@2
   510
    typedef RangeChecker RangeCheckerBase ;
williamr@2
   511
    typedef RawConverter RawConverterBase ;
williamr@2
   512
williamr@2
   513
    typedef Traits traits ;
williamr@2
   514
williamr@2
   515
    typedef typename Traits::source_type   source_type   ;
williamr@2
   516
    typedef typename Traits::argument_type argument_type ;
williamr@2
   517
    typedef typename Traits::result_type   result_type   ;
williamr@2
   518
williamr@2
   519
    static source_type nearbyint ( argument_type s ) { return s ; }
williamr@2
   520
williamr@2
   521
    static result_type convert ( argument_type s )
williamr@2
   522
    {
williamr@2
   523
      RangeCheckerBase::validate_range(s);
williamr@2
   524
      return RawConverterBase::low_level_convert(s);
williamr@2
   525
    }
williamr@2
   526
  } ;
williamr@2
   527
williamr@2
   528
williamr@2
   529
williamr@2
   530
williamr@2
   531
//--------------------------------------------------------------------------
williamr@2
   532
//
williamr@2
   533
// Selectors for the optimized Converter class.
williamr@2
   534
//
williamr@2
   535
//--------------------------------------------------------------------------
williamr@2
   536
williamr@2
   537
  template<class Traits,class OverflowHandler,class Float2IntRounder,class RawConverter, class UserRangeChecker>
williamr@2
   538
  struct get_non_trivial_converter
williamr@2
   539
  {
williamr@2
   540
    typedef GetRC<Traits,OverflowHandler,Float2IntRounder> InternalRangeCheckerQ ;
williamr@2
   541
williamr@2
   542
    typedef is_same<UserRangeChecker,UseInternalRangeChecker> use_internal_RC ;
williamr@2
   543
williamr@2
   544
    typedef mpl::identity<UserRangeChecker> UserRangeCheckerQ ;
williamr@2
   545
williamr@2
   546
    typedef typename
williamr@2
   547
      mpl::eval_if<use_internal_RC,InternalRangeCheckerQ,UserRangeCheckerQ>::type
williamr@2
   548
        RangeChecker ;
williamr@2
   549
williamr@2
   550
    typedef non_rounding_converter<Traits,RangeChecker,RawConverter>              NonRounding ;
williamr@2
   551
    typedef rounding_converter<Traits,RangeChecker,RawConverter,Float2IntRounder> Rounding ;
williamr@2
   552
williamr@2
   553
    typedef mpl::identity<NonRounding> NonRoundingQ ;
williamr@2
   554
    typedef mpl::identity<Rounding>    RoundingQ    ;
williamr@2
   555
williamr@2
   556
    typedef typename Traits::int_float_mixture int_float_mixture ;
williamr@2
   557
williamr@2
   558
    typedef typename
williamr@2
   559
      for_int_float_mixture<int_float_mixture, NonRoundingQ, NonRoundingQ, RoundingQ, NonRoundingQ>::type
williamr@2
   560
        selector ;
williamr@2
   561
williamr@2
   562
    typedef typename selector::type type ;
williamr@2
   563
  } ;
williamr@2
   564
williamr@2
   565
  template< class Traits
williamr@2
   566
           ,class OverflowHandler
williamr@2
   567
           ,class Float2IntRounder
williamr@2
   568
           ,class RawConverter
williamr@2
   569
           ,class UserRangeChecker
williamr@2
   570
          >
williamr@2
   571
  struct get_converter_impl
williamr@2
   572
  {
williamr@2
   573
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT( 0x0561 ) )
williamr@2
   574
    // bcc55 prefers sometimes template parameters to be explicit local types.
williamr@2
   575
    // (notice that is is illegal to reuse the names like this)
williamr@2
   576
    typedef Traits           Traits ;
williamr@2
   577
    typedef OverflowHandler  OverflowHandler ;
williamr@2
   578
    typedef Float2IntRounder Float2IntRounder ;
williamr@2
   579
    typedef RawConverter     RawConverter ;
williamr@2
   580
    typedef UserRangeChecker UserRangeChecker ;
williamr@2
   581
#endif
williamr@2
   582
williamr@2
   583
    typedef trivial_converter_impl<Traits> Trivial ;
williamr@2
   584
    typedef mpl::identity        <Trivial> TrivialQ ;
williamr@2
   585
williamr@2
   586
    typedef get_non_trivial_converter< Traits
williamr@2
   587
                                      ,OverflowHandler
williamr@2
   588
                                      ,Float2IntRounder
williamr@2
   589
                                      ,RawConverter
williamr@2
   590
                                      ,UserRangeChecker
williamr@2
   591
                                     > NonTrivialQ ;
williamr@2
   592
williamr@2
   593
    typedef typename Traits::trivial trivial ;
williamr@2
   594
williamr@2
   595
    typedef typename mpl::eval_if<trivial,TrivialQ,NonTrivialQ>::type type ;
williamr@2
   596
  } ;
williamr@2
   597
williamr@2
   598
} } } // namespace boost::numeric::convdetail
williamr@2
   599
williamr@2
   600
#endif
williamr@2
   601
williamr@2
   602