summaryrefslogtreecommitdiff
path: root/ace/Truncate.h
blob: 252205f1223597e318e58e9f2179d5539dbf2145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// -*- C++ -*-

//=============================================================================
/**
 * @file Truncate.h
 *
 * $Id$
 *
 * @author Steve Huston  <shuston@riverace.com>
 * @author Ossama Othman <ossama_othman at symantec dot com>
 * @author Russell Mora  <russell_mora at symantec dot com>
 */
//=============================================================================

#ifndef ACE_TRUNCATE_H
#define ACE_TRUNCATE_H

#include /**/ "ace/pre.h"

#include "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Global_Macros.h"
#include "ace/If_Then_Else.h"
#include "ace/Numeric_Limits.h"

#if defined (__BORLANDC__) && __BORLANDC__ <= 0x800
# include "ace/Basic_Types.h"
#endif  /* __BORLANDC__ <= 0x800 */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE_Utils
{

#if !defined (__BORLANDC__) || __BORLANDC__ > 0x800

  template<typename T> struct Sign_Check;

  // Specialize the unsigned signed cases.
  template<> struct Sign_Check<unsigned char>  { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
  template<> struct Sign_Check<unsigned short> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
  template<> struct Sign_Check<unsigned int>   { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
  template<> struct Sign_Check<unsigned long>  { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
#ifndef ACE_LACKS_LONGLONG_T
# ifdef __GNUC__
  // Silence g++ "-pedantic" warnings regarding use of "long long"
  // type.
  __extension__
# endif  /* __GNUC__ */
  template<> struct Sign_Check<unsigned long long> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
#endif  /* !ACE_LACKS_LONGLONG_T */

  // Specialize the signed cases.
  template<> struct Sign_Check<signed char>  { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
  template<> struct Sign_Check<signed short> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
  template<> struct Sign_Check<signed int>   { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
  template<> struct Sign_Check<signed long>  { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
#ifndef ACE_LACKS_LONGLONG_T
# ifdef __GNUC__
  // Silence g++ "-pedantic" warnings regarding use of "long long"
  // type.
  __extension__
# endif  /* __GNUC__ */
  template<> struct Sign_Check<signed long long> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
#endif  /* !ACE_LACKS_LONGLONG_T */

  // -----------------------------------------------------

  /**
   * @struct To_Unsigned
   *
   * @brief Retrieve unsigned counterpart to given type or value.
   *
   * Retrieve unsigned counterpart to given type or value.
   */
  template<typename T> struct To_Unsigned;

  template<>
  struct To_Unsigned<unsigned char>
  {
    typedef unsigned char unsigned_type;

    unsigned_type operator() (unsigned_type x) { return x; }
  };

  template<>
  struct To_Unsigned<unsigned short>
  {
    typedef unsigned short unsigned_type;

    unsigned_type operator() (unsigned_type x) { return x; }
  };

  template<>
  struct To_Unsigned<unsigned int>
  {
    typedef unsigned int unsigned_type;

    unsigned_type operator() (unsigned_type x) { return x; }
  };

  template<>
  struct To_Unsigned<unsigned long>
  {
    typedef unsigned long unsigned_type;

    unsigned_type operator() (unsigned_type x) { return x; }
  };

#ifndef ACE_LACKS_LONGLONG_T
# ifdef __GNUC__
  // Silence g++ "-pedantic" warnings regarding use of "long long"
  // type.
  __extension__
# endif  /* __GNUC__ */
  template<>
  struct To_Unsigned<unsigned long long>
  {
    typedef unsigned long long unsigned_type;

    unsigned_type operator() (unsigned_type x) { return x; }
  };
#endif  /* !ACE_LACKS_LONGLONG_T */

  // ----------------

  template<>
  struct To_Unsigned<signed char>
  {
    typedef signed char   signed_type;
    typedef unsigned char unsigned_type;

    unsigned_type operator() (signed_type x)
    {
      return static_cast<unsigned_type> (x);
    }
  };

  template<>
  struct To_Unsigned<signed short>
  {
    typedef signed short   signed_type;
    typedef unsigned short unsigned_type;

    unsigned_type operator() (signed_type x)
    {
      return static_cast<unsigned_type> (x);
    }
  };

  template<>
  struct To_Unsigned<signed int>
  {
    typedef signed int   signed_type;
    typedef unsigned int unsigned_type;

    unsigned_type operator() (signed_type x)
    {
      return static_cast<unsigned_type> (x);
    }
  };

  template<>
  struct To_Unsigned<signed long>
  {
    typedef signed long   signed_type;
    typedef unsigned long unsigned_type;

    unsigned_type operator() (signed_type x)
    {
      return static_cast<unsigned_type> (x);
    }
  };

#ifndef ACE_LACKS_LONGLONG_T
# ifdef __GNUC__
  // Silence g++ "-pedantic" warnings regarding use of "long long"
  // type.
  __extension__
# endif  /* __GNUC__ */
  template<>
  struct To_Unsigned<signed long long>
  {
    typedef signed long long   signed_type;
    typedef unsigned long long unsigned_type;

    unsigned_type operator() (signed_type x)
    {
      return static_cast<unsigned_type> (x);
    }
  };
#endif  /* !ACE_LACKS_LONGLONG_T */

  // -----------------------------------------------------

  /**
   * @struct Safe_Comparator
   *
   * @brief Conservative comparison of types that may not be safely
   *        promoted and/or converted to each other.
   *
   * The comparison operations provided by this structure perform
   * negative value checking when necessary to prevent wrap-around
   * when explicitly casting to an unsigned type.
   *
   * @internal This structure is not meant for general use.
   */
  template<typename LEFT,
           typename RIGHT,
           bool IS_LEFT_SIGNED,
           bool IS_RIGHT_SIGNED> struct Safe_Comparator;

  // LEFT: signed, RIGHT: unsigned
  template<typename LEFT, typename RIGHT>
  struct Safe_Comparator<LEFT, RIGHT, true, false>
  {
    static bool greater_than (LEFT lhs, RIGHT rhs)
    {
      // Prevent wrap-around when casting to unsigned.
      if (lhs < 0)
        return false;  // since rhs is always positive
      else
        {
          // Implicit promotion of unsigned LEFT and RIGHT types here.
          return To_Unsigned<LEFT>() (lhs) > rhs;
        }
    }
  };

  // LEFT: unsigned, RIGHT: signed
  template<typename LEFT, typename RIGHT>
  struct Safe_Comparator<LEFT, RIGHT, false, true>
  {
    static bool greater_than (LEFT lhs, RIGHT rhs)
    {
      // Prevent wrap-around when casting to unsigned.
      if (rhs < 0)
        return true;  // since lhs is always positive
      else
        {
          // Implicit promotion of unsigned LEFT and RIGHT types here.
          return lhs > To_Unsigned<RIGHT>() (rhs);
        }
    }
  };

  // LEFT: unsigned, RIGHT: unsigned
  template<typename LEFT, typename RIGHT>
  struct Safe_Comparator<LEFT, RIGHT, false, false>
  {
    static bool greater_than (LEFT lhs, RIGHT rhs)
    {
      // Implicit promotion of unsigned LEFT and RIGHT types here.
      return lhs > rhs;
    }
  };

  // LEFT: signed, RIGHT: signed
  template<typename LEFT, typename RIGHT>
  struct Safe_Comparator<LEFT, RIGHT, true, true>
  {
    static bool greater_than (LEFT lhs, RIGHT rhs)
    {
      // Implicit promotion of signed LEFT and RIGHT types here.
      return lhs > rhs;
    }
  };

  // -----------------------------------------------------

  /**
   * @struct Fast_Comparator
   *
   * @brief Quick comparison of types that can be safely promoted
   *        and/or converted to each other.
   *
   * The comparison operations provided by this structure perform no
   * negative value checking, meaning it is not applicable to all
   * types.  Check the value of the @c USABLE enumerator to determine
   * if it applies to the types in question.
   *
   * @internal This structure is not meant for general use.
   */
  template<typename LEFT, typename RIGHT>
  struct Fast_Comparator
  {
    ACE_STATIC_CONSTANT (
      bool,
      USE_LEFT  = ((sizeof (LEFT) > sizeof (RIGHT)
                    && (Sign_Check<LEFT>::is_signed == 1
                        || Sign_Check<RIGHT>::is_signed == 0))

                   // The following is basically the case where LEFT
                   // and RIGHT are the same integral type.
                   || (sizeof (LEFT) == sizeof (RIGHT)
                       // Can't portably do
                       // Sign_Check<LEFT>::is_signed ==
                       // Sign_Check<RIGHT>::is_signed,
                       // i.e. comparison of anonymous enumerations,
                       // without triggering a compiler diagnostic
                       // so expand the comparison.
                       && ((Sign_Check<LEFT>::is_signed == 1
                            && Sign_Check<RIGHT>::is_signed == 1)
                           || (Sign_Check<LEFT>::is_signed == 0
                               && Sign_Check<RIGHT>::is_signed == 0)))));

    ACE_STATIC_CONSTANT (
      bool,
      USE_RIGHT = (sizeof (RIGHT) > sizeof (LEFT)
                   && (Sign_Check<RIGHT>::is_signed == 1
                       || Sign_Check<LEFT>::is_signed == 0)));

    ACE_STATIC_CONSTANT (bool, USABLE = (USE_LEFT || USE_RIGHT));

    typedef typename ACE::If_Then_Else<
      USE_LEFT,
      LEFT,
      typename ACE::If_Then_Else<
        USE_RIGHT,
        RIGHT,
        void>::result_type>::result_type promote_type;

    static bool greater_than (LEFT lhs, RIGHT rhs)
    {
      // The explicit cast is assumed to change the type of rhs without
      // changing its value.
      return
        (static_cast<promote_type> (lhs) > static_cast<promote_type> (rhs));
    }

  };

  // -----------------------------------------------------

  /**
   * @struct Comparator
   *
   * @brief Structure that provides optimal comparison operation for
   *        given types.
   *
   * The comparison operations provided by this structure are chosen
   * at compile time based on the signs and sizes of types being
   * compared.
   * @par
   * Comparisons of values with the same sign or those with types that
   * can be promoted safely are done quickly, without any range
   * checking.
   * @par
   * Comparisons of values of different types that cannot be safely
   * promoted incur an additional check for a negative value to allow
   * the compiler to perform the appropriate implicit unsigned type
   * promotion.
   *
   * @note In general, the operations found in this structure should
   *       not be used to work around compiler diagnostics regarding
   *       comparison of signed and unsigned types.  Verify that your
   *       types are correct before relying on those operations.
   *
   * @internal This structure is not meant for general use.
   */
  template<typename LEFT, typename RIGHT>
  struct Comparator
  {
    typedef typename ACE::If_Then_Else<
      Fast_Comparator<LEFT, RIGHT>::USABLE,
      Fast_Comparator<LEFT, RIGHT>,
      Safe_Comparator<LEFT,
                      RIGHT,
                      Sign_Check<LEFT>::is_signed,
                      Sign_Check<RIGHT>::is_signed> >::result_type comp_type;
  };

  // -----------------------------------------------------

  /**
   * @struct Truncator
   *
   * @brief Truncate value of type @c FROM to value of type @c TO.
   *
   * Truncate a value of type @c FROM to value of type @c TO, if the
   * value is larger than the maximum of value of type @c TO.
   */
  template<typename FROM, typename TO>
  struct Truncator
  {
    ACE_STATIC_CONSTANT (
      bool,
      // max FROM always greater than max TO
      MAX_FROM_GT_MAX_TO = (sizeof(FROM) > sizeof (TO)
                            || (sizeof(FROM) == sizeof (TO)
                                && Sign_Check<FROM>::is_signed == 0)));

    typedef typename ACE::If_Then_Else<
      MAX_FROM_GT_MAX_TO,
      FROM,
      TO>::result_type comp_to_type;

    // Take advantage of knowledge that we're casting a positive value
    // to a type large enough to hold it so that we can bypass
    // negative value checks at compile-time.  Otherwise fallback on
    // the safer comparison.
    typedef typename ACE::If_Then_Else<
      MAX_FROM_GT_MAX_TO,
      Fast_Comparator<FROM, comp_to_type>,
      typename Comparator<FROM, comp_to_type>::comp_type>::result_type comparator;

    /// Truncate a value of type @c FROM to value of type @c TO, if
    /// the value is larger than the maximum of value of type @c TO.
    TO operator() (FROM val)
    {
      return
        (comparator::greater_than (val, ACE_Numeric_Limits<TO>::max ())
         ? ACE_Numeric_Limits<TO>::max ()
         : static_cast<TO> (val));
    }

  };

  // Partial specialization for the case where the types are the same.
  // No truncation is necessary.
  template<typename T>
  struct Truncator<T, T>
  {
    T operator() (T val)
    {
      return val;
    }
  };

  // -----------------------------------------------------
  /**
   * @struct Noop_Truncator
   *
   * @brief No-op truncation.
   *
   * This structure/functor performs no truncation since it assumes
   * that @c sizeof(FROM) @c < @c sizeof(TO), meaning that
   * @c numeric_limits<FROM>::max() @c < @c numeric_limits<TO>::max().
   */
  template<typename FROM, typename TO>
  struct Noop_Truncator
  {
    TO operator() (FROM val)
    {
      return static_cast<TO> (val);
    }
  };
  // -----------------------------------------------------

  /**
   * @class Truncate
   *
   * @brief Helper function to truncate an integral value to the
   *        maximum  value of the given type.
   *
   *        Very useful since ACE methods return @c int very often and
   *        the value's source is often a different-size integral
   *        type, such as @c size_t.  This function hides the
   *        truncation logic and resolves compiler diagnostics.
   *
   * @internal Internal use only.
   */
  template<typename TO, typename FROM>
  inline TO Truncate (FROM val)
  {
    // If the size of FROM is less than the size of TO, "val" will
    // never be greater than the maximum "TO" value, so there is no
    // need to attempt to truncate.
    typedef typename ACE::If_Then_Else<
      (sizeof (FROM) < sizeof (TO)),
      Noop_Truncator<FROM, TO>,
      Truncator<FROM, TO> >::result_type truncator;

    return truncator() (val);
  }

#else

  // Borland can't handle the template meta-programming above so
  // provide explicit specializations for a few types.  More will be
  // added if necessary.

  /**
   * @deprecated Borland ACE_Utils::Truncator<> specializations should
   *             be removed.
   */

  template<typename FROM, typename TO> struct Truncator;

  // Partial specialization for the case where the types are the same.
  // No truncation is necessary.
  template<typename T>
  struct Truncator<T, T>
  {
    T operator() (T val)
    {
      return val;
    }
  };

  //----------------------------------------------------------
  // sizeof(FROM) >  sizeof(TO)
  //----------------------------------------------------------
  template<>
  struct Truncator<ACE_INT64, ACE_INT32>
  {
    ACE_INT32 operator() (ACE_INT64 val)
    {
      return
        (val > ACE_Numeric_Limits<ACE_INT32>::max ()
         ? ACE_Numeric_Limits<ACE_INT32>::max ()
         : static_cast<ACE_INT32> (val));
    }
  };

  template<>
  struct Truncator<ACE_INT64, ACE_UINT32>
  {
    ACE_UINT32 operator() (ACE_INT64 val)
    {
      return
        (val > static_cast<ACE_INT64> (ACE_Numeric_Limits<ACE_UINT32>::max ())
         ? ACE_Numeric_Limits<ACE_UINT32>::max ()
         : static_cast<ACE_UINT32> (val));
    }
  };

  template<>
  struct Truncator<ACE_UINT64, ACE_UINT32>
  {
    ACE_UINT32 operator() (ACE_UINT64 val)
    {
      return
        (val > ACE_Numeric_Limits<ACE_UINT32>::max ()
         ? ACE_Numeric_Limits<ACE_UINT32>::max ()
         : static_cast<ACE_UINT32> (val));
    }
  };

  template<>
  struct Truncator<ACE_UINT64, ACE_INT32>
  {
    ACE_INT32 operator() (ACE_UINT64 val)
    {
      return
        (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<ACE_INT32>::max ())
         ? ACE_Numeric_Limits<ACE_INT32>::max ()
         : static_cast<ACE_INT32> (val));
    }
  };

  //----------------------------------------------------------
  // sizeof(FROM) == sizeof(TO)
  //----------------------------------------------------------

  template<>
  struct Truncator<ACE_INT32, ACE_UINT32>
  {
    ACE_UINT32 operator() (ACE_INT32 val)
    {
      return static_cast<ACE_UINT32> (val);
    }
  };

  template<>
  struct Truncator<ACE_UINT32, ACE_INT32>
  {
    ACE_INT32 operator() (ACE_UINT32 val)
    {
      return
        (val > static_cast<ACE_UINT32> (ACE_Numeric_Limits<ACE_INT32>::max ())
         ? ACE_Numeric_Limits<ACE_INT32>::max ()
         : static_cast<ACE_INT32> (val));
    }
  };

  template<>
  struct Truncator<ACE_INT64, ACE_UINT64>
  {
    ACE_UINT64 operator() (ACE_INT64 val)
    {
      return static_cast<ACE_UINT64> (val);
    }
  };

  template<>
  struct Truncator<ACE_UINT64, ACE_INT64>
  {
    ACE_INT64 operator() (ACE_UINT64 val)
    {
      return
        (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<ACE_INT64>::max ())
         ? ACE_Numeric_Limits<ACE_INT64>::max ()
         : static_cast<ACE_INT64> (val));
    }
  };

  //----------------------------------------------------------
  // sizeof(FROM) <  sizeof(TO)
  //----------------------------------------------------------
  template<>
  struct Truncator<ACE_INT32, ACE_INT64>
  {
    ACE_INT64 operator() (ACE_INT32 val)
    {
      return static_cast<ACE_INT64> (val);
    }
  };

  template<>
  struct Truncator<ACE_INT32, ACE_UINT64>
  {
    ACE_UINT64 operator() (ACE_INT32 val)
    {
      return static_cast<ACE_UINT64> (val);
    }
  };

  template<>
  struct Truncator<ACE_UINT32, ACE_UINT64>
  {
    ACE_UINT64 operator() (ACE_UINT32 val)
    {
      return static_cast<ACE_UINT64> (val);
    }
  };

  template<>
  struct Truncator<ACE_UINT32, ACE_INT64>
  {
    ACE_INT64 operator() (ACE_UINT32 val)
    {
      return static_cast<ACE_INT64> (val);
    }
  };

  // -------------------------------------
  template<typename TO, typename FROM>
  inline TO Truncate (FROM val)
  {
    return Truncator<FROM, TO>() (val);
  }

#endif  /* !__BORLANDC__ || __BORLANDC__ > 0x800 */

} // namespace ACE_Utils

ACE_END_VERSIONED_NAMESPACE_DECL

#include /**/ "ace/post.h"

#endif /* ACE_TRUNCATE_H*/