3 //=============================================================================
7 * $Id: Truncate.h 82623 2008-08-13 14:41:09Z johnnyw $
9 * @author Steve Huston <shuston@riverace.com>
10 * @author Ossama Othman <ossama_othman@symantec.com>
11 * @author Russell Mora <russell_mora@symantec.com>
13 //=============================================================================
15 #ifndef ACE_TRUNCATE_H
16 #define ACE_TRUNCATE_H
18 #include /**/ "ace/pre.h"
20 #include "ace/config-all.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #include "ace/Global_Macros.h"
27 #include "ace/If_Then_Else.h"
28 #include "ace/Numeric_Limits.h"
30 #if defined (ACE_LACKS_LONGLONG_T) \
31 || defined (__BORLANDC__) && __BORLANDC__ < 0x590
32 # include "ace/Basic_Types.h"
33 #endif /* ACE_LACKS_LONGLONG_T || __BORLANDC__ < 0x590 */
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
40 #if !defined (__BORLANDC__) || __BORLANDC__ >= 0x590
42 template<typename T
> struct Sign_Check
;
44 // Specialize the unsigned signed cases.
45 template<> struct Sign_Check
<unsigned char> { ACE_STATIC_CONSTANT (bool, is_signed
= 0); };
46 template<> struct Sign_Check
<unsigned short> { ACE_STATIC_CONSTANT (bool, is_signed
= 0); };
47 template<> struct Sign_Check
<unsigned int> { ACE_STATIC_CONSTANT (bool, is_signed
= 0); };
48 template<> struct Sign_Check
<unsigned long> { ACE_STATIC_CONSTANT (bool, is_signed
= 0); };
49 #if !(defined(ACE_LACKS_LONGLONG_T) || defined(ACE_LACKS_UNSIGNEDLONGLONG_T))
51 // Silence g++ "-pedantic" warnings regarding use of "long long"
54 # endif /* __GNUC__ */
55 template<> struct Sign_Check
<unsigned long long> { ACE_STATIC_CONSTANT (bool, is_signed
= 0); };
57 template<> struct Sign_Check
<ACE_U_LongLong
> { ACE_STATIC_CONSTANT (bool, is_signed
= 0); };
58 #endif /* !ACE_LACKS_LONGLONG_T */
60 // Specialize the signed cases.
61 template<> struct Sign_Check
<signed char> { ACE_STATIC_CONSTANT (bool, is_signed
= 1); };
62 template<> struct Sign_Check
<signed short> { ACE_STATIC_CONSTANT (bool, is_signed
= 1); };
63 template<> struct Sign_Check
<signed int> { ACE_STATIC_CONSTANT (bool, is_signed
= 1); };
64 template<> struct Sign_Check
<signed long> { ACE_STATIC_CONSTANT (bool, is_signed
= 1); };
65 #ifndef ACE_LACKS_LONGLONG_T
67 // Silence g++ "-pedantic" warnings regarding use of "long long"
70 # endif /* __GNUC__ */
71 template<> struct Sign_Check
<signed long long> { ACE_STATIC_CONSTANT (bool, is_signed
= 1); };
72 #endif /* !ACE_LACKS_LONGLONG_T */
74 // -----------------------------------------------------
79 * @brief Retrieve unsigned counterpart to given type or value.
81 * Retrieve unsigned counterpart to given type or value.
83 template<typename T
> struct To_Unsigned
;
86 struct To_Unsigned
<unsigned char>
88 typedef unsigned char unsigned_type
;
90 unsigned_type
operator() (unsigned_type x
) { return x
; }
94 struct To_Unsigned
<unsigned short>
96 typedef unsigned short unsigned_type
;
98 unsigned_type
operator() (unsigned_type x
) { return x
; }
102 struct To_Unsigned
<unsigned int>
104 typedef unsigned int unsigned_type
;
106 unsigned_type
operator() (unsigned_type x
) { return x
; }
110 struct To_Unsigned
<unsigned long>
112 typedef unsigned long unsigned_type
;
114 unsigned_type
operator() (unsigned_type x
) { return x
; }
117 #if !(defined(ACE_LACKS_LONGLONG_T) || defined(ACE_LACKS_UNSIGNEDLONGLONG_T))
119 // Silence g++ "-pedantic" warnings regarding use of "long long"
122 # endif /* __GNUC__ */
124 struct To_Unsigned
<unsigned long long>
126 typedef unsigned long long unsigned_type
;
128 unsigned_type
operator() (unsigned_type x
) { return x
; }
132 struct To_Unsigned
<ACE_U_LongLong
>
134 typedef ACE_U_LongLong unsigned_type
;
136 unsigned_type
operator() (unsigned_type x
) { return x
; }
138 #endif /* !ACE_LACKS_LONGLONG_T */
143 struct To_Unsigned
<signed char>
145 typedef signed char signed_type
;
146 typedef unsigned char unsigned_type
;
148 unsigned_type
operator() (signed_type x
)
150 return static_cast<unsigned_type
> (x
);
155 struct To_Unsigned
<signed short>
157 typedef signed short signed_type
;
158 typedef unsigned short unsigned_type
;
160 unsigned_type
operator() (signed_type x
)
162 return static_cast<unsigned_type
> (x
);
167 struct To_Unsigned
<signed int>
169 typedef signed int signed_type
;
170 typedef unsigned int unsigned_type
;
172 unsigned_type
operator() (signed_type x
)
174 return static_cast<unsigned_type
> (x
);
179 struct To_Unsigned
<signed long>
181 typedef signed long signed_type
;
182 typedef unsigned long unsigned_type
;
184 unsigned_type
operator() (signed_type x
)
186 return static_cast<unsigned_type
> (x
);
190 #if !(defined(ACE_LACKS_LONGLONG_T) || defined(ACE_LACKS_UNSIGNEDLONGLONG_T))
192 // Silence g++ "-pedantic" warnings regarding use of "long long"
195 # endif /* __GNUC__ */
197 struct To_Unsigned
<signed long long>
199 typedef signed long long signed_type
;
200 typedef unsigned long long unsigned_type
;
202 unsigned_type
operator() (signed_type x
)
204 return static_cast<unsigned_type
> (x
);
207 #endif /* !ACE_LACKS_LONGLONG_T */
209 // -----------------------------------------------------
212 * @struct Safe_Comparator
214 * @brief Conservative comparison of types that may not be safely
215 * promoted and/or converted to each other.
217 * The comparison operations provided by this structure perform
218 * negative value checking when necessary to prevent wrap-around
219 * when explicitly casting to an unsigned type.
221 * @internal This structure is not meant for general use.
223 template<typename LEFT
,
226 bool IS_RIGHT_SIGNED
> struct Safe_Comparator
;
228 // LEFT: signed, RIGHT: unsigned
229 template<typename LEFT
, typename RIGHT
>
230 struct Safe_Comparator
<LEFT
, RIGHT
, true, false>
232 static bool greater_than (LEFT lhs
, RIGHT rhs
)
234 // Prevent wrap-around when casting to unsigned.
236 return false; // since rhs is always positive
239 // Implicit promotion of unsigned LEFT and RIGHT types here.
240 return To_Unsigned
<LEFT
>() (lhs
) > rhs
;
245 // LEFT: unsigned, RIGHT: signed
246 template<typename LEFT
, typename RIGHT
>
247 struct Safe_Comparator
<LEFT
, RIGHT
, false, true>
249 static bool greater_than (LEFT lhs
, RIGHT rhs
)
251 // Prevent wrap-around when casting to unsigned.
253 return true; // since lhs is always positive
256 // Implicit promotion of unsigned LEFT and RIGHT types here.
257 return lhs
> To_Unsigned
<RIGHT
>() (rhs
);
262 // LEFT: unsigned, RIGHT: unsigned
263 template<typename LEFT
, typename RIGHT
>
264 struct Safe_Comparator
<LEFT
, RIGHT
, false, false>
266 static bool greater_than (LEFT lhs
, RIGHT rhs
)
268 // Implicit promotion of unsigned LEFT and RIGHT types here.
273 // LEFT: signed, RIGHT: signed
274 template<typename LEFT
, typename RIGHT
>
275 struct Safe_Comparator
<LEFT
, RIGHT
, true, true>
277 static bool greater_than (LEFT lhs
, RIGHT rhs
)
279 // Implicit promotion of signed LEFT and RIGHT types here.
284 // -----------------------------------------------------
287 * @struct Fast_Comparator
289 * @brief Quick comparison of types that can be safely promoted
290 * and/or converted to each other.
292 * The comparison operations provided by this structure perform no
293 * negative value checking, meaning it is not applicable to all
294 * types. Check the value of the @c USABLE enumerator to determine
295 * if it applies to the types in question.
297 * @internal This structure is not meant for general use.
299 template<typename LEFT
, typename RIGHT
>
300 struct Fast_Comparator
302 ACE_STATIC_CONSTANT (
304 USE_LEFT
= ((sizeof (LEFT
) > sizeof (RIGHT
)
305 && (Sign_Check
<LEFT
>::is_signed
== 1
306 || Sign_Check
<RIGHT
>::is_signed
== 0))
308 // The following is basically the case where LEFT
309 // and RIGHT are the same integral type.
310 || (sizeof (LEFT
) == sizeof (RIGHT
)
312 // Sign_Check<LEFT>::is_signed ==
313 // Sign_Check<RIGHT>::is_signed,
314 // i.e. comparison of anonymous enumerations,
315 // without triggering a compiler diagnostic
316 // so expand the comparison.
317 && ((Sign_Check
<LEFT
>::is_signed
== 1
318 && Sign_Check
<RIGHT
>::is_signed
== 1)
319 || (Sign_Check
<LEFT
>::is_signed
== 0
320 && Sign_Check
<RIGHT
>::is_signed
== 0)))));
322 ACE_STATIC_CONSTANT (
324 USE_RIGHT
= (sizeof (RIGHT
) > sizeof (LEFT
)
325 && (Sign_Check
<RIGHT
>::is_signed
== 1
326 || Sign_Check
<LEFT
>::is_signed
== 0)));
328 ACE_STATIC_CONSTANT (bool, USABLE
= (USE_LEFT
|| USE_RIGHT
));
330 typedef typename
ACE::If_Then_Else
<
333 typename
ACE::If_Then_Else
<
336 void>::result_type
>::result_type promote_type
;
338 static bool greater_than (LEFT lhs
, RIGHT rhs
)
340 // The explicit cast is assumed to change the type of rhs without
341 // changing its value.
343 (static_cast<promote_type
> (lhs
) > static_cast<promote_type
> (rhs
));
348 // -----------------------------------------------------
353 * @brief Structure that provides optimal comparison operation for
356 * The comparison operations provided by this structure are chosen
357 * at compile time based on the signs and sizes of types being
360 * Comparisons of values with the same sign or those with types that
361 * can be promoted safely are done quickly, without any range
364 * Comparisons of values of different types that cannot be safely
365 * promoted incur an additional check for a negative value to allow
366 * the compiler to perform the appropriate implicit unsigned type
369 * @note In general, the operations found in this structure should
370 * not be used to work around compiler diagnostics regarding
371 * comparison of signed and unsigned types. Verify that your
372 * types are correct before relying on those operations.
374 * @internal This structure is not meant for general use.
376 template<typename LEFT
, typename RIGHT
>
379 typedef typename
ACE::If_Then_Else
<
380 Fast_Comparator
<LEFT
, RIGHT
>::USABLE
,
381 Fast_Comparator
<LEFT
, RIGHT
>,
382 Safe_Comparator
<LEFT
,
384 Sign_Check
<LEFT
>::is_signed
,
385 Sign_Check
<RIGHT
>::is_signed
> >::result_type comp_type
;
388 // -----------------------------------------------------
393 * @brief Truncate value of type @c FROM to value of type @c TO.
395 * Truncate a value of type @c FROM to value of type @c TO, if the
396 * value is larger than the maximum of value of type @c TO.
398 template<typename FROM
, typename TO
>
401 ACE_STATIC_CONSTANT (
403 // max FROM always greater than max TO
404 MAX_FROM_GT_MAX_TO
= (sizeof(FROM
) > sizeof (TO
)
405 || (sizeof(FROM
) == sizeof (TO
)
406 && Sign_Check
<FROM
>::is_signed
== 0)));
408 typedef typename
ACE::If_Then_Else
<
411 TO
>::result_type comp_to_type
;
413 // Take advantage of knowledge that we're casting a positive value
414 // to a type large enough to hold it so that we can bypass
415 // negative value checks at compile-time. Otherwise fallback on
416 // the safer comparison.
417 typedef typename
ACE::If_Then_Else
<
419 Fast_Comparator
<FROM
, comp_to_type
>,
420 typename Comparator
<FROM
, comp_to_type
>::comp_type
>::result_type comparator
;
422 /// Truncate a value of type @c FROM to value of type @c TO, if
423 /// the value is larger than the maximum of value of type @c TO.
424 TO
operator() (FROM val
)
427 (comparator::greater_than (val
, ACE_Numeric_Limits
<TO
>::max ())
428 ? ACE_Numeric_Limits
<TO
>::max ()
429 : static_cast<TO
> (val
));
434 // Partial specialization for the case where the types are the same.
435 // No truncation is necessary.
437 struct Truncator
<T
, T
>
446 #if defined (ACE_LACKS_LONGLONG_T) || defined (ACE_LACKS_UNSIGNEDLONGLONG_T)
447 // Partial specialization for the case where we're casting from
448 // ACE_U_LongLong to a smaller integer. We assume that we're always
449 // truncating from ACE_U_LongLong to a smaller type. The partial
450 // specialization above handles the case where both the FROM and TO
451 // types are ACE_U_LongLong.
452 template<typename TO
>
453 struct Truncator
<ACE_U_LongLong
, TO
>
455 TO
operator() (ACE_U_LongLong
const & val
)
457 // If val less than or equal to ACE_Numeric_Limits<TO>::max(),
458 // val.lo() must be less than or equal to
459 // ACE_Numeric_Limits<TO>::max (), as well.
461 (val
> ACE_Numeric_Limits
<TO
>::max ()
462 ? ACE_Numeric_Limits
<TO
>::max ()
463 : static_cast<TO
> (val
.lo ()));
466 #endif /* ACE_LACKS_LONGLONG_T || ACE_LACKS_UNSIGNEDLONGLONG_T */
468 // -----------------------------------------------------
470 * @struct Noop_Truncator
472 * @brief No-op truncation.
474 * This structure/functor performs no truncation since it assumes
475 * that @c sizeof(FROM) @c < @c sizeof(TO), meaning that
476 * @c numeric_limits<FROM>::max() @c < @c numeric_limits<TO>::max().
478 template<typename FROM
, typename TO
>
479 struct Noop_Truncator
481 TO
operator() (FROM val
)
483 return static_cast<TO
> (val
);
486 // -----------------------------------------------------
489 * @class truncate_cast
491 * @brief Helper function to truncate an integral value to the
492 * maximum value of the given type.
494 * Very useful since ACE methods return @c int very often and
495 * the value's source is often a different-size integral
496 * type, such as @c size_t. This function hides the
497 * truncation logic and resolves compiler diagnostics.
499 * @internal Internal use only.
501 template<typename TO
, typename FROM
>
502 inline TO
truncate_cast (FROM val
)
504 // If the size of FROM is less than the size of TO, "val" will
505 // never be greater than the maximum "TO" value, so there is no
506 // need to attempt to truncate.
507 typedef typename
ACE::If_Then_Else
<
508 (sizeof (FROM
) < sizeof (TO
)),
509 Noop_Truncator
<FROM
, TO
>,
510 Truncator
<FROM
, TO
> >::result_type truncator
;
512 return truncator() (val
);
517 // Borland can't handle the template meta-programming above so
518 // provide explicit specializations for a few types. More will be
519 // added if necessary.
522 * @deprecated Borland ACE_Utils::Truncator<> specializations should
526 template<typename FROM
, typename TO
> struct Truncator
;
528 //----------------------------------------------------------
529 // sizeof(FROM) > sizeof(TO)
530 //----------------------------------------------------------
533 struct Truncator
<ACE_INT32
, ACE_INT8
>
535 ACE_INT8
operator() (ACE_INT32 val
)
538 (val
> ACE_Numeric_Limits
<ACE_INT8
>::max ()
539 ? ACE_Numeric_Limits
<ACE_INT8
>::max ()
540 : static_cast<ACE_INT8
> (val
));
545 struct Truncator
<ACE_UINT32
, ACE_UINT8
>
547 ACE_UINT8
operator() (ACE_UINT32 val
)
550 (val
> static_cast<ACE_UINT32
> (ACE_Numeric_Limits
<ACE_UINT8
>::max ())
551 ? ACE_Numeric_Limits
<ACE_UINT8
>::max ()
552 : static_cast<ACE_UINT8
> (val
));
557 struct Truncator
<ACE_INT32
, ACE_UINT8
>
559 ACE_UINT8
operator() (ACE_INT32 val
)
562 (val
> static_cast<ACE_INT32
> (ACE_Numeric_Limits
<ACE_UINT8
>::max ())
563 ? ACE_Numeric_Limits
<ACE_UINT8
>::max ()
564 : static_cast<ACE_UINT8
> (val
));
569 struct Truncator
<ACE_UINT32
, ACE_INT8
>
571 ACE_INT8
operator() (ACE_UINT32 val
)
574 (val
> static_cast<ACE_UINT32
> (ACE_Numeric_Limits
<ACE_INT8
>::max ())
575 ? ACE_Numeric_Limits
<ACE_INT8
>::max ()
576 : static_cast<ACE_INT8
> (val
));
580 #if defined (ACE_SIZEOF_LONG) && ACE_SIZEOF_LONG < 8
582 struct Truncator
<ACE_INT64
, signed long>
584 signed long operator() (ACE_INT64 val
)
587 (val
> ACE_Numeric_Limits
<signed long>::max ()
588 ? ACE_Numeric_Limits
<signed long>::max ()
589 : static_cast<signed long> (val
));
594 struct Truncator
<ACE_INT64
, unsigned long>
596 unsigned long operator() (ACE_INT64 val
)
599 (val
> static_cast<ACE_INT64
> (ACE_Numeric_Limits
<unsigned long>::max ())
600 ? ACE_Numeric_Limits
<unsigned long>::max ()
601 : static_cast<unsigned long> (val
));
606 struct Truncator
<ACE_UINT64
, unsigned long>
608 unsigned long operator() (ACE_UINT64 val
)
611 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<unsigned long>::max ())
612 ? ACE_Numeric_Limits
<unsigned long>::max ()
613 : static_cast<unsigned long> (val
));
618 struct Truncator
<ACE_UINT64
, signed long>
620 signed long operator() (ACE_UINT64 val
)
623 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<signed long>::max ())
624 ? ACE_Numeric_Limits
<signed long>::max ()
625 : static_cast<signed long> (val
));
630 struct Truncator
<const ACE_UINT64
, signed long>
632 signed long operator() (const ACE_UINT64 val
)
635 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<signed long>::max ())
636 ? ACE_Numeric_Limits
<signed long>::max ()
637 : static_cast<signed long> (val
));
641 #endif /* ACE_SIZEOF_LONG < 8 */
643 #if defined (ACE_SIZEOF_INT) && ACE_SIZEOF_INT < 8
645 struct Truncator
<ACE_INT64
, signed int>
647 ACE_INT32
operator() (ACE_INT64 val
)
650 (val
> ACE_Numeric_Limits
<signed int>::max ()
651 ? ACE_Numeric_Limits
<signed int>::max ()
652 : static_cast<signed int> (val
));
657 struct Truncator
<ACE_INT64
, unsigned int>
659 ACE_UINT32
operator() (ACE_INT64 val
)
662 (val
> static_cast<ACE_INT64
> (ACE_Numeric_Limits
<unsigned int>::max ())
663 ? ACE_Numeric_Limits
<unsigned int>::max ()
664 : static_cast<unsigned int> (val
));
669 struct Truncator
<ACE_UINT64
, unsigned int>
671 ACE_UINT32
operator() (ACE_UINT64 val
)
674 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<unsigned int>::max ())
675 ? ACE_Numeric_Limits
<unsigned int>::max ()
676 : static_cast<unsigned int> (val
));
681 struct Truncator
<ACE_UINT64
, signed int>
683 signed int operator() (ACE_UINT64 val
)
686 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<signed int>::max ())
687 ? ACE_Numeric_Limits
<signed int>::max ()
688 : static_cast<signed int> (val
));
692 #endif /* ACE_SIZEOF_INT < 8 */
694 //----------------------------------------------------------
695 // sizeof(FROM) == sizeof(TO)
696 //----------------------------------------------------------
699 struct Truncator
<unsigned int, char>
701 char operator() (unsigned int val
)
703 return static_cast<char> (val
);
708 struct Truncator
<unsigned int, wchar_t>
710 wchar_t operator() (unsigned int val
)
712 return static_cast<wchar_t> (val
);
717 struct Truncator
<signed int, unsigned int>
719 unsigned int operator() (signed int val
)
721 return static_cast<unsigned int> (val
);
726 struct Truncator
<unsigned int, signed int>
728 signed int operator() (unsigned int val
)
731 (val
> static_cast<unsigned int> (ACE_Numeric_Limits
<signed int>::max ())
732 ? ACE_Numeric_Limits
<signed int>::max ()
733 : static_cast<signed int> (val
));
738 struct Truncator
<const unsigned int, signed int>
740 signed int operator() (const unsigned int val
)
743 (val
> static_cast<unsigned int> (ACE_Numeric_Limits
<signed int>::max ())
744 ? ACE_Numeric_Limits
<signed int>::max ()
745 : static_cast<signed int> (val
));
750 struct Truncator
<signed long, unsigned long>
752 unsigned long operator() (signed long val
)
754 return static_cast<unsigned long> (val
);
759 struct Truncator
<unsigned long, signed long>
761 signed long operator() (unsigned long val
)
764 (val
> static_cast<unsigned long> (ACE_Numeric_Limits
<signed long>::max ())
765 ? ACE_Numeric_Limits
<signed long>::max ()
766 : static_cast<signed long> (val
));
770 #if defined (ACE_SIZEOF_INT) && defined (ACE_SIZEOF_LONG) \
771 && ACE_SIZEOF_INT == ACE_SIZEOF_LONG
774 struct Truncator
<signed int, unsigned long>
776 unsigned long operator() (signed int val
)
778 return static_cast<unsigned long> (val
);
783 struct Truncator
<signed int, signed long>
785 signed long operator() (signed int val
)
787 return static_cast<signed long> (val
);
792 struct Truncator
<unsigned long, signed int>
794 signed int operator() (unsigned long val
)
797 (val
> static_cast<unsigned long> (ACE_Numeric_Limits
<signed int>::max ())
798 ? ACE_Numeric_Limits
<signed int>::max ()
799 : static_cast<signed int> (val
));
804 struct Truncator
<signed long, signed int>
806 signed int operator() (signed long val
)
808 return static_cast<signed int> (val
);
809 // This code causes asserts and compiler crashes with BCB6 Static and
812 // (val > static_cast<signed long> (ACE_Numeric_Limits<signed int>::max ())
813 // ? ACE_Numeric_Limits<signed int>::max ()
814 // : static_cast<signed int> (val));
819 struct Truncator
<signed long, unsigned int>
821 unsigned int operator() (signed long val
)
823 return static_cast<unsigned int> (val
);
828 struct Truncator
<const signed long, unsigned int>
830 unsigned int operator() (const signed long val
)
832 return static_cast<unsigned int> (val
);
838 struct Truncator
<unsigned int, signed long>
840 signed long operator() (unsigned int val
)
843 (val
> static_cast<unsigned int> (ACE_Numeric_Limits
<signed long>::max ())
844 ? ACE_Numeric_Limits
<signed long>::max ()
845 : static_cast<signed long> (val
));
849 #endif /* ACE_SIZEOF_INT == ACE_SIZEOF_LONG */
852 struct Truncator
<ACE_INT64
, ACE_UINT64
>
854 ACE_UINT64
operator() (ACE_INT64 val
)
856 return static_cast<ACE_UINT64
> (val
);
861 struct Truncator
<ACE_UINT64
, ACE_INT64
>
863 ACE_INT64
operator() (ACE_UINT64 val
)
866 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<ACE_INT64
>::max ())
867 ? ACE_Numeric_Limits
<ACE_INT64
>::max ()
868 : static_cast<ACE_INT64
> (val
));
872 //----------------------------------------------------------
873 // sizeof(FROM) < sizeof(TO)
874 //----------------------------------------------------------
877 struct Truncator
<ACE_INT8
, ACE_INT32
>
879 ACE_INT32
operator() (ACE_INT8 val
)
881 return static_cast<ACE_INT32
> (val
);
886 struct Truncator
<ACE_UINT8
, ACE_UINT32
>
888 ACE_UINT32
operator() (ACE_UINT8 val
)
890 return static_cast<ACE_UINT32
> (val
);
895 struct Truncator
<ACE_UINT8
, ACE_INT32
>
897 ACE_INT32
operator() (ACE_UINT8 val
)
899 return static_cast<ACE_INT32
> (val
);
904 struct Truncator
<ACE_INT8
, ACE_UINT32
>
906 ACE_UINT32
operator() (ACE_INT8 val
)
908 return static_cast<ACE_UINT32
> (val
);
912 #if defined (ACE_SIZEOF_LONG) && ACE_SIZEOF_LONG < 8
914 struct Truncator
<signed long, ACE_INT64
>
916 ACE_INT64
operator() (signed long val
)
918 return static_cast<ACE_INT64
> (val
);
923 struct Truncator
<signed long, ACE_UINT64
>
925 ACE_UINT64
operator() (signed long val
)
927 return static_cast<ACE_UINT64
> (val
);
932 struct Truncator
<const signed long, ACE_UINT64
>
934 ACE_UINT64
operator() (const signed long val
)
937 (val
> static_cast<ACE_UINT64
> (ACE_Numeric_Limits
<signed long>::max ())
938 ? ACE_Numeric_Limits
<signed long>::max ()
939 : static_cast<signed long> (val
));
944 struct Truncator
<unsigned long, ACE_UINT64
>
946 ACE_UINT64
operator() (unsigned long val
)
948 return static_cast<ACE_UINT64
> (val
);
953 struct Truncator
<unsigned long, ACE_INT64
>
955 ACE_INT64
operator() (unsigned long val
)
957 return static_cast<ACE_INT64
> (val
);
960 #endif /* ACE_SIZEOF_LONG < 8 */
962 #if defined (ACE_SIZEOF_INT) && ACE_SIZEOF_INT < 8
964 struct Truncator
<signed int, ACE_INT64
>
966 ACE_INT64
operator() (signed int val
)
968 return static_cast<ACE_INT64
> (val
);
973 struct Truncator
<signed int, ACE_UINT64
>
975 ACE_UINT64
operator() (signed int val
)
977 return static_cast<ACE_UINT64
> (val
);
982 struct Truncator
<unsigned int, ACE_UINT64
>
984 ACE_UINT64
operator() (unsigned int val
)
986 return static_cast<ACE_UINT64
> (val
);
991 struct Truncator
<unsigned int, ACE_INT64
>
993 ACE_INT64
operator() (unsigned int val
)
995 return static_cast<ACE_INT64
> (val
);
998 #endif /* ACE_SIZEOF_INT < 8 */
1001 struct Truncator
<size_t, unsigned long>
1003 unsigned long operator() (size_t val
)
1006 (val
> static_cast<unsigned long> (ACE_Numeric_Limits
<size_t>::max ())
1007 ? ACE_Numeric_Limits
<size_t>::max ()
1008 : static_cast<size_t> (val
));
1012 // Partial specialization for the case where the types are the same.
1013 // No truncation is necessary.
1014 template<typename T
>
1015 struct Truncator
<T
, T
>
1017 T
operator() (T val
)
1023 // Partial specialization for the case where the types are the same,
1024 // but the from type is const. No truncation is necessary.
1026 // This is only necessary to workaround a problem with the BCB6
1028 template<typename T
>
1029 struct Truncator
<T
const, T
>
1031 T
operator() (T val
)
1037 // -------------------------------------
1039 template<typename TO
, typename FROM
>
1040 inline TO
truncate_cast (FROM val
)
1042 typedef Truncator
<FROM
, TO
> truncator
;
1044 return truncator() (val
);
1047 #endif /* !__BORLANDC__ || __BORLANDC__ >= 0x590 */
1049 } // namespace ACE_Utils
1051 ACE_END_VERSIONED_NAMESPACE_DECL
1053 #include /**/ "ace/post.h"
1055 #endif /* ACE_TRUNCATE_H*/